Why I can't output table from function

Hello, Dear all:

I am running multiple cox hazard models for 8 different outcomes. In the end, I only want to get 8 result tables with beta estimate, HR ratio with CI and P value for each variable in the model. I follow the online codes and I almost can see it but I can't output from function. When I type "res", the error will say there is no such object. I know I can't output data.frame. but don't know how to make it right....
thanks a lot!
#In the formula dataset, there are 8 formula listed there.
models_res <- lapply(mod_formula, function(x){coxph(x, data = df_mod1)})

results <- lapply(models_res,
function(x){
x0 <- summary(x);
pval<-signif(x0$coef[,5], digits=2);
beta<-signif(x0$coef[,1], digits=2);#coeficient beta
HR <-signif(x0$coef[,2], digits=3);#exp(beta)
HR.confint.lower <- signif(x0$conf.int[,"lower .95"],3)
HR.confint.upper <- signif(x0$conf.int[,"upper .95"],3)
HR1 <- paste0(HR, " (",
HR.confint.lower, "-", HR.confint.upper, ")")
res<-data.frame(beta,HR1,pval)
names(res)<-c("beta", "HR (95% CI for HR)", "p.value")
print(res)
return(res)
})
/**************/
beta HR (95% CI for HR) p.value
age 0.059 1.06 (1.06-1.06) 7.1e-266
sexMALE 0.370 1.45 (1.35-1.56) 1.0e-25
eGFRcateeGFRcate2:-5<=-3 -0.470 0.628 (0.554-0.711) 2.4e-13
eGFRcateeGFRcate3:-3<=-1 -0.640 0.527 (0.471-0.588) 6.8e-30
eGFRcateeGFRcate4:1<=3 -0.600 0.548 (0.482-0.622) 2.2e-20
eGFRcateeGFRcate5:3+ 0.120 1.13 (1.02-1.24) 1.6e-02
eGFRcateRef -0.730 0.483 (0.432-0.54) 1.7e-37

You've assigned the output to an object called results. That is the object that has the output of your code. If you type results in the console, do you see the output you expected? res only exists inside the lapply call and goes away once lapply finishes.

1 Like

you are totally right. It is actually in results. :slight_smile:

thanks a lot!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.