[R] How to bind models into a list of models?

Phil Spector spector at stat.berkeley.edu
Tue Dec 14 22:45:54 CET 2010


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



More information about the R-help mailing list