【问题标题】:How do I apply survival analysis in R to a new dataset?如何将 R 中的生存分析应用于新数据集?
【发布时间】:2019-10-08 09:55:45
【问题描述】:

代码直接从 Data Camp 的 Marketing Analytics in R 模块提取,并应用于新的客户数据,但在将模型应用于新数据集后,我对如何处理结果感到困惑。

我有带有常数变量公式的 cox ph 模型,如下所示

fitCPH1 <- cph(Surv(tenure, purchase) ~ gender + 
                 maritalstatus +  age + monthlypurchase,
               data = customer,                
               x = TRUE, 
               y = TRUE, 
               surv = TRUE,                
               tenure.inc = 1)

我已经验证了中间的模型,现在想将结果应用到新的数据集。 (带有 3 个测试行的模拟客户数据 2.csv)

newdata <- read.csv (file = "mockcustomerdata2.csv",
                      header = TRUE,
                      stringsAsFactors = TRUE,
                      row.names =1,
                      sep=",")

做了

survfit(formula = fitCPH1, newdata = newdata)

运行该行,我得到一个 3 行结果,显示 n、事件、中位数(这是每个新数据点执行事件的中位数时间)和 0.95LCL/UCL。

__________________________________________
 |  n | events | median | 0.95LCL | 0.95UCL|
1|1000| 281    | 332    | 305     | 361    |
2|1000| 281    | 320    | 297     | 350    |
3|1000| 281    | 322    | 298     | 355    | 

我想要做的是获取每个数据点的汇总结果,并将其与我的新数据集合并,这样我就有了每个数据点的预期值(中值)、上限和下限,以预测它们何时出现会做一个活动。

这可能吗?我该怎么做?

【问题讨论】:

  • survfit(formula = fitCPH1$call, data = newdata) 工作吗?
  • predict(fittedmodel, newdata) 怎么样?
  • @jay.sf 函数原来是 surv_median()。不过谢谢!

标签: r survival-analysis survival


【解决方案1】:

使用函数surv_median() 解决了这个问题,该函数将结果表存储到数据框中,然后可以与newdata 合并。希望这对某人有所帮助!


results <- survfit(formula = fitCPH1, newdata = newdata)

medianvalues <- surv_median(results) #Turns results into a dataframe

#The strata column needs to be converted to a row.name, hence the step below
medianvaluesdf <- data.frame(medianvalues[,-1], row.names=medianvalues[,1])

merged <- merge(newdata, medianvalues, by = "row.names")

【讨论】:

    猜你喜欢
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多