Suppose I have the following data: y<-rnorm(10) age<-rnorm(10) sex<-rbinom(10,1, 0.5) edu<-round(runif(10, 1, 20)) edu2<-edu^2 df<-data.frame(y,age,sex,edu,edu2) I want to run a large number of models, for example: lm(y~age) lm(y~age+sex) lm(y~age+sex+edu) lm(y~age+sex+edu+edu2) lm(y~sex+edu2) lm(y~age+edu+edu2) .... But I would like to first define a list containing all possible sets of regressors, and then execute each one in a loop/lapply. Unfortunately I got lost in trying to paste variables' name in the formula with no result. many thanks in advance. paolo -- Paolo Brunori Ricercatore in Economia Politica & Life Course Centre Fellow Dipartimento di Scienze Economiche - Universit? di Bari www.uniba.it/docenti/brunori-paolo www.equalchances.org www.lifecoursecentre.org.au
It's hard to imagine a situation where this makes sense, but of course you can do it if you want. Perhaps rhs <- unlist(sapply(1:(ncol(df)-1), function(x) apply(combn(names(df)[-1], x), 2, paste, collapse = " + "))) lapply(rhs, function(x) lm(as.formula(paste("y ~", x)), data = df)) --Ista On Fri, Nov 4, 2016 at 12:53 PM, paolo brunori <paolo.brunori at uniba.it> wrote:> Suppose I have the following data: > > y<-rnorm(10) > age<-rnorm(10) > sex<-rbinom(10,1, 0.5) > edu<-round(runif(10, 1, 20)) > edu2<-edu^2 > > df<-data.frame(y,age,sex,edu,edu2) > > I want to run a large number of models, for example: > > lm(y~age) > lm(y~age+sex) > lm(y~age+sex+edu) > lm(y~age+sex+edu+edu2) > lm(y~sex+edu2) > lm(y~age+edu+edu2) > .... > > But I would like to first define a list containing all possible sets of > regressors, and then execute each one in a loop/lapply. Unfortunately I got > lost in trying to paste variables' name in the formula with no result. > > many thanks in advance. > > paolo > > > > -- > Paolo Brunori > Ricercatore in Economia Politica & Life Course Centre Fellow > Dipartimento di Scienze Economiche - Universit? di Bari > www.uniba.it/docenti/brunori-paolo > www.equalchances.org > www.lifecoursecentre.org.au > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
1. Statistically, you probably don't want to do this at all (but that's another story). 2. Programatically, you probably want to use one of several packages that do it already rather than trying to reinvent the wheel. A quick search on rseek.org for "all subsets regression" brought up this: http://rpubs.com/kaz_yos/all-subset Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Fri, Nov 4, 2016 at 9:53 AM, paolo brunori <paolo.brunori at uniba.it> wrote:> Suppose I have the following data: > > y<-rnorm(10) > age<-rnorm(10) > sex<-rbinom(10,1, 0.5) > edu<-round(runif(10, 1, 20)) > edu2<-edu^2 > > df<-data.frame(y,age,sex,edu,edu2) > > I want to run a large number of models, for example: > > lm(y~age) > lm(y~age+sex) > lm(y~age+sex+edu) > lm(y~age+sex+edu+edu2) > lm(y~sex+edu2) > lm(y~age+edu+edu2) > .... > > But I would like to first define a list containing all possible sets of > regressors, and then execute each one in a loop/lapply. Unfortunately I got > lost in trying to paste variables' name in the formula with no result. > > many thanks in advance. > > paolo > > > > -- > Paolo Brunori > Ricercatore in Economia Politica & Life Course Centre Fellow > Dipartimento di Scienze Economiche - Universit? di Bari > www.uniba.it/docenti/brunori-paolo > www.equalchances.org > www.lifecoursecentre.org.au > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.