[R] All combinations possible in a mutliple regression

arun smartpink111 at yahoo.com
Thu Aug 9 18:56:23 CEST 2012


HI,

I hope this helps you,

set.seed(1)
 dat1<-data.frame(X1=rnorm(25,15),X2=rnorm(25,5),X3=runif(25,0.4),X4=rnorm(25,12),Y=rnorm(25,35))
 ColNam<-names(dat1)
 ColNam
#[1] "X1" "X2" "X3" "X4" "Y" 

 ColNam<-ColNam[!ColNam %in% "Y"]
 n<-length(ColNam)
 ColNam
#[1] "X1" "X2" "X3" "X4"

 id1<-unlist(lapply(1:n,function(x)combn(1:n,x,simplify=F)),recursive=F)
f1<-lapply(id1,function(x)
 paste("Y~",paste(ColNam[x],collapse="+")))
res1<- lapply(f1,function(x) lm(as.formula(x),data=dat1))
 summary(res1[[1]])

Call:
lm(formula = as.formula(x), data = dat1)

Residuals:
     Min       1Q   Median       3Q      Max 
-1.46344 -0.78252 -0.08563  0.68324  1.49788 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 34.26057    3.03804  11.277 7.56e-11 ***
X1           0.05828    0.19991   0.292    0.773    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 0.9305 on 23 degrees of freedom
Multiple R-squared: 0.003682,    Adjusted R-squared: -0.03964 
F-statistic: 0.085 on 1 and 23 DF,  p-value: 0.7732 

A.K.



----- Original Message -----
From: zel7223 <jmichel.fortin at hotmail.fr>
To: r-help at r-project.org
Cc: 
Sent: Thursday, August 9, 2012 9:28 AM
Subject: [R] All combinations possible in a mutliple regression

Hi,

I want to use four independent variables to predict the output of one
dependent variable using a linear model lm. I want to compare all possible
combinations of the 4 independent variables, including singles, pairs and
triples.

I was thinking of using the AIC test to compare all models and pick the best
one.

The model looks like this : 

lm(Y ~ X1 + X2 + X3 + X4)

Thanks for your help

Cheers

Jean-Michel Fortin
UOttawa



--
View this message in context: http://r.789695.n4.nabble.com/All-combinations-possible-in-a-mutliple-regression-tp4639762.html
Sent from the R help mailing list archive at Nabble.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.




More information about the R-help mailing list