[R] My First Function: cryptic error message

Joel J. Adamson jadamson at PARTNERS.ORG
Mon Apr 16 23:14:11 CEST 2007


Dear List,
My first R function is a rip-off bagging algorithm from pg. 138 of
Everitt and Hothorn's "Handbook of Statistical Analyses using R"
(HSAUR).  I'm using recursive partitioning to develop a set of useful
variables in diagnosing ADHD.  

I'm running this in ESS in XEmacs 21.4.19, R 2.4.1 on Slackware Linux
11.0 with a 2.6 kernel.

This is almost an entire script, although I've removed or disguised
parts that would reveal my plans for world domination.  The function
is right here at the top:

####################
# bagging function:
bagger <- function (length = 25, formula, dataframe) {
  trees <- vector(mode = "list", length = length)
  n <- nrow(dataframe)
  bootsamples <- rmultinom (length(trees), n,
                            rep(1,n)/n)
  mod <- rpart(formula, data = dataframe,
               control = rpart.control(xval = 0))
  for (i in 1:length(trees)) {
    trees[[i]] <- update(mod,
                         weights = bootsamples[, i])
  }
}

#bring in data
[snip]
#Recursive partitioning:
library("rpart")
# get a 2/3 random sample of ids
ids <- sample(probands$id,
                   size = 2*(length(probands$id))/3,
                   replace = FALSE)
# now I want a logical vector telling me which sample to put the
# subjects in:
#training_true <- probands$id %in% ids
training <- subset(probands, probands$id %in% ids)
testing <- subset(probands, ! probands$id %in% ids)

# formula for the rpart model
adhdcbin <- factor(probands$adhdcbin, ordered = TRUE,
                   labels = cbind("Control","ADHD" ))
cbs38s <- as.formula(paste("adhdcbin ~",
                     paste(names(probands[grep("cbs38_..*",names(probands))]),
                           collapse = "+")))

# fitting the rpart model
rpart_training <- rpart(cbs38s,
                        data = training,
                        method = "class")
X11 (); plot(rpart_training);text(rpart_training)
rpart_training.bagged <- bagger(length = 100, formula = cbs38s, training)
X11 (); plot(rpart_training.bagged);text(rpart_training.bagged)
####################

The second-to-last line gives me the following error:

Error in eval(expr, envir, enclos) : object "bootsamples" not found

I've used backtrace(), debug(bagger) and options(error=recover ()) and
nothing seems to make sense.  What should I be looking for?
"bootsamples" is there at every time except ...

I've tried this with other formulas, I've tried typing in
the function vs. reading it in by source(); I've re-formatted the top
line to read with the brace on the same line as the declaration or on
the next line.  EVerything produces the same message.  Am I binding
the variables correctly?  Am I declaring the arguments correctly?

The most irritating part is that this worked last week ;)  I swear.

Thanks,
Joel
-- 
Joel J. Adamson
Biostatistician
Pediatric Psychopharmacology Research Unit
Massachusetts General Hospital
Boston, MA  02114
(617) 643-1432
(303) 880-3109





The information transmitted in this electronic communication...{{dropped}}



More information about the R-help mailing list