[R] mapply, coxph, and model formula

Erik Iverson iverson at biostat.wisc.edu
Thu Nov 8 22:43:13 CET 2007


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_COLLATE=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"



More information about the R-help mailing list