美文网首页特征变量选择
随机森林找分类marker | R

随机森林找分类marker | R

作者: kkkkkkang | 来源:发表于2021-08-28 21:27 被阅读0次

metagenomics中,寻找区分两个类别的marker是很常见的分析。根据很多文章中的结果,摸索了一下怎么实现

数据准备

Otu为列名,添加一个分类列如status

数据
代码部分

首先我定义了一个自己常用的主题

my_theme <- function() {
    library(ggplot2)
    library(ggthemr)
    theme_set(theme_classic() +
                  theme(axis.text = element_text(size = 8, face = "bold"), #坐标轴标签大小,加粗
                        axis.title = element_text(size = 12, face = "bold"), #坐标轴标题大小,加粗
                        plot.margin = unit(rep(1, 4), "cm"), #边距,顺序:上右下左
                        panel.grid = element_blank())) #不要横向和纵向格子线
}

正儿八经开始画图

setwd("D:/bioinfo/R learn")
library(reshape2)
library(randomForest)
library(dplyr)
library(ggplot2)
library(magrittr)
library(patchwork)
my_theme()
otu <- read.table("randomforest.txt", header = T, row.names = 1, sep = "\t")
set.seed(123)
nsamp <- floor(nrow(otu))
# 随机选择70%数据作为训练集
indexes <- sample(1:nrow(otu), size = nsamp*0.7)
training <- otu[indexes,]
training$Status <- factor(training$Status)
validation <- otu[-indexes,]
validation$Status <- factor(validation$Status)
# 训练
rf_classifier <- randomForest(Status ~ ., data=training, ntree=100, importance=TRUE)
rf_classifier
varImpPlot(rf_classifier)
# 当然可以提取出来自己画图
# 首先寻找合适个数的预测变量
# 通过交叉验证来寻找
otu_train.cv <- replicate(5, rfcv(training[-ncol(training)], 
                                  training$Status, 
                                  cv.fold = 10, step = 2), simplify = FALSE)
otu_train.cv
#提取验证结果绘图
otu_train.cv.df <- data.frame(sapply(otu_train.cv, '[[', 'error.cv'))
colnames(otu_train.cv.df) <- c("err1", "err2", "err3", "err4", "err5")
otu_train.cv.df %<>% .[-nrow(.),] %>% mutate(errmean = rowMeans(.)) %>% mutate(num = as.numeric(rownames(.)))
# 选择9个预测变量
p1 <- ggplot() +
    geom_line(aes(x = otu_train.cv.df$num, y = otu_train.cv.df$err1), colour = 'grey', lwd = 1.5) +
    geom_line(aes(x = otu_train.cv.df$num, y = otu_train.cv.df$err2), colour = 'grey', lwd = 1.5) +
    geom_line(aes(x = otu_train.cv.df$num, y = otu_train.cv.df$err3), colour = 'grey', lwd = 1.5) +
    geom_line(aes(x = otu_train.cv.df$num, y = otu_train.cv.df$err4), colour = 'grey', lwd = 1.5) +
    geom_line(aes(x = otu_train.cv.df$num, y = otu_train.cv.df$err5), colour = 'grey', lwd = 1.5) +
    geom_line(aes(x = otu_train.cv.df$num, y = otu_train.cv.df$errmean), colour = 'black', lwd = 1.5) +
    geom_vline(xintercept = 9, colour='black', lwd=1, linetype="dashed") +
    labs(title=paste('Training set (n = ', length(indexes),')', sep = ''), 
         x='Number of classes ', y='Cross-validation error rate') + 
    coord_trans(x = "log2") +
    scale_x_continuous(breaks = c(1, 2, 5, 10, 20, 30, 50, 100, 140)) +
    annotate("text", x = 9, y = max(otu_train.cv.df$errmean), label=paste("optimal = ", 9, sep=""))
# 选择前9个important feature画图
imp <- importance(rf_classifier) %>% 
    data.frame() %>% 
    arrange(desc(MeanDecreaseAccuracy)) %>% 
    head(n = 9) %>% 
    select(3) %>%
    mutate(class = rownames(.)) %>% 
    mutate(phylum = sapply(strsplit(rownames(.), ".", fixed = TRUE), "[[",2))
imp_long <- melt(imp, id.vars = c("class", "phylum")) %>% arrange(value)
imp_long$class <- factor(imp_long$class, levels = imp_long$class)
imp_long$phylum <- factor(imp_long$phylum)

p2 <- ggplot(imp_long, aes(x= class, y = value, fill = phylum)) +
    geom_bar(stat= "identity") + 
    coord_flip() + 
    scale_fill_brewer(palette = "Paired")
# 拼图
p1+p2+plot_annotation(title = "Fig 1", tag_levels = "A")

results

Fig 1. A 部分代码参考了如下文章,如果用到,请正确引用别人的文章(这不是我的文章)
ref: https://www.nature.com/articles/s41587-019-0104-4

相关文章

  • 随机森林找分类marker | R

    metagenomics中,寻找区分两个类别的marker是很常见的分析。根据很多文章中的结果,摸索了一下怎么实现...

  • 随机森林

    随机森林(原理/样例实现/参数调优) R包randomForest的随机森林分类模型以及对重要变量的选择 简单易懂...

  • 扩增子随机森林图 2022-07-27

    R语言randomForest包的随机森林分类模型以及对重要变量的选择 https://cloud.tencent...

  • 何为决策树和随机森林?

    随机森林 定义:随机森林或随机决策森林是用于分类、回归和其他任务的集成学习方法。 名字由来:随机森林就是使用随机的...

  • 第八章 数据决策分析算法——基于随机森林的决策分类

    8.4 基于随机森林的决策分类 随机森林是一种一个包含多个决策树的分类器,是用随机的方法建立一个森林,森林里面由很...

  • 随机森林-R

    这里与Python的区别在于R中有一种变量叫factor,是专门用来表示分类对象的,我们需要把分类结果转换成fac...

  • 【转】机器学习算法---随机森林实现(包括回归和分类)

    1.随机森林回归和分类的不同: 随机森林可以应用在分类和回归问题上。实现这一点,取决于随机森林的每颗cart树是分...

  • 2018-07-05

    数据挖掘实验报告 实验要求: 对样本进行二分类,获取分类概率 采用方法: 随机森林 方法简介 随机森林就是通过集成...

  • 随机森林算法梳理

    随机森林分类器是一种基于装袋(bagging)全称Bootstrap Aggregation。的集成算法 随机森林...

  • 分类算法 - 随机森林

    一、定义 上次我写了决策树算法,决策树可以解决分类问题,也有CART算法可以解决回归问题,而随机森林也和决策树非常...

网友评论

    本文标题:随机森林找分类marker | R

    本文链接:https://www.haomeiwen.com/subject/frdiiltx.html