[R] save lm output into vectors

Chuck Cleland ccleland at optonline.net
Wed Oct 10 14:48:34 CEST 2007


Henrique Dallazuanna wrote:
> Hi,
> 
> mods <- lapply(lapply(df[,which(sapply(df, is.factor))],
> function(reg)lm(df$value~reg)), summary)
> res <- lapply(mods, "[", c(4,10))
> 
> And for each model adjusted:
> 1-pf(res[[2]][[2]][1], res[[2]][[2]][2], res[[2]][[2]][3])
> 1-pf(res[[1]][[2]][1], res[[1]][[2]][2], res[[1]][[2]][3]) 

Jiong:
  Here is another approach:

mymodels <- lapply(split(iris, list(iris$Species)),
              function(x){lm(Sepal.Length ~ Sepal.Width +
                                Petal.Length +
                                Petal.Width, data = x)})

lapply(mymodels, function(x){coefficients(summary(x))})
$setosa
              Estimate Std. Error   t value     Pr(>|t|)
(Intercept)  2.3518898 0.39286751 5.9864707 3.034183e-07
Sepal.Width  0.6548350 0.09244742 7.0833236 6.834434e-09
Petal.Length 0.2375602 0.20801921 1.1420107 2.593594e-01
Petal.Width  0.2521257 0.34686362 0.7268727 4.709870e-01

$versicolor
               Estimate Std. Error   t value     Pr(>|t|)
(Intercept)   1.8955395  0.5070552  3.738329 5.112246e-04
Sepal.Width   0.3868576  0.2045449  1.891309 6.488965e-02
Petal.Length  0.9083370  0.1654325  5.490681 1.666695e-06
Petal.Width  -0.6792238  0.4353821 -1.560064 1.255990e-01

$virginica
               Estimate Std. Error    t value     Pr(>|t|)
(Intercept)   0.6998830 0.53360089  1.3116227 1.961563e-01
Sepal.Width   0.3303370 0.17432873  1.8949086 6.439972e-02
Petal.Length  0.9455356 0.09072204 10.4223360 1.074269e-13
Petal.Width  -0.1697527 0.19807243 -0.8570233 3.958750e-01

lapply(mymodels, function(x){coefficients(summary(x))[,4]})
$setosa
 (Intercept)  Sepal.Width Petal.Length  Petal.Width
3.034183e-07 6.834434e-09 2.593594e-01 4.709870e-01

$versicolor
 (Intercept)  Sepal.Width Petal.Length  Petal.Width
5.112246e-04 6.488965e-02 1.666695e-06 1.255990e-01

$virginica
 (Intercept)  Sepal.Width Petal.Length  Petal.Width
1.961563e-01 6.439972e-02 1.074269e-13 3.958750e-01

  Once you have a list of fitted models and can see how to extract parts
of the summaries, you can reorganize what you extract to meet your needs.

hope this helps,

Chuck

> On 09/10/2007, Jiong Zhang, PhD <jizhang at chori.org> wrote:
>> Hi,
>>
>> May I ask how I can save the coefficients and the p values into a table?
>>
>> thanks.
>>
>> jiong
>> The email message (and any attachments) is for the sol...{{dropped:20}}
> 
> ______________________________________________
> 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. 

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list