0

I have a regular Surv object from the survival package;

s <- Surv(sample(100:150, 5), sample(c(T, F), 5, replace = T))

And a matrix of multiple variables;

df <- data.frame(var1 = rnorm(5),
                 var2 = rnorm(5),
                 var3 = rnorm(5))

I need to fit a Cox-PH model for each variable separately. My code currently uses a loop as follows:

for (v in colnames(df)) {
    coxph(s ~ df[[v]])
}

Of course, in reality there are thousands of variables and this process takes a bit. I wanted to follow the answer given here to try and do it all with tidyr but I'm kinda stumped because the predictand isn't a factor, it's a survival object, so I don't quite know how to handle it as part of a tibble.

Ronny Efronny
  • 1,148
  • 9
  • 28

1 Answers1

1

Assuming your response is s for the survival model, you can use a nested dataframe similar to the answer you link to, then map the model to the different variables:

library(tidyverse)

df_nested <- df %>% pivot_longer(cols = var1:var3) %>% group_by(name) %>% nest()


surv_model <- function(df) {
  coxph(s ~ df$value)
}

df_nested <- df_nested %>% mutate(model = map(data, surv_model))

df_nested
# A tibble: 3 x 3
# Groups:   name [3]
  name  data             model  
  <chr> <list>           <list> 
1 var1  <tibble [5 x 1]> <coxph>
2 var2  <tibble [5 x 1]> <coxph>
3 var3  <tibble [5 x 1]> <coxph>
astrofunkswag
  • 2,608
  • 12
  • 25
  • So a column in the tibble is itself a list? That sounds like something that I couldn't really work with... But what if I changed the function to only return, say, 3 values from the model? Is it possible to map that directly into 3 columns? – Ronny Efronny Aug 19 '20 at 15:55
  • Its a list of models. You don't have to store data this way, but you asked for a `tidyr` alternative to a for loop and here is one way to do it. Look into `purrr::map` and the `broom` package for how to work with them. – astrofunkswag Aug 19 '20 at 16:33
  • 1
    You could map the predictions from the 3 models into 3 columns. Look into the `add_predictions` function – astrofunkswag Aug 19 '20 at 16:38