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 <[EMAIL PROTECTED]> 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}}

______________________________________________
R-help@r-project.org 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.

Reply via email to