随机森林模型重要性分析:使用R语言分析OTU数据与TOC之间的关系

本文将详细解读使用R语言进行随机森林模型分析的步骤,并展示如何提取重要性变量、绘制柱状图,以及进行显著性检验,最终得到总体显著性结果。

代码示例

set.seed(123)
otu_forest <- rfPermute(TOC~., data = otu, importance = TRUE, ntree = 500,nrep = 99, 
                        num.cores = 1)
otu_forest$rf # 查看构建模型的情况


importance_otu <- otu_forest$rf$importance
importance_otu

importance_otu_IncNodePurity <- importance_otu[order(importance_otu[,2], decreasing = TRUE), ]# decreasing = TRUE降序的意思
#再将原otu中的10种otu提取出来
otu.select <- importance_otu_IncNodePurity[1:10, ]
otu.select <- as.data.frame(otu.select)
otu.select <- otu[,row.names(otu.select)]
otu.select <- otu.select[rownames(TOC), ]
otu.select <- cbind(otu.select, TOC)
#用新的otu跑随机森林
set.seed(111)
otu_select_forest <- rfPermute(TOC~., data = otu.select, importance = TRUE, ntree = 1000,nrep = 1000, num.cores = 1)
importance_otu_select_forest <- data.frame(importance(otu_select_forest, scale = TRUE), check.names = FALSE)#scale = TRUE标准化
importance_otu_select_forest$OTU_name <- rownames(importance_otu_select_forest)
importance_otu_select_forest <- importance_otu_select_forest[order(importance_otu_select_forest[,3], decreasing = TRUE), ]
importance_otu_select_forest$OTU_name <- factor(importance_otu_select_forest$OTU_name, levels = importance_otu_select_forest$OTU_name)

otu_select_forest$rf

p1 <- ggplot(importance_otu_select_forest, aes(OTU_name, IncNodePurity))+
  geom_col(width = 0.5, fill = '#FFC068', color = NA) +
  labs(title =NULL, x = NULL, y = 'IncNodePurity', fill = NULL) +
  theme(panel.grid = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = 'black')) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  scale_y_continuous(expand = c(0, 0), limit = c(0, 60)) +
  annotate('text', label = sprintf('italic(R^2) == %.2f', 68.93), x = 10, y = 40, size = 3, parse = TRUE)

p1


#####################进行显著性P值校验###########################
######IncNodePurity
for (OTU in rownames(importance_otu_select_forest)) {
  if (importance_otu_select_forest[OTU,'IncNodePurity.pval'] >= 0.05) importance_otu_select_forest[OTU,'IncNodePurity.sig'] <- ''
  else if (importance_otu_select_forest[OTU,'IncNodePurity.pval'] >= 0.01 & importance_otu_select_forest[OTU,'IncNodePurity.pval'] < 0.05) importance_otu_select_forest[OTU,'IncNodePurity.sig'] <- '*'
  else if (importance_otu_select_forest[OTU,'IncNodePurity.pval'] >= 0.001 & importance_otu_select_forest[OTU,'IncNodePurity.pval'] < 0.01) importance_otu_select_forest[OTU,'IncNodePurity.sig'] <- '**'
  else if (importance_otu_select_forest[OTU,'IncNodePurity.pval'] < 0.001) importance_otu_select_forest[OTU,'IncNodePurity.sig'] <- '***'
}

p1 <- p1 +
  geom_text(data = importance_otu_select_forest, aes(x = OTU_name,  y=IncNodePurity, label = IncNodePurity.sig),position="stack",stat="identity")

p1


##############计算总体显著性
library(A3)
set.seed(333)
otu_forest.pval <- a3(TOC~., data = otu.select, model.fn = randomForest, p.acc = 0.001, model.args = list(importance = TRUE, ntree = 500))
otu_forest.pval


p1 <- p1 +
  annotate('text', label = sprintf('italic(P) < %.3f', 0.001), x = 10, y = 45, size = 3)
