Skip to content

All combinations possible in a mutliple regression

6 messages · Philippe Massicotte, arun, Greg Snow +1 more

#
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.
#
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.
#
HI,

I forgot about the AIC.
?resAIC<-list()
for(i in 1:length(res1)){
?resAIC[[i]]<-list()
?resAIC[[i]]<-AIC(res1[[i]])
?}
?unlist(resAIC)
# [1] 71.25981 65.22991 71.32024 71.29489 67.20616 73.15101 73.13823 66.17742
?#[9] 66.96219 73.27309 67.78183 68.85621 75.03196 68.00660 69.39852

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.
2 days later
#
Look at the leaps function in the leaps package.  It will compute the
Cp statistic which is a function of AIC.
On Thu, Aug 9, 2012 at 7:28 AM, zel7223 <jmichel.fortin at hotmail.fr> wrote:

  
    
2 days later
#
Leaps works :)

Thanks a lot!

JMF



-----
Jean-Michel Fortin
?tudiant au premier cycle en Biologie/ Undergraduate in Biology
Lab Currie Universit? d'Ottawa/  Currie Lab University of Ottawa
--
View this message in context: http://r.789695.n4.nabble.com/All-combinations-possible-in-a-mutliple-regression-tp4639762p4640295.html
Sent from the R help mailing list archive at Nabble.com.