2016-07-01 49 views
-1

使用ranger包我运行下面的脚本:如何从predict.ranger的预测输出读出的索引,R

rf <- ranger(Surv(time, Y) ~ ., data = train_frame[1:50000, ], write.forest = TRUE, num.trees = 100) 

test_frame <- train_frame[50001:100000, ] 
preds <- predict(rf, test_frame) 
chfs <- preds$chf 
plot(chfs[1, ]) 

的累积危险函数具有索引1 - 在X轴36。显然这与时间一致,但我不知道如何:我的观察时间变量范围从最小值0到最大值399.原始数据与预测输出predict.ranger之间的映射是什么?我怎样才能在一段给定的时间之后,操作这个来量化给定主体的风险程度?

这里是什么我的时间/事件数据看起来像一个示例:

 Y time 
    <int> <dbl> 
1  1 358 
2  0 90 
3  0 162 
4  0 35 
5  0 307 
6  0 69 
7  0 184 
8  0 24 
9  0 366 
10  0 33 

这里就是第一个主题的瑞郎看起来像: enter image description here 谁能帮我连接点? "matrix"对象上没有行或列名称,即preds$chf

+0

'str(preds)'看起来像什么?当我用'rf < - ranger(Surv(time,status)〜。,data = veteran,write.forest = TRUE,num.trees = 100)'运行你的代码时,''chfs'有行和列。我很惊讶你可以用生存的重要性''杂质''。另外,。无论如何,你可以使这种重现?我很惊讶你可以使用重要性''杂质'与生存 – Jota

+0

这是一个旧的代码行。 “重要性='杂质'确实会引发错误。我不在我的工作区前面,但是'str(preds)'是类'ranger.predict'的命名列表。我的CHF和生存函数也是矩阵。 @ mnwright在下面的答案击中了头部。 – Aaron

回答

3

在预测对象中是包含计算CHF和生存估计值的时间点的名为unique.death.times的载体。 chf矩阵在列中的行和这些时间点中具有观察值,对于survival也是如此。

重复的例子:

library(survival) 
library(ranger) 

## Split the data 
n <- nrow(veteran) 
idx <- sample(n, 2/3*n) 
train <- veteran[idx, ] 
test <- veteran[-idx, ] 

## Grow RF and predict 
rf <- ranger(Surv(time, status) ~ ., train, write.forest = TRUE) 
preds <- predict(rf, test) 

## Example CHF plot 
plot(preds$unique.death.times, preds$chf[1, ]) 

## Example survival plot 
plot(preds$unique.death.times, preds$survival[1, ]) 

设置importance = "impurity"生存的森林应该抛出一个错误。

+0

钉了它,谢谢!而且,重要='杂质'确实是通过一个错误。这是来自一个较旧的脚本。 – Aaron