[R] More on for() Loop...
ripley@stats.ox.ac.uk
ripley at stats.ox.ac.uk
Sat Jun 8 14:04:37 CEST 2002
On Sat, 8 Jun 2002, Frank E Harrell Jr wrote:
> On Sat, 8 Jun 2002 22:44:07 +1200 (NZST)
> Ko-Kang Kevin Wang <kwan022 at stat.auckland.ac.nz> wrote:
>
> > Hi,
> >
> > Say I want to do something like fitting 10 different sized trees with
> > rpart() function. The only modification I need to do is to set 10
> > different cp's, which I have in a vector called foo.
> >
> > Can I do something like:
> >
> > for(i in 1:10) {
> > rpart(y ~ ., cp = foo[i], data = mydata)
> > }
> >
> > My problem is, I wish to save the 10 rpart objects into 10 different
> > names, my.rpart1 ~ my.rpart10, for example. But I'm not sure how to do
> > this...
> >
>
> fits <- vector('list',10)
> names(fits) <- format(foo) # optional - will label list components
> for(i in 1:10) fits[[i]] <- rpart(...)
>
> Fetch the ith fit by using fits[[i]] (or fits[['formatted cp value']]).
Two refinements:
1) Use substitute() to get the call information in the fit as a value
and not fun[i]. The Programmer's Niche in the next R-news is all about
that, or see `S Programming'.
2) You only need to call prune.rpart to change the amount of pruning
in an rpart fit. Even if you didn't, I would advise setting the random
seed so you got the same cross-validation partition both times.
--
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 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list