p1

代码解释

  1. set.seed(123): 设置随机数种子,保证每次运行结果一致。
  2. otu_forest <- rfPermute(TOC~., data = otu, importance = TRUE, ntree = 500,nrep = 99, num.cores = 1): 构建随机森林模型,预测变量为TOC,自变量为otu数据集中的所有列,重要性为TRUE,树的数量为500,重复99次,使用单核运算。
  3. importance_otu <- otu_forest$rf$importance: 从随机森林模型中提取每个自变量的重要性。
  4. importance_otu_IncNodePurity <- importance_otu[order(importance_otu[,2], decreasing = TRUE), ]: 按照IncNodePurity(节点纯度)的大小降序排列自变量的重要性。
  5. otu.select <- importance_otu_IncNodePurity[1:10, ]: 从按重要性排序后的自变量中选择前十个作为新的otu数据集。
  6. set.seed(111): 设置随机数种子,保证每次运行结果一致。
  7. otu_select_forest <- rfPermute(TOC~., data = otu.select, importance = TRUE, ntree = 1000,nrep = 1000, num.cores = 1): 使用新的otu数据集构建随机森林模型,预测变量为TOC,自变量为新的otu数据集中的所有列,重要性为TRUE,树的数量为1000,重复1000次,使用单核运算。
  8. importance_otu_select_forest <- data.frame(importance(otu_select_forest, scale = TRUE), check.names = FALSE): 从新的随机森林模型中提取每个自变量的重要性,并标准化。
  9. importance_otu_select_forest$OTU_name <- rownames(importance_otu_select_forest): 将每个自变量的名称作为新的一列。
  10. importance_otu_select_forest <- importance_otu_select_forest[order(importance_otu_select_forest[,3], decreasing = TRUE), ]: 按照重要性大小降序排列自变量。
  11. importance_otu_select_forest$OTU_name <- factor(importance_otu_select_forest$OTU_name, levels = importance_otu_select_forest$OTU_name): 将自变量名称转换为因子变量,以便于后续绘图。
  12. otu_select_forest$rf: 查看新的随机森林模型构建情况。
  13. p1 <- ggplot(importance_otu_select_forest, aes(OTU_name, IncNodePurity))...: 绘制重要性排名前十的自变量的IncNodePurity(节点纯度)值的柱状图,并添加主题、标签、坐标轴等设置。
  14. for (OTU in rownames(importance_otu_select_forest))...: 对每个自变量进行显著性检验,并将结果以星号的形式标记在图中。
  15. library(A3): 加载A3包,用于计算总体显著性。
  16. set.seed(333): 设置随机数种子,保证每次运行结果一致。
  17. otu_forest.pval <- a3(TOC~., data = otu.select, model.fn = randomForest, p.acc = 0.001, model.args = list(importance = TRUE, ntree = 500)): 计算随机森林模型的总体显著性,p.acc = 0.001表示期望的p值精度为0.001。
  18. p1 <- p1 + annotate('text', label = sprintf('italic(P) < %.3f', 0.001), x = 10, y = 45, size = 3): 在图中添加总体显著性的p值。

总结

本文展示了使用R语言进行随机森林模型分析的步骤,并详细解释了每个步骤的含义和参数设置。通过提取重要性变量、绘制柱状图、进行显著性检验,最终得到了总体显著性结果。该方法可以帮助研究人员深入分析OTU数据与TOC之间的关系,并识别出对TOC影响最大的OTU。

注意:

  • 代码中的 otu 和 TOC 需要替换成您的实际数据。
  • 您可以根据您的研究目的调整模型参数,例如树的数量、重复次数等。
  • 本文只展示了基本的操作步骤,具体的应用可能需要进行更深入的探索。
随机森林模型重要性分析:使用R语言分析OTU数据与TOC之间的关系

原文地址: https://www.cveoy.top/t/topic/owzF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录