[R] QUESTION: enumerating linear models

Korbinian Strimmer korbinian.strimmer at zoology.oxford.ac.uk
Mon Sep 10 15:44:26 CEST 2001


Hi all,

how do I enumerate models in R in an efficient way?

Suppose I am interested in the fitted values of all linear
models with at most four predictors.  Can I use a loop 1:16
to do the equivalent of the following? 


m <-  matrix(ncol=length(Y),nrow=16)

m[1,] <- rep(mean(Y), length(Y)) # no predictor
m[2,] <- lm(Y ~ X1)$fitted.values # one predictor
m[3,] <- lm(Y ~ X2)$fitted.values
m[4,] <- lm(Y ~ X3)$fitted.values
m[5,] <- lm(Y ~ X4)$fitted.values
m[6,] <- lm(Y ~ X1+X2)$fitted.values # two predictors
m[7,] <- lm(Y ~ X1+X3)$fitted.values
m[8,] <- lm(Y ~ X1+X4)$fitted.values
m[9,] <- lm(Y ~ X2+X3)$fitted.values
m[10,] <- lm(Y ~ X2+X4)$fitted.values
m[11,] <- lm(Y ~ X3+X4)$fitted.values
m[12,] <- lm(Y ~ X1+X2+X3)$fitted.values # three predictors
m[13,] <- lm(Y ~ X1+X2+X4)$fitted.values
m[14,] <- lm(Y ~ X1+X3+X4)$fitted.values
m[15,] <- lm(Y ~ X2+X3+X4)$fitted.values
m[16,] <- lm(Y ~ X1+X2+X3+X4)$fitted.values # four predictors



--
Korbinian Strimmer                       http://users.ox.ac.uk/~strimmer
Dept. of Zoology, University of Oxford, South Parks Road, Oxford OX1 3PS
+44 1865 271272 (phone), -49 (fax), korbinian.strimmer at zoo.ox.ac.uk

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