library(survival)
justices <- read.csv("http://data.princeton.edu/pop509/justices2.csv")
PREDS = c("age", "year")
m = coxph(Surv(tenure, event == 1) ~ age + year, data = justices)
summary(m)
exp(coef(m)[1])
exp(confint(m,level=(1-0.05/1))[1,])
DVMOD <- function(PREDS, data){
t <- coxph(paste0("Surv(tenure, event == 1) ~ "), PREDS + number + name, data = data)
return((c(PREDS, coef(t)[1], confint(t)[1,])))
}
all_models <- lapply(PREDS,DVMOD, PREDS = PREDS, data=justices)
我希望为PREDS中的每个变量运行单独的Coxph模型,然后将其名称与危险比和置信度一起存储。
如果使用强制转换公式字符串,则as.formula()
可以在DVMOD()
函数中更改自变量,如下所示:
DVMOD <- function(PREDS, data){
theFormula <- paste("Surv(tenure, event == 1) ~ ",PREDS," + number + name")
t <- coxph(as.formula(theFormula), data = data)
return((c(PREDS, coef(t)[1], confint(t)[1,])))
}
DVMOD("age",justices)
all_models <- lapply(c("age"),function(x,y){
DVMOD(x,y)
},justices)
由于for的模型year
无法收敛(即,当DVMOD()
直接调用withyear
作为的值时失败PREDS
),lapply()
with 2变量将失败,但可用于age
。
...以及输出:
> all_models
[[1]]
age 2.5 % 97.5 %
"age" "24.4400841925434" "-20.8849057264629" "69.7650741115497"
>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句