R:绘制个人预测 [英] R: Plot Individual Predictions

查看:65
本文介绍了R:绘制个人预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R编程语言.我正在尝试按照本教程进行操作:

但是,这似乎为最近两次经历状态1对状态2"的观察结果产生了平均危害.

有没有办法绘制第一次观察和第二次观察的个体危害?

谢谢

我知道如何针对此软件包中的其他功能执行此操作,例如在这里,您可以一次绘制7条观测值的这些曲线:

  data(退伍军人,包裹="randomForestSRC")plot.survival(rfsrc(Surv(time,status)〜.,退伍军人),cens.model ="rfsrc")## pbc数据数据(pbc,数据包="randomForestSRC")pbc.obj<-rfsrc(Surv(天,状态)〜.,pbc)##使用子集专注于特定个人plot.survival(pbc.obj,子集= c(3,10)) 

此示例似乎一次显示了7个观察值的预测生存曲线(加上置信区间-红线是平均值).但是我仍然不知道如何针对"plot.competing.risk"进行此操作.功能.

我认为可能有一种间接的方法来解决此问题-您可以分别预测每个观察值:

  #use模型分别预测最后两个观测值f1<-预测(follic.obj,follic [540,])f2<-预测(follic.obj,follic [541,])#绘制单个曲线plot.competing.risk(f1)plot.competing.risk(f2) 

但是我希望有一种更简单的方法来做到这一点.有人知道吗?

解决方案

一种可能的方法是修改单行的函数 plot.competing.risk ,并在 for 循环以重叠单个行,如下所示.

  #use模型预测最后三个观察值f<-预测(follic.obj,follic [539:541,])&par(mfrow = c(2,2))对于(1:3中的k){#k用于情节类型for(i in 1:dim(x $ chf)[1]){#i x中的所有个人#cschf<-apply(x $ chf,c(2,3),平均值,na.rm = TRUE)#原始组平均值cschf = x $ chf [i ,,]#个体值#cif<-apply(x $ cif,c(2,3),平均值,na.rm = TRUE)#原始组平均值cif = x $ cif [i ,,]#个体值cpc<-do.call(cbind,lapply(1:ncol(cif),function(j){cif [,j]/(1-rowSums(cif [,-j,drop = FALSE]))}))如果(k == 1){matx = cschf范围=范围(x $ chf)}如果(k == 2){matx = CIF范围=范围(x $ cif)}如果(k == 3){matx =每次点击费用range = c(0,1)#手动分配,暂时}ylab = c(特定原因的CHF",概率(%)",概率(%)")[k]matplot(x $ time.interest,matx,type ='l',lty = 1,lwd = 3,col = 1:2,add = ifelse(i == 1,F,T),ylim = range,xlab ="Time",ylab = ylab)#ADD标签用于重叠单独的行}图例<-粘贴(c("CSCHF","CIF","CPC")[k],1:2,")图例("bottomright",图例=图例,col =(1:2),lty = 1,lwd = 3)} 

I am using the R programming language. I am trying to follow this tutorial :https://rdrr.io/cran/randomForestSRC/man/plot.competing.risk.rfsrc.html

This tutorial shows how to use the "survival random forest" algorithm - an algorithm used to analyze survival data. In this example, the "follic" data set is used, the survival random forest algorithm is used to analyze the instant hazard of observation experiencing "status 1" vs "status 2" (this is called "competing risks).

In the code below, the survival random forest model is trained on the follic data set using all observations except the last two observations. Then, this model is used to predict the hazards of the last two observations:

#load library
library(randomForestSRC)

#load data
data(follic, package = "randomForestSRC")

#train model on all observations except the last 2 observations
follic.obj <- rfsrc(Surv(time, status) ~ ., follic[c(1:539),], nsplit = 3, ntree = 100)

#use model to predict the last two observations
f <- predict(follic.obj, follic[540:541, ])

#plot individual curves - does not work
plot.competing.risk(f)

However, this seems to produce the average hazards for the last two observations experiencing "status 1 vs status 2".

Is there a way to plot the individual hazards of the first observation and the second observation?

Thanks

EDIT1:

I know how to do this for other functions in this package, e.g. here you can plot these curves for 7 observations at once:

data(veteran, package = "randomForestSRC") 
plot.survival(rfsrc(Surv(time, status)~ ., veteran), cens.model = "rfsrc")

## pbc data
data(pbc, package = "randomForestSRC") 
pbc.obj <- rfsrc(Surv(days, status) ~ ., pbc)

## use subset to focus on specific individuals
plot.survival(pbc.obj, subset = c(3, 10))

This example seems to show the predicted survival curves for 7 observations (plus the confidence intervals - the red line is the average) at once. But I still do not know how to do this for the "plot.competing.risk" function.

EDIT2:

I think there might be an indirect way to solve this - you can predict each observation individually:

#use model to predict the last two observations individually
f1 <- predict(follic.obj, follic[540, ])
f2 <- predict(follic.obj, follic[541, ])

#plot individual curves 
plot.competing.risk(f1)
plot.competing.risk(f2)

But I was hoping there was a more straightforward way to do this. Does anyone know how?

解决方案

One possible way is to modify the function plot.competing.risk for individual line, and plot over a for loop for overlapping individual lines, as shown below.

#use model to predict the last three observations
f <- predict(follic.obj, follic[539:541, ])

x <- f
par(mfrow = c(2, 2))
for (k in 1:3) { #k for type of plot
    for (i in 1:dim(x$chf)[1]) { #i for all individuals in x
        #cschf <- apply(x$chf, c(2, 3), mean, na.rm = TRUE) #original group mean
        cschf = x$chf[i,,] #individual values

        #cif <- apply(x$cif, c(2, 3), mean, na.rm = TRUE) #original group mean
        cif = x$cif[i,,] #individual values

        cpc <- do.call(cbind, lapply(1:ncol(cif), function(j) {
                cif[, j]/(1 - rowSums(cif[, -j, drop = FALSE]))
            }))

        if (k==1)
            {matx = cschf
            range = range(x$chf)
            }
        if (k==2)
            {matx = cif
            range = range(x$cif)
            }
        if (k==3)
            {matx = cpc
            range = c(0,1) #manually assign, for now
            }

        ylab = c("Cause-Specific CHF","Probability (%)","Probability (%)")[k]
        matplot(x$time.interest, matx, type='l', lty=1, lwd=3, col=1:2, 
            add=ifelse(i==1,F,T), ylim=range, xlab="Time", ylab=ylab) #ADD tag for overlapping individual lines
        }
    legend <- paste(c("CSCHF","CIF","CPC")[k], 1:2, "  ")
    legend("bottomright", legend = legend, col = (1:2), lty = 1, lwd = 3)
    }

这篇关于R:绘制个人预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