[R] Question on passing in parameter to Cox hazard
David Winsemius
dwinsemius at comcast.net
Tue Mar 9 22:35:30 CET 2010
On Mar 9, 2010, at 4:19 PM, Tim Smith wrote:
> Hi,
>
> I wanted to do the cox model using a matrix. The following lines
> illustrate what I want to do:
> --------------------------------
> dat <- matrix(rnorm(30), ncol=3,dimnames = list(1:10,letters[1:3]))
> Survival <- rexp(10)
> Status <- ifelse(runif(10) < .7, 1, 0)
> mat <- as.data.frame(cbind(dat,Survival,Status))
>
> cmod <- coxph(, mat)
> ---------------------------------
> This works fine. However, I need to change the code so that the
> column headers ( a+b+c )are passed into the coxph function on the
> fly. What string/object do I need to generate so the function works?
> I am trying:
>
> # For example
> chead <- "a+b+c"
?as.formula
Perhaps:
as.formula(paste("Surv(Survival, Status) ~", chead))
Formula arguments are not just character strings. They have additional
attributes.
> cmod <- coxph(as.formula(paste("Surv(Survival, Status) ~", chead)),
mat)
> cmod
Call:
coxph(formula = as.formula(paste("Surv(Survival, Status) ~",
chead)), data = mat)
coef exp(coef) se(coef) z p
a -0.286 0.751 0.663 -0.431 0.67
b 0.161 1.174 0.705 0.228 0.82
c -0.330 0.719 1.307 -0.252 0.80
Likelihood ratio test=0.62 on 3 df, p=0.893 n= 10
> cmod <- coxph(Surv(Survival, Status) ~ chead, mat)
> but this gives an error since I'm passing in a string. Can I change
> chead to something so that the code works?
> many thanks.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list