Re: [R] Rpart and case weights: working with functions

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Tue, 03 Jun 2008 11:21:56 +0100 (BST)

'weights' should be a column in your 'data': that is the standard way to specify weights to a model-fitting function.

On Tue, 3 Jun 2008, Xavier Robin wrote:

> 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
> 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.
>

-- 
Brian D. Ripley,                  ripley_at_stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


______________________________________________ R-help_at_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.

Received on Tue 03 Jun 2008 - 10:50:53 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 - 12:30:37 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