I have a dataset with many columns. I want to do a Cox regression for each one of the columns, so I used "apply". miRNA names are the predictor variables, the columns I want to do Cox regression in separated models. This is the code:
mirna_names <-colnames(DB)[33:54]
cox_cont = sapply(mirna_names, function(x){
formula = as.formula(paste('Surv(years, AD)~', x))
coxFit = coxph(formula, data = DB)
summary(coxFit)$coefficients[,c(2,3,5)] %>% round(3)
})
Now, I would like to test Cox Proportional Assumption using "cox.zph". I want to do it for all the miRNAs again, so I used "apply". However, this doesn't work. Any help?
cox_assump = sapply(mirna_names, function(x){
formula = as.formula(paste('coxph(Surv(years, AD)~', x))
coxFit_assump = cox.zph(formula, data = DB)
print(coxFit_assump)
})
A MWE would have helped, but the code below works. I've made as few changes to your original code as possible.
The issue had to do with the input for
cox.zph. It wants acoxphobject, which you recognized, evident in how you were reestimating the models again. However, your reestimatedcoxphhad nodataargument. (Thedataargument present in yourcox.zphline won't get passed tocoxph.) I sidestepped the issue entirely by looping over thecoxphobjects from the firstlapply.