[R] mapply, coxph, and model formula
Christos Hatzis
christos at nuverabio.com
Thu Nov 8 22:58:39 CET 2007
Instead of mapply, use lapply (it is more appropriate in this case anyway as
you have only one list that you need to iterate over):
lapply(df.list, function(x) coxph(form, x))
-Christos
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Erik Iverson
> Sent: Thursday, November 08, 2007 4:43 PM
> To: 'r-help at stat.math.ethz.ch'
> Subject: [R] mapply, coxph, and model formula
>
> Hello -
>
> I am wanting to create some Cox PH models with coxph (in package
> survival) using different datasets.
>
> The code below illustrates my current approach and problem
> with completing this.
>
> ### BEGIN R SAMPLE CODE ##############################
> library(survival)
>
> #Define a function to make test data
> makeTestDF <- function(n) {
> times <- sample(1:200, n, replace = TRUE)
> event <- rbinom(n, 1, prob = .1)
> trt <- rep(c("A","B"), each = n/2)
> testdf <- data.frame(times,event,trt) }
>
> #Create two sets of test data of different sizes
> testdf1 <- makeTestDF(100)
> testdf2 <- makeTestDF(200)
>
> #Define a formula and call coxph
> form <- Surv(times, event) ~ trt
> coxph(form, testdf1) #Works
> coxph(form, testdf2) #Works
>
> #Create a list of the two data.frames
> df.list <- list(testdf1, testdf2)
>
> #Try to use mapply
> mapply(coxph, form, df.list)
>
> ### END R SAMPLE CODE ###############################
>
> The mapply call generates the following message:
>
> Error in terms.default(formula, special, data = data) :
> no terms component
>
> It appears from debugging that the formula argument passed
> into coxph is simply `~` in this case, with class "name"
> instead of "formula".
>
> Any ideas on how I can get this to work using this approach?
>
> Best,
> Erik Iverson
> iverson at biostat.wisc.edu
>
> > sessionInfo()
> R version 2.5.1 (2007-06-27)
> i686-pc-linux-gnu
>
> locale:
> LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLA
> TE=en_US.UTF-8;LC_MONETARY=en_US.UTF-8;LC_MESSAGES=en_US.UTF-8
> ;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC
> _MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C
>
> attached base packages:
> [1] "grid" "grDevices" "datasets" "tcltk" "splines"
> "graphics"
> [7] "utils" "stats" "methods" "base"
>
> other attached packages:
> debug mvbutils SPLOTS_1.2-6 reshape SPLOTS_1.3-22
> "1.1.0" "1.1.1" "1.2-6" "0.8.0" "1.3-22"
> Hmisc chron survival
> "3.4-2" "2.3-13" "2.32"
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
More information about the R-help
mailing list