小杜的生信笔记 发表于 2024-6-30 16:48:57

多因素因素独立预后分析 | 随机森林图绘制

[![](data/attachment/forum/plugin_zhanmishu_markdown/202406/34ce076e36a298d40d20a7d47d3aab3a_1719737316_4731.png)](http://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455863363&idx=3&sn=1fd47c41f477e426c787675a02b710df&chksm=8cffde20bb885736cedd671cc9a23e9b058ce33aecbdde4c49274fb88645861f23839fa4c863&scene=21#wechat_redirect)

### 本期教程

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/adcaa8938f9d621c2600cc25f5ba2d6b_1719737316_8829.jpg)
多因素独立预后

> **在社群中可获得本期教程全部代码和示例数据**❞

## 写在前面

我们上一个教程做了[单因素独立预后分析](https://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455863489&idx=1&sn=aeed80ae9a9e55d55d79d0075329e6db&scene=21#wechat_redirect),今天的教程基于单因素独立预后分析结果,做**「多因素独立预后分析」**。分析原理:基于单因素Cox分析结果,我们对P值小0.05的临床分期进行多因素Cox独立预后分析。

### 往期教程部分内容

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/adcaa8938f9d621c2600cc25f5ba2d6b_1719737316_6217.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/adcaa8938f9d621c2600cc25f5ba2d6b_1719737316_2733.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/adcaa8938f9d621c2600cc25f5ba2d6b_1719737316_1276.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/adcaa8938f9d621c2600cc25f5ba2d6b_1719737316_3657.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/adcaa8938f9d621c2600cc25f5ba2d6b_1719737316_7504.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/adcaa8938f9d621c2600cc25f5ba2d6b_1719737316_5488.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/adcaa8938f9d621c2600cc25f5ba2d6b_1719737316_9709.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/adcaa8938f9d621c2600cc25f5ba2d6b_1719737316_3203.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/adcaa8938f9d621c2600cc25f5ba2d6b_1719737316_4007.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/adcaa8938f9d621c2600cc25f5ba2d6b_1719737316_1429.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/adcaa8938f9d621c2600cc25f5ba2d6b_1719737316_3068.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/adcaa8938f9d621c2600cc25f5ba2d6b_1719737316_5090.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/adcaa8938f9d621c2600cc25f5ba2d6b_1719737316_9129.jpg)

## 分析代码

```
# 多因素独立预后分析
input <- read.csv("ARFM3M_input_data.csv", header = T)
head(input)
dim(input)
##'@单因素Cox结果
rt <- read.csv("unoCox_result.csv", header = T, row.names = 1)
head(rt)
rt1 <- input[,c("sample","futime", "fustat", rownames(rt))]##根据单因素分析,筛选出p值小于0.05的因素,P值大小可结合自己需求设置,继续分析
head(rt1)
dim(rt1)
write.csv(rt1, "multi.cox.input.csv", row.names = F)
```

分析

```
rt <- read.csv("multi.cox.input.csv", header = T, row.names = 1)

##'@年龄大于65分组为2,小于设置1
rt$Age = ifelse(rt$Age >= 65,2,1)
##'@创建模型
multiCox=coxph(Surv(futime, fustat) ~ ., data = rt)
multiCoxSum=summary(multiCox)
##
outTab=data.frame()
outTab=cbind(
HR=multiCoxSum$conf.int[,"exp(coef)"],
HR.95L=multiCoxSum$conf.int[,"lower .95"],
HR.95H=multiCoxSum$conf.int[,"upper .95"],
pvalue=multiCoxSum$coefficients[,"Pr(>|z|)"])
outTab=cbind(id=row.names(outTab),outTab)
##'@保存结果
outTab
write.csv(outTab,file="multiCox_result.csv",row.names=F)
```

```
id        HR        HR.95L        HR.95H        pvalue
Age        1.8220090221.1698349162.8377652520.007957913
M        1.3858823010.9248583622.0767177240.113781591
N        1.3875005221.040098971.8509370280.025923837
Stage        1.1471839050.7677170711.7142134270.502823866
T1.417666780.9947762172.0203328790.053484719
ARFM3M        1.5104725470.8649405452.6377851380.14709173

```

