Hello, I would like to create a subscriptable collection (presumably a list) of lm() models. I have a data frame DX containing 6 groups of data. The general idea is (NOT RUN) ... for (i in 1:6) { DXS = subset( DX, <whatever>); LMX[ i] = lm( <formula>, data = DXS); } Now access model results by subscript ... e.g. coefficients( LMX[ 2]). Or would it be [[ 2]]? I have experimented with various schemes, attempting to "pre-allocate" a list etc. without success. Also, I assume that lm() does not make a copy of input data? That is, if I want "predict( LMX[ i])", I would have to retain a copy the associated DXS subsets? TIA, Chip Barnaby --------------------------------------------------------- Chip Barnaby cbarnaby at wrightsoft.com Vice President of Research Wrightsoft Corp. 781-862-8719 x118 voice 131 Hartwell Ave 781-861-2058 fax Lexington, MA 02421 www.wrightsoft.com
On Mon, 12 May 2008, Chip Barnaby wrote:> Hello, > > I would like to create a subscriptable collection (presumably a list) of lm() > models. > > I have a data frame DX containing 6 groups of data. The general idea is (NOT > RUN) ... > > for (i in 1:6) > { DXS = subset( DX, <whatever>); > LMX[ i] = lm( <formula>, data = DXS); > } > > Now access model results by subscript ... e.g. coefficients( LMX[ 2]). Or > would it be [[ 2]]? > > I have experimented with various schemes, attempting to "pre-allocate" a list > etc. without success.You want LMX <- list(6) ... LMX[[1]] <- lm( <formula>, data = DX, subset=<whatever>) (No trailing ';' needed, [[]] not [] for list elements.)> Also, I assume that lm() does not make a copy of input data?It may. This is controlled by the 'model' argument which defaults to TRUE (in recent versions of R: I think it once defaulted to FALSE).> That is, if I > want "predict( LMX[ i])", I would have to retain a copy the associated DXS > subsets?It does retain the fitted values: see the 'Value' section of ?lm.> > TIA, > > Chip Barnaby > > > > --------------------------------------------------------- > Chip Barnaby cbarnaby at wrightsoft.com > Vice President of Research > Wrightsoft Corp. 781-862-8719 x118 voice > 131 Hartwell Ave 781-861-2058 fax > Lexington, MA 02421 www.wrightsoft.com > > ______________________________________________ > 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
try something like the following: x <- runif(1000, -3, 3) y <- 2 + 3 * x + rnorm(1000) f <- gl(10, 100) lm.lis <- vector("list", 10) for (i in 1:10) { lm.lis[[i]] <- lm(y ~ x, subset = f == as.character(i)) } sapply(lm.lis, coef) sapply(lm.lis, fitted) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm Quoting Chip Barnaby <cbarnaby at wrightsoft.com>:> Hello, > > I would like to create a subscriptable collection (presumably a list) > of lm() models. > > I have a data frame DX containing 6 groups of data. The general idea > is (NOT RUN) ... > > for (i in 1:6) > { DXS = subset( DX, <whatever>); > LMX[ i] = lm( <formula>, data = DXS); > } > > Now access model results by subscript ... e.g. coefficients( LMX[ 2]). > Or would it be [[ 2]]? > > I have experimented with various schemes, attempting to "pre-allocate" > a list etc. without success. > > Also, I assume that lm() does not make a copy of input data? That is, > if I want "predict( LMX[ i])", I would have to retain a copy the > associated DXS subsets? > > TIA, > > Chip Barnaby > > > > --------------------------------------------------------- > Chip Barnaby cbarnaby at wrightsoft.com > Vice President of Research > Wrightsoft Corp. 781-862-8719 x118 voice > 131 Hartwell Ave 781-861-2058 fax > Lexington, MA 02421 www.wrightsoft.com > > ______________________________________________ > 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.Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm