At 10:37 PM 5/1/00 -0400, E. S. Venkatraman wrote:>I have the following problem. I have survival data (time, status) along >with several covariates (X1, X2,..., Xn). I want to fit a Cox model for >each of the covariate (univariately) and obtain the fitted probability of >survival at a fixed time point t0 and covariate value Xi0. I tried to do >this in a for loop where the index is the variable name as follows > >covnames <- names(...) >for (covi in covnames) { > survout <- coxph(Surv(time,status) ~ covi, data = dataname) > print(summary(survfit(survout,newdata),t0)) >}You could try covnames <- names(...) form <- Surv(time, status) ~ dummy for(nam in covnames) { form[[3]] <- as.name(nam) survout <- coxph(form, data = dataname) print(summary(survfit(survout, newdata), t0)) }> >Unfortunately "covi" is being treated as the variable name instead of >being evaluated and the coxph call gives an error. None of as.character, >eval, substitute (at least the way I called them) worked. How does one >pass on variable names to model formula?The above way is one. A slightly more direct way would be to use some column of the data frame on the right hand side: for (i in 1:5) { survout <- coxph(Surv(time, status) ~ dataname[, i], dataname) ..... } There is a distinction between character strings and names.> >Thanks, >Venkat >----------------------------------------------------------------------- >E. S. Venkatraman, Ph.D. Phone: (212) 639-8520 Fax: (212) 717-3137 >Assistant Attending Member Memorial Sloan-Kettering Cancer Center >----------------------------------------------------------------------- > >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.->r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html >Send "info", "help", or "[un]subscribe" >(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch >_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._> >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Tue, 2 May 2000, Bill Venables wrote:> At 10:37 PM 5/1/00 -0400, E. S. Venkatraman wrote: > >I have the following problem. I have survival data (time, status) along > >with several covariates (X1, X2,..., Xn). I want to fit a Cox model for > >each of the covariate (univariately) and obtain the fitted probability of > >survival at a fixed time point t0 and covariate value Xi0. I tried to do > >this in a for loop where the index is the variable name as follows > > > >covnames <- names(...) > >for (covi in covnames) { > > survout <- coxph(Surv(time,status) ~ covi, data = dataname) > > print(summary(survfit(survout,newdata),t0)) > >} > > You could try > > covnames <- names(...) > form <- Surv(time, status) ~ dummy > for(nam in covnames) { > form[[3]] <- as.name(nam) > survout <- coxph(form, data = dataname) > print(summary(survfit(survout, newdata), t0)) > }A related idea, which doesn't require you to know the structure of formula object, but will be slower, is covnames <- names(...) for(nam in covnames) { form<-substitute(Surv(time,status)~dummy,list(dummy=nam)) survout<-coxph(form,data=dataname) print(summary(survfit(survout, newdata), t0)) } This method also works if you want to loop over different time and status variables. -thomas Thomas Lumley Assistant Professor, Biostatistics University of Washington, Seattle -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Tue, 02 May 2000 17:11:52 +1000, Bill Venables wrote in message <3.0.32.20000502171151.0126c440 at pophost.nsw.cmis.csiro.au>:>At 10:37 PM 5/1/00 -0400, E. S. Venkatraman wrote: >>I have the following problem. I have survival data (time, status) along >>with several covariates (X1, X2,..., Xn). I want to fit a Cox model for >>each of the covariate (univariately) and obtain the fitted probability of >>survival at a fixed time point t0 and covariate value Xi0. > >You could try > >covnames <- names(...) >form <- Surv(time, status) ~ dummy >for(nam in covnames) { > form[[3]] <- as.name(nam) > survout <- coxph(form, data = dataname) > print(summary(survfit(survout, newdata), t0)) >} > >... A slightly more direct way would be to use some >column of the data frame on the right hand side: > >for (i in 1:5) { > survout <- coxph(Surv(time, status) ~ dataname[, i], dataname) >..... >} >I just discovered a nice way to do this in S-PLUS the other day; it doesn't work in R 0.99, but I don't have version 1 installed on this machine to check there. The terms() function in S-PLUS returns an expression listing the terms in a model formula. It is nice to manipulate: formula(terms(somefit)[1]) gives the model formula using just the first term, formula(terms(somefit)[-1]) gives the model formula leaving out the first term, etc. I don't know if copying this functionality would break anything (or maybe it's already there in 1.x), but it seems to me like a nice addition. Duncan Murdoch -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._