[R] Rpart and case weights: working with functions
S Ellison
S.Ellison at lgc.co.uk
Tue Jun 3 13:32:00 CEST 2008
A bit of fiddling suggests that the problem is less to do with rpart
(which will quite happily acept weights defined inside a function) and
more to do with the formula argument to the test function.
This is probably because defining the formula in the global environment
(as you implicitly do by placing it in the function call) drags the
original formula environment along with it; try
> f<-function(form) str(form)
> f(y~x)
to see what I mean.
As a work-round, try something lik
test.function <- function (formula.str, data) {
weights <- rep(.1, 8)
rpart(as.formula(formula.str), data, weights)
}
which works if formula.str is a string instead of a formula object.
Alternatively,
test.function <- function (form, data) {
weights <- rep(.1, 8)
environment(form)<-environment() #re-sets the formula environment
rpart(form, data, weights)
}
test.function(y~x,df)
### SEEMED to work - though it looks like the sort of kludge an expert
would quail at; there's
### bound to be a right way to get the formula evaluated in the current
environment.
>>> Xavier Robin <Xavier.Robin at medecine.unige.ch> 03/06/2008 10:33:06
>>>
I can't get rpart accept case weights defined inside a function.
It keeps using the copy defined in the "global" environment (if they
exists) instead of the function-defined ones.
*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}
More information about the R-help
mailing list