一 数据输入,处理
library(tidyverse)
library(openxlsx)
library("survival")
library("survminer")
library(randomForestSRC)
load("SKCM.uni-COX.RData")
module_expr.cox2 <- module_expr.cox %>% select(- "_PATIENT") %>%
column_to_rownames("sample")
module_expr.cox2[1:6,1:6]
# 7:3 拆分
ind <- sample(nrow(module_expr.cox2),nrow(module_expr.cox2) * 0.7 )
train <- module_expr.cox2[ind,]
test <- module_expr.cox2[-ind,]
##确保训练集和验证集的基因一致
gene_com <- intersect(colnames(train) ,colnames(test))
training <- train %>%
select(gene_com)
testing <- test %>%
select(gene_com)
training[1:4,1:4]
# OS OS.time TYRP1 IGKV4_1
#TCGA-EE-A2MM-06A 1 5107 1.38460143 5.2408878
#TCGA-EE-A2GE-06A 0 5286 0.04187911 10.1611678
#TCGA-ER-A194-01A 1 1354 9.56901508 0.3122559
#TCGA-EB-A44R-06A 1 315 0.06131739 7.3046339
二 构建随机森林生存模型
fit <- rfsrc(Surv(OS.time,OS)~.,data = training,
ntree = 1000,
nodesize = 10,
splitrule = 'logrank',
importance = T,
proximity = T,
forest = T,
seed = 1234)
importance_gene <- data.frame(fit$importance) %>%
rownames_to_column("gene") %>%
arrange(- fit.importance) %>%
head(20)
importance_gene
plot(fit,10)
ggplot(data=importance_gene, aes(x = reorder(gene, fit.importance),
y=fit.importance,fill=gene)) +
geom_bar(stat="identity") +
theme_classic() +
theme(legend.position = 'none') +
coord_flip()
三 RSF模型验证
1,使用RSF得到的重要基因构建COX模型
(1)在上面的importance_gene文件中,根据fit.importance设置阈值,然后选出候选基因 或者
(2)在上面的importance_gene文件中,直接选择TOP多少的基因作为候选基因。
然后将候选基因构建多因素COX模型,这样就可以得到文献中常见的基因系数。
注:这里的阈值和TOP没有固定的cutoff ,结果导向即可。
2,RSF模型直接验证集预测
直接使用验证集验证模型,得到每个样本的系数,然后可以使用生存分析得到Cindex以及KM曲线等。
(1)C-index
fit.p <- predict(fit, as.data.frame(testing))
testing$RSF_p <- as.vector(fit.p$predicted)
#计算C index
testing_surv <- coxph(Surv(OS.time, OS) ~ fit.p$predicted,data = testing)
summary(testing_surv)$concordance
C se(C)
0.64523954 0.03881865
(2)KM曲线
testing$RSF_score <- ifelse(testing$RSF_p > median(testing$RSF_p),"High","Low")
fit <- survfit(Surv(OS.time, as.numeric(OS)) ~ RSF_score, data=testing)
ggsurvplot(fit, data = testing,
pval = T,
risk.table = T,
surv.median.line = "hv", #添加中位生存曲线
palette=c("red", "blue"), #更改线的颜色
legend.labs=c("High risk","Low risk"), #标签
legend.title="RiskScore",
title="Overall survival", #标题
ylab="Cumulative survival (percentage)",xlab = " Time (Days)", #更改横纵坐标
censor.shape = 124,censor.size = 2,conf.int = FALSE, #删失点的形状和大小
break.x.by = 720#横坐标间隔
)
这样就完成了随机森林生存模型筛选变量或者预测的介绍,Lasso之外可以多一种尝试了。
[1] Getting starting with the randomForestSRC R-package for random forest analysis of regression, classification, survival and more • Fast Unified Random Forests with randomForestSRC
◆ ◆ ◆ ◆ ◆
精心整理(含图PLUS版)|R语言生信分析,可视化(R统计,ggplot2绘图,生信图形可视化汇总)
RNAseq纯生信挖掘思路分享?不,主要是送你代码!(建议收藏)