[R] model.frame: how does one use it?
Dirk Eddelbuettel
edd at debian.org
Fri Jun 15 17:47:19 CEST 2007
Philipp Benner reported a Debian bug report against r-cran-rpart aka rpart.
In short, the issue has to do with how rpart evaluates a formula and
supporting arguments, in particular 'weights'.
A simple contrived example is
-----------------------------------------------------------------------------
library(rpart)
## using data from help(rpart), set up simple example
myformula <- formula(Kyphosis ~ Age + Number + Start)
mydata <- kyphosis
myweight <- abs(rnorm(nrow(mydata)))
goodFunction <- function(mydata, myformula, myweight) {
hyp <- rpart(myformula, data=mydata, weights=myweight, method="class")
prev <- hyp
}
goodFunction(mydata, myformula, myweight)
cat("Ok\n")
## now remove myweight and try to compute it inside a function
rm(myweight)
badFunction <- function(mydata, myformula) {
myweight <- abs(rnorm(nrow(mydata)))
mf <- model.frame(myformula, mydata, myweight)
print(head(df))
hyp <- rpart(myformula,
data=mf,
weights=myweight,
method="class")
prev <- hyp
}
badFunction(mydata, myformula)
cat("Done\n")
-----------------------------------------------------------------------------
Here goodFunction works, but only because myweight (with useless random
weights, but that is not the point here) is found from the calling
environment.
badFunction fails after we remove myweight from there:
:~> cat /tmp/philipp.R | R --slave
Ok
Error in eval(expr, envir, enclos) : object "myweight" not found
Execution halted
:~>
As I was able to replicate it, I reported this to the package maintainer. It
turns out that seemingly all is well as this is supposed to work this way,
and I got a friendly pointer to study model.frame and its help page.
Now I am stuck as I can't make sense of model.frame -- see badFunction
above. I would greatly appreciate any help in making rpart work with a local
argument weights so that I can tell Philipp that there is no bug. :)
Regards, Dirk
--
Hell, there are no rules here - we're trying to accomplish something.
-- Thomas A. Edison
More information about the R-help
mailing list