[R] Rpart and case weights: working with functions

From: Xavier Robin <Xavier.Robin_at_medecine.unige.ch>
Date: Tue, 03 Jun 2008 11:33:06 +0200

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.

Here is what I do:

test.function <- function (formula, data) {

   weights <- rep(.1, 100)
   rpart(formula, data, weights)

test.function(x~y, data)

And I get an error:
> Error in model.frame(formula, rownames, variables, varnames, extras, extranames, :
> invalid type (closure) for variable '(weights)'

If I define weights in the global environment, it uses this instead of the function-defined one (which can be verified by .Last.value$frame$wt[1]: it should equal sum(weights) and actually equals the sum of global weights, not the function one).

I tried to pass a named argument (weights=weights), using other names (w, foo, ...), or both, but still it doesn't work. I found nothing in rpart doc about that behaviour...
It was tested on R 2.7.0 (Windows XP) and R 2.5.1 (Linux kernel 2.6.9).

For the moment, the only workaround I found is to use/modify the global weights using <<- (not very satisfying).

What did I wrong? And what should I do to get it work (i.e. using the copy of weights defined inside the function)?

Thanks in advance for your help.

Xavier Robin

Xavier Robin

Biomedical Proteomics Research Group (BPRG)
Department of Structural Biology and Bioinformatics (DBSB)
Geneva University Medical Center (CMU)
1, rue Michel Servet - CH-1211 Genève 4 - Switzerland
Tel: (+41 22) 379 53 21

R-help_at_r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Tue 03 Jun 2008 - 10:13:52 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 Tue 03 Jun 2008 - 14:30:38 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.

list of date sections of archive