从列表将预测变量传递给Cox模型

时间:2020-01-21 15:29:54

标签: r list parameter-passing batch-processing cox-regression

我正在从survminer包中运行一个非常简单的cox模型。

surv_object <- Surv(time, event)
model <- coxph(surv_object ~ female + age + ethnicity + imd, data = df)

我需要运行多个Cox模型,并且对于每个模型,我的预测变量都会发生变化。我将所有预测变量存储在这样的单独数据框中(我们将其称为pred_df):

> pred_df    

# A tibble: 4 x 2
       predictor    endpoint 
       <chr>        <chr>    
     1 female       Mortality
     2 age          Mortality
     3 ethnicity    Mortality
     4 imd          Mortality

是否有一种简单的方法将项目从predictor列传递到coxph()?像这样:

coxph(surv_object ~ predictors, data = df)

我已经尝试过的内容:

我已经尝试过以下方法:

pred_vars <- pred_df %>% 
     pull(predictor) %>%                 # extract column values as a vector
     paste(collapse = " + ") %>%         # combine values in a string
     parse(text = . )                    # parse the string as an expression

model <- coxph(surv_object ~ eval(pred_vars), data = df)

R实际上了解这一点并运行模型。但是输出是无法解释的。该模型似乎正在运行,但是没有输出单独的预测变量,即femaleageethnicityimd。相反,它仅输出eval(pred_vars)

Call:
coxph(formula = Surv(time, event) ~ eval(pred_vars), data = df)

  n= 62976, number of events= 12882 
   (3287 observations deleted due to missingness)

                     coef exp(coef)  se(coef)     z Pr(>|z|)    
eval(pred_vars) 3.336e-05 1.000e+00 5.339e-06 6.249 4.14e-10 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

                exp(coef) exp(-coef) lower .95 upper .95
eval(pred_vars)         1          1         1         1

Concordance= 0.515  (se = 0.003 )
Rsquare= 0.001   (max possible= 0.989 )
Likelihood ratio test= 38.28  on 1 df,   p=6e-10
Wald test            = 39.04  on 1 df,   p=4e-10
Score (logrank) test = 39.07  on 1 df,   p=4e-10

必须有一种更简单的方法吗?

2 个答案:

答案 0 :(得分:1)

尝试重新配制。

public class Toto {
    [Display(Name = "Identifier", Order = 2)
    public int Id { get; set; }

    [Display(Name = "Description", Order = 1)
    public string Label {get; set; }
}

答案 1 :(得分:0)

您可以在as.formulapaste(..., collapse = " + ")中以R为基数,例如...

foo <- as.formula(paste0("Surv(time, event) ~ ", paste(pred_df$predictors, collapse = " + ")))

该行的结果:

> foo
Surv(time, event) ~ female + age + ethnicity + imd

然后您只需将foo传递给对coxph的呼叫即可。