Hi R-helpers, I have a character object called dd that has 32 elements each of which is a model formula contained within quotation marks. Here's what it looks like:> dd[1] "lm(y ~ 1,data=Cement)" "lm(y ~ X,data=Cement)" "lm(y ~ X1,data=Cement)" [4] "lm(y ~ X2,data=Cement)" "lm(y ~ X3,data=Cement)" "lm(y ~ X4,data=Cement)" [7] "lm(y ~ X + X1,data=Cement)" "lm(y ~ X + X2,data=Cement)" "lm(y ~ X + X3,data=Cement)" [10] "lm(y ~ X + X4,data=Cement)" "lm(y ~ X1 + X2,data=Cement)" "lm(y ~ X1 + X3,data=Cement)" [13] "lm(y ~ X1 + X4,data=Cement)" "lm(y ~ X2 + X3,data=Cement)" "lm(y ~ X2 + X4,data=Cement)" [16] "lm(y ~ X3 + X4,data=Cement)" "lm(y ~ X + X1 + X2,data=Cement)" "lm(y ~ X + X1 + X3,data=Cement)" [19] "lm(y ~ X + X1 + X4,data=Cement)" "lm(y ~ X + X2 + X3,data=Cement)" "lm(y ~ X + X2 + X4,data=Cement)" [22] "lm(y ~ X + X3 + X4,data=Cement)" "lm(y ~ X1 + X2 + X3,data=Cement)" "lm(y ~ X1 + X2 + X4,data=Cement)" [25] "lm(y ~ X1 + X3 + X4,data=Cement)" "lm(y ~ X2 + X3 + X4,data=Cement)" "lm(y ~ X + X1 + X2 + X3,data=Cement)" [28] "lm(y ~ X + X1 + X2 + X4,data=Cement)" "lm(y ~ X + X1 + X3 + X4,data=Cement)" "lm(y ~ X + X2 + X3 + X4,data=Cement)" [31] "lm(y ~ X1 + X2 + X3 + X4,data=Cement)" "lm(y ~ X + X1 + X2 + X3 + X4,data=Cement)" I would like to convert this object into a list called Cand.models with 32 list elements each of which would contain one of the above model formulae. When I print the list, the models should run, so the first few elements of the list would look like this (see below output from a list I created by hand). Many thanks for any help you can provide! Mark>Cand.models[[1]] Call: lm(formula = y ~ 1, data = Cement) Coefficients: (Intercept) 95.42 [[2]] Call: lm(formula = y ~ X, data = Cement) Coefficients: (Intercept) X 82.308 1.874 [[3]] Call: lm(formula = y ~ X1, data = Cement) Coefficients: (Intercept) X1 81.479 1.869
Try step() ? On Tue, Dec 14, 2010 at 10:05 PM, Mark Na <mtb954 at gmail.com> wrote:> Hi R-helpers, > > I have a character object called dd that has 32 elements each of which > is a model formula contained within quotation marks. Here's what it > looks like: > >> dd > ?[1] "lm(y ~ 1,data=Cement)" ? ? ? ? ? ? ? ? ? ? "lm(y ~ > X,data=Cement)" ? ? ? ? ? ? ? ? ? ? "lm(y ~ X1,data=Cement)" > ?[4] "lm(y ~ X2,data=Cement)" ? ? ? ? ? ? ? ? ? ?"lm(y ~ > X3,data=Cement)" ? ? ? ? ? ? ? ? ? ?"lm(y ~ X4,data=Cement)" > ?[7] "lm(y ~ X + X1,data=Cement)" ? ? ? ? ? ? ? ?"lm(y ~ X + > X2,data=Cement)" ? ? ? ? ? ? ? ?"lm(y ~ X + X3,data=Cement)" > [10] "lm(y ~ X + X4,data=Cement)" ? ? ? ? ? ? ? ?"lm(y ~ X1 + > X2,data=Cement)" ? ? ? ? ? ? ? "lm(y ~ X1 + X3,data=Cement)" > [13] "lm(y ~ X1 + X4,data=Cement)" ? ? ? ? ? ? ? "lm(y ~ X2 + > X3,data=Cement)" ? ? ? ? ? ? ? "lm(y ~ X2 + X4,data=Cement)" > [16] "lm(y ~ X3 + X4,data=Cement)" ? ? ? ? ? ? ? "lm(y ~ X + X1 + > X2,data=Cement)" ? ? ? ? ? "lm(y ~ X + X1 + X3,data=Cement)" > [19] "lm(y ~ X + X1 + X4,data=Cement)" ? ? ? ? ? "lm(y ~ X + X2 + > X3,data=Cement)" ? ? ? ? ? "lm(y ~ X + X2 + X4,data=Cement)" > [22] "lm(y ~ X + X3 + X4,data=Cement)" ? ? ? ? ? "lm(y ~ X1 + X2 + > X3,data=Cement)" ? ? ? ? ?"lm(y ~ X1 + X2 + X4,data=Cement)" > [25] "lm(y ~ X1 + X3 + X4,data=Cement)" ? ? ? ? ?"lm(y ~ X2 + X3 + > X4,data=Cement)" ? ? ? ? ?"lm(y ~ X + X1 + X2 + X3,data=Cement)" > [28] "lm(y ~ X + X1 + X2 + X4,data=Cement)" ? ? ?"lm(y ~ X + X1 + X3 + > X4,data=Cement)" ? ? ?"lm(y ~ X + X2 + X3 + X4,data=Cement)" > [31] "lm(y ~ X1 + X2 + X3 + X4,data=Cement)" ? ? "lm(y ~ X + X1 + X2 + > X3 + X4,data=Cement)" > > I would like to convert this object into a list called Cand.models > with 32 list elements each of which would contain one of the above > model formulae. When I print the list, the models should run, so the > first few elements of the list would look like this (see below output > from a list I created by hand). > > Many thanks for any help you can provide! > > Mark > > > > >>Cand.models > > [[1]] > > Call: > lm(formula = y ~ 1, data = Cement) > > Coefficients: > (Intercept) > ? ? ?95.42 > > > [[2]] > > Call: > lm(formula = y ~ X, data = Cement) > > Coefficients: > (Intercept) ? ? ? ? ? ?X > ? ? 82.308 ? ? ? ?1.874 > > > [[3]] > > Call: > lm(formula = y ~ X1, data = Cement) > > Coefficients: > (Intercept) ? ? ? ? ? X1 > ? ? 81.479 ? ? ? ?1.869 > > ______________________________________________ > 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. >
Mark - I believe lapply(dd,function(m)eval(parse(text=m))) will do what you want. - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Tue, 14 Dec 2010, Mark Na wrote:> Hi R-helpers, > > I have a character object called dd that has 32 elements each of which > is a model formula contained within quotation marks. Here's what it > looks like: > >> dd > [1] "lm(y ~ 1,data=Cement)" "lm(y ~ > X,data=Cement)" "lm(y ~ X1,data=Cement)" > [4] "lm(y ~ X2,data=Cement)" "lm(y ~ > X3,data=Cement)" "lm(y ~ X4,data=Cement)" > [7] "lm(y ~ X + X1,data=Cement)" "lm(y ~ X + > X2,data=Cement)" "lm(y ~ X + X3,data=Cement)" > [10] "lm(y ~ X + X4,data=Cement)" "lm(y ~ X1 + > X2,data=Cement)" "lm(y ~ X1 + X3,data=Cement)" > [13] "lm(y ~ X1 + X4,data=Cement)" "lm(y ~ X2 + > X3,data=Cement)" "lm(y ~ X2 + X4,data=Cement)" > [16] "lm(y ~ X3 + X4,data=Cement)" "lm(y ~ X + X1 + > X2,data=Cement)" "lm(y ~ X + X1 + X3,data=Cement)" > [19] "lm(y ~ X + X1 + X4,data=Cement)" "lm(y ~ X + X2 + > X3,data=Cement)" "lm(y ~ X + X2 + X4,data=Cement)" > [22] "lm(y ~ X + X3 + X4,data=Cement)" "lm(y ~ X1 + X2 + > X3,data=Cement)" "lm(y ~ X1 + X2 + X4,data=Cement)" > [25] "lm(y ~ X1 + X3 + X4,data=Cement)" "lm(y ~ X2 + X3 + > X4,data=Cement)" "lm(y ~ X + X1 + X2 + X3,data=Cement)" > [28] "lm(y ~ X + X1 + X2 + X4,data=Cement)" "lm(y ~ X + X1 + X3 + > X4,data=Cement)" "lm(y ~ X + X2 + X3 + X4,data=Cement)" > [31] "lm(y ~ X1 + X2 + X3 + X4,data=Cement)" "lm(y ~ X + X1 + X2 + > X3 + X4,data=Cement)" > > I would like to convert this object into a list called Cand.models > with 32 list elements each of which would contain one of the above > model formulae. When I print the list, the models should run, so the > first few elements of the list would look like this (see below output > from a list I created by hand). > > Many thanks for any help you can provide! > > Mark > > > > >> Cand.models > > [[1]] > > Call: > lm(formula = y ~ 1, data = Cement) > > Coefficients: > (Intercept) > 95.42 > > > [[2]] > > Call: > lm(formula = y ~ X, data = Cement) > > Coefficients: > (Intercept) X > 82.308 1.874 > > > [[3]] > > Call: > lm(formula = y ~ X1, data = Cement) > > Coefficients: > (Intercept) X1 > 81.479 1.869 > > ______________________________________________ > 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. >
Reasonably Related Threads
- R's AIC values differ from published values
- MuMIn package, problem using model selection table from manually created list of models
- delta AIC for models with 2 variables using MuMIn
- Tabulating Sparse Contingency Table
- delete.response leaves response in attribute dataClasses