如何用pairwise.adonis2针对特定因子水平做成对PERMANOVA比较?
针对不同深度水平单独开展年份的PERMANOVA成对比较
要分别在10m深度和50m深度下检验2020年与2023年的群落差异,可通过拆分数据集或批量分组分析实现,以下是具体方案:
准备工作(优化数据处理)
先将原始数据中的分类变量转换为因子类型,避免数值型干扰:
library(tidyverse) library(vegan) library(pairwiseAdonis) # 原始数据 df <- structure(list(Sample = c("A1", "A2", "A3", "A4", "A5", "B1", "B2", "B3", "B4", "B5", "C1", "C2", "C3", "C4", "C5", "D1", "D2", "D3", "D4", "D5"), Depth = c(10, 10, 10, 10, 10, 50, 50, 50, 50, 50, 10, 10, 10, 10, 10, 50, 50, 50, 50, 50), Year = c(2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023), Sp1 = c(0, 0, 0, 0, 1, 0, 5, 0, 0, 8, 0, 6, 0, 4, 0, 12, 16, 6, 8, 4), Sp2 = c(3, 7, 9, 2, 6, 4, 5, 9, 4, 3, 6, 8, 6, 4, 7, 9, 2, 5, 3, 3), Sp3 = c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0), Sp4 = c(11, 32, 12, 17, 5, 0, 13, 19, 3, 4, 5, 0, 24, 20, 15, 3, 2, 0, 1, 8), Sp5 = c(5, 6, 7, 2, 3, 4, 8, 1, 1, 1, 0, 12, 6, 3, 8, 6, 2, 0, 1, 2), Sp6 = c(0, 0, 0, 0, 0, 5, 6, 7, 3, 6, 0, 0, 0, 0, 0, 15, 12, 4, 8, 11)), row.names = c(NA, -20L), class = "data.frame") # 转换分类变量为因子类型 df_combined <- df %>% mutate(Depth = as.factor(Depth), Year = as.factor(Year))
方法一:手动拆分数据集单独分析
分别提取10m和50m深度的子集,独立运行PERMANOVA:
# 处理10m深度数据 depth_10 <- df_combined %>% filter(Depth == 10) species_10 <- depth_10 %>% select(starts_with("Sp")) factors_10 <- depth_10 %>% select(Sample, Depth, Year) # 10m深度下年份对群落的PERMANOVA adonis_depth10 <- adonis2(species_10 ~ Year, data = factors_10, permutations = 9999, method = "bray") cat("10m深度下年份的PERMANOVA结果:\n") print(adonis_depth10) # 处理50m深度数据 depth_50 <- df_combined %>% filter(Depth == 50) species_50 <- depth_50 %>% select(starts_with("Sp")) factors_50 <- depth_50 %>% select(Sample, Depth, Year) # 50m深度下年份对群落的PERMANOVA adonis_depth50 <- adonis2(species_50 ~ Year, data = factors_50, permutations = 9999, method = "bray") cat("\n50m深度下年份的PERMANOVA结果:\n") print(adonis_depth50)
方法二:分组批量处理(高效)
用dplyr的分组功能批量运行各深度下的PERMANOVA,适合多水平场景:
# 按Depth分组,批量执行PERMANOVA depth_year_adonis <- df_combined %>% group_by(Depth) %>% group_modify(function(data, key) { species_data <- data %>% select(starts_with("Sp")) adonis_result <- adonis2(species_data ~ Year, data = data, permutations = 9999, method = "bray") # 将结果转换为数据框,统一输出 as.data.frame(adonis_result) %>% mutate(Depth = key$Depth) }) %>% ungroup() cat("各深度下年份的PERMANOVA批量结果:\n") print(depth_year_adonis)
成对比较(含多重检验控制)
若需更细致的年份成对差异检验,可在每个深度子集内运行pairwise.adonis2:
# 10m深度下年份成对比较 pairwise_depth10 <- pairwise.adonis2(species_10 ~ Year, data = factors_10, permutations = 9999) cat("\n10m深度下年份的成对比较结果:\n") print(pairwise_depth10) # 50m深度下年份成对比较 pairwise_depth50 <- pairwise.adonis2(species_50 ~ Year, data = factors_50, permutations = 9999) cat("\n50m深度下年份的成对比较结果:\n") print(pairwise_depth50)
通过对比两个深度的PERMANOVA结果(R²值、P值),即可判断不同深度的群落受年份影响的差异。
内容的提问来源于stack exchange,提问作者mbroadribb
相关产品推荐
相关产品推荐

