【问题标题】:Trying to specify predict.coxph type within map2() function试图在 map2() 函数中指定 predict.coxph 类型
【发布时间】:2018-02-14 18:53:33
【问题描述】:

过去几天我一直在网上搜索,查看 map2 的文档。我已经获取了一个训练集,嵌套了数据并为其创建了 coxph 模型,将这些模型保存在嵌套表中。现在我想从那个模型中预测,但我想使用 type="expected" as,根据文档 (R documentation: predict.coxph)

受试者的生存概率等于 exp(-expected)

我已经修改了相关代码以使用 mpg 数据集重现我的问题。

我在下面有 4 个示例,这些示例在预测功能起作用之后不起作用。请注意,我已经从这个集合中删除了 coxph.null 模型,所以唯一的模型是类(coxph)。此代码可用于复制错误。

#Needed libraries
library(ggplot2)
library(tidyverse)
library(purrr)
library(broom)
library(survival)
#Create data set
mpg_data <- mpg
mpg_data <- mpg_data %>% 
  mutate(mpg_diff = cty - hwy)
mpg_data <- mpg_data %>% 
  mutate(EVENT = (mpg_diff >= -8))
set.seed(1)
mpg_data <- mpg_data %>% 
  mutate(TIME_TO_EVENT = as.integer(runif(234, 1, 100)))
mpg_nested <- mpg_data %>% 
  group_by(manufacturer) %>% 
  mutate(n_prot = length(model)) %>% 
  nest()
# Stepwise regression 
stepwise <- function(data) {
  response <- Surv(time = data$TIME_TO_EVENT, event = data$EVENT, type = "right") 
full <- "Surv(time = data$TIME_TO_EVENT, event = data$EVENT, type = 'right') ~ data$cyl+data$cty+data$hwy+data$displ"
x <- factor(as.factor(data$model))
full <- ifelse(nlevels(x) >= 2, paste(full, "as.character(data$model)", sep = "+"), full)
x <- factor(as.factor(data$trans))
full <- ifelse(nlevels(x) >= 2, paste(full, "as.character(data$trans)", sep = "+"), full)
x <- factor(as.factor(data$drv))
full <- ifelse(nlevels(x) >= 2, paste(full, "as.character(data$drv)", sep = "+"), full)
null_model_ONE <- coxph(response ~ 1, data=data)
full_model_ONE <- coxph(as.formula(full), data=data)
model_ONE <- step(null_model_ONE, scope=list(lower=null_model_ONE, upper=full_model_ONE))
}
survival_mpg <- mpg_nested %>%  
  mutate(model_fit = map(data, stepwise))
#Predicting values
#This works but is not type="expected"
survival_mpg_predict <- survival_mpg %>% 
  mutate(mpg_predict = map2(model_fit, data, predict))
##TRY 1##
predict.F <- function(model_fit, data){
  predict(model_fit, newdata=data, type="expected")
}
survival_mpg_predict <- survival_mpg %>% 
  mutate(mpg_predict = map2(model_fit, data, predict.F))
#Error in mutate_impl(.data, dots) : Evaluation error: requires numeric/complex matrix/vector arguments.
##Try 2##
survival_mpg_predict <- survival_mpg %>% 
  mutate(mpg_predict = map2(model_fit, data, predict(model_fit, newdata = data, type="expected")))
#Error in mutate_impl(.data, dots) : Evaluation error: no applicable method for 'predict' applied to an object of class "list".
##Try 3##
survival_mpg_predict <- survival_mpg %>% 
  mutate(mpg_predict = map2(model_fit, data, ~ predict(.x, newdata = .y, type="expected")))
#Error in mutate_impl(.data, dots) : Evaluation error: requires numeric/complex matrix/vector arguments.
##Try 4##
survival_mpg_predict <- survival_mpg %>% 
  mutate(mpg_predict = map2(model_fit, data, function(model_fit, data) predict(model_fit, newdata=data, type="expected")))
#Error in mutate_impl(.data, dots) : Evaluation error: requires numeric/complex matrix/vector arguments.

【问题讨论】:

  • 看起来这是由于模型中没有变量。例如,看看列表中的第二个模型。如果您尝试使用 newdata 参数进行预测,则会得到相同的错误:predict(survival_mpg$model_fit[[2]], survival_mpg$data[[2]], type = "expected")。如果你不给它一个新的数据集,它似乎可以工作。由于您使用与模型拟合相同的数据进行预测,因此您可以将newdata 参数全部删除。
  • 感谢您的快速回复。对于这组,我试图预测相同的数据。因此,删除 newdata 参数并切换到 map() 函数就可以了!

标签: r prediction survival-analysis purrr cox-regression


【解决方案1】:

修改 ##TRY 1## 以删除 newdata 参数并将 map2() 函数更改为 map() 函数工作

predict.F <- function(model_fit, data){
predict(model_fit, type="expected")
}
survival_mpg_predict <- survival_mpg %>% 
mutate(mpg_predict = map(model_fit, predict.F))

【讨论】:

    猜你喜欢
    • 2016-10-16
    • 2018-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多