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

Philipp Benner pbenner at uos.de
Sat Jun 16 00:59:22 CEST 2007


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



More information about the R-help mailing list