Re: [R] model.frame: how does one use it?

From: Philipp Benner <pbenner_at_uos.de>
Date: Sat, 16 Jun 2007 00:59:22 +0200

Thanks for your explanation!

> With this in mind, either of the following might do what you want:
>
> badFunction <- function(mydata, myformula) {
> mydata$myweight <- abs(rnorm(nrow(mydata)))
> hyp <-
> rpart(myformula,
> data=mydata,
> weights=myweight,
> method="class")
> prev <- hyp
> }
>
>
> badFunction <- function(mydata, myformula) {
> myweight <- abs(rnorm(nrow(mydata)))
> environment(myformula) <- environment()
> hyp <-
> rpart(myformula,
> data=mydata,
> weights=myweight,
> method="class")
> prev <- hyp
> }

OK, this is what I have now:

adaboostBad <- function(formula, data) {   ## local definition of the weight vector (won't work because pima.formula is not defined within this function)   w <- abs(rnorm(nrow(data)))
  rpart(formula, data=data, weights=w)
}

adaboostGood <- function(formula, data) {   ## create weight vector in the data object   data$w <- abs(rnorm(nrow(data)))
  rpart(formula, data=data, weights=w)
}

adaboostBest <- function(formula, data) {   ## associate the current environment (this function's one) with the object `formula'   environment(formula) <- environment()
  w <- abs(rnorm(nrow(data)))
  rpart(formula, data=data, weights=w)
}

As far as I understand this non-standard evaluation stuff, adaboostGood() and adaboostBest() are the only two possibilities to call rpart() with weight vectors. Now suppose that I don't know what `data' contains and suppose further that it already contains a column called `w'. adaboostGood() would overwrite that column with new data which is then used as weight vector and as training data for rpart(). adaboostBest() would just use the wrong data as weight vector as it finds data$w before the real weight vector. So, in both cases I have to check for `names(data) == "w"` and stop if TRUE? Or is there a better way?

Regards

-- 
Philipp Benner

______________________________________________
R-help_at_stat.math.ethz.ch 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.
Received on Fri 15 Jun 2007 - 23:06:14 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 16 Jun 2007 - 00:31:58 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.