```
##'@重新导入结果
rt <- read.csv("multiCox_result.csv", header = T, row.names = 1)
##
gene <- rownames(rt)
hr <- sprintf("%.3f",rt$"HR")
hrLow<- sprintf("%.3f",rt$"HR.95L")
hrHigh <- sprintf("%.3f",rt$"HR.95H")
Hazard.ratio <- paste0(hr,"(",hrLow,"-",hrHigh,")")
pVal <- ifelse(rt$pvalue<0.001, "<0.001", sprintf("%.3f", rt$pvalue))

#'@绘制随机森林图
pdf(file="multi.forest.pdf", width = 7,height = 5)
n <- nrow(rt)
nRow <- n+1
ylim <- c(1,nRow)
layout(matrix(c(1,2),nc=2),width=c(3,2.5))

#绘制森林图左边的临床信息
xlim = c(0,3)
par(mar=c(4,2.5,2,1))
plot(1,xlim=xlim,ylim=ylim,type="n",axes=F,xlab="",ylab="")
text.cex=0.8
text(0,n:1,gene,adj=0,cex=text.cex)
text(1.5-0.5*0.2,n:1,pVal,adj=1,cex=text.cex);text(1.5-0.5*0.2,n+1,'pvalue',cex=text.cex,font=2,adj=1)
text(3,n:1,Hazard.ratio,adj=1,cex=text.cex);text(3,n+1,'Hazard ratio',cex=text.cex,font=2,adj=1,)

#绘制森林图
par(mar=c(4,1,2,1),mgp=c(2,0.5,0))
xlim = c(0,max(as.numeric(hrLow),as.numeric(hrHigh)+1))
plot(1,xlim=xlim,ylim=ylim,type="n",axes=F,ylab="",xaxs="i",xlab="Hazard ratio")
arrows(as.numeric(hrLow),n:1,as.numeric(hrHigh),n:1,angle=90,code=3,length=0.05,col="darkblue",lwd=2.5)
abline(v=1,col="black",lty=2,lwd=2)
boxcolor = ifelse(as.numeric(hr) > 1, 'red', 'green')
points(as.numeric(hr), n:1, pch = 15, col = boxcolor, cex=1.3)
axis(1)

dev.off()
```

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/34ce076e36a298d40d20a7d47d3aab3a_1719737316_6761.png)

> **在社群中可获得本期教程全部代码和示例数据**❞

> 若我们的教程对你有所帮助,请 `点赞+收藏+转发`,这是对我们最大的支持。❞

### 往期部分文章

**「1. 最全WGCNA教程(替换数据即可出全部结果与图形)」**

* (https://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455850466&idx=1&sn=6036fc2a19594f7fc38d65271f21e9cc&scene=21#wechat_redirect)
* (https://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455850727&idx=1&sn=1156c2bb0b0c9baff02838f5ffce39bf&scene=21#wechat_redirect)
* (https://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455850742&idx=1&sn=3d3eeedb3b58e536a83dc38d15725cd4&scene=21#wechat_redirect)
* (https://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455854728&idx=5&sn=bc98befb8dd0f0090bdfe69f3ffdb008&scene=21#wechat_redirect)
* (https://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455856103&idx=1&sn=774f8a084d21f757266f35c501c7155d&scene=21#wechat_redirect)

---

**「2. 精美图形绘制教程」**

* [精美图形绘制教程](https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzAwODY5NDU0MA==&action=getalbum&album_id=2614156000866385923&scene=173&from_msgid=2455848496&from_itemidx=1&count=3&nolastread=1#wechat_redirect)

**「3. 转录组分析教程」**

* **「[转录组上游分析教程[零基础]](https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzAwODY5NDU0MA==&action=getalbum&album_id=2870608342451224581&scene=126&uin=&key=&devicetype=Windows+10+x64&version=63090719&lang=zh_CN&ascene=0)」**
* **「[一个转录组上游分析流程 | Hisat2-Stringtie](https://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455857417&idx=1&sn=653f3bdb0af386c22a128732ec8a01a4&scene=21#wechat_redirect)」**

**「4. 转录组下游分析」**

* [批量做差异分析及图形绘制 | 基于DESeq2差异分析](https://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455860684&idx=1&sn=7be489c453cca737ad1092c6e4499827&scene=21#wechat_redirect)
* (https://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455859512&idx=1&sn=bac01a018f8b58afc7e2b3484f476bf4&scene=21#wechat_redirect)
* [单基因GSEA富集分析](https://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455859147&idx=1&sn=b022d80868e4b8014f64d443c82d1668&scene=21#wechat_redirect)
* [全基因集GSEA富集分析](https://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455860201&idx=1&sn=dd65c5b967123a876a6f5a38d4723ca6&scene=21#wechat_redirect)

> **「小杜的生信筆記」** ,主要发表或收录生物信息学教程,以及基于R分析和可视化(包括数据分析,图形绘制等);分享感兴趣的文献和学习资料!!
页: [1]
查看完整版本: 多因素因素独立预后分析 | 随机森林图绘制