[R] How to generate table output of a series of lm's

Daniel Dunn daniel at dunncapital.com
Tue Feb 5 15:54:26 CET 2008


I have a related question.  Suppose I generate a series of linear models

modco=list()
modco[[length(modco)+1]]=lm(normskvop ~ I(nts^0.5)-1, data = colo,
weights=wtz)
modco[[length(modco)+1]]=lm(normskvop ~ I(nts^0.5)-1, data = colo,
weights=wtz, subset=sector!="X")
modco[[length(modco)+1]]=lm(normskvop ~ I(nts^0.5)-1, data = colo,
weights=wtz, subset=sector!="A")
modco[[length(modco)+1]]=lm(normskvop ~ I(nts^0.5)-1, data = colo,
weights=wtz, subset=sector=="A")
modco[[length(modco)+1]]=lm(normskvop ~ I(nts^0.5)-1, data = colo,
weights=wtz, subset=sector=="M")

I would like to have a table of the coefficients (including the t value &
the Pr(>|t|) value) of the five models akin to Peter Dalgaard's elegant
"sapply(t,unlist)" solution below.

For reference, here is the summary of modco[[1]]

summary(modco[[1]])

Call:
lm(formula = normskvop ~ I(nts^0.5) - 1, data = colo, weights = wtz)

Residuals:
      Min        1Q    Median        3Q       Max 
-525.3071   -1.3386    0.2369    1.7981  594.7388 

Coefficients:
           Estimate Std. Error t value Pr(>|t|)    
I(nts^0.5) -0.58329    0.08453  -6.901  6.2e-12 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 34.04 on 3232 degrees of freedom
Multiple R-Squared: 0.01452,    Adjusted R-squared: 0.01422 
F-statistic: 47.62 on 1 and 3232 DF,  p-value: 6.199e-12

Thanks in advance,

Daniel Dunn, M.D., Ph.D.
309 SE Osceola St. #350
Stuart, FL 34994

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Peter Dalgaard
Sent: Tuesday, February 05, 2008 4:59 AM
To: Uwe Ligges
Cc: r-help
Subject: Re: [R] How to generate table output of t-test

Uwe Ligges wrote:
> Ng Stanley wrote:
>   
>> Hi,
>>
>> Given
>>
>> test <- matrix(c(1, 1,2,2), 2,2)
>> t <- apply(test, 1, t.test)
>>     
>  >
>   
>> How can I obtain a table of p-values, confidence interval etc, 
>> instead of
>>     
>
> A quick hack would be:
>
> m <- t(sapply(t, function(x)  c(x[["p.value"]], x[["conf.int"]])))
> colnames(m) <- c("p", "lwr", "upr")
> m
>
>   
Even quicker:

> sapply(t,unlist)
                   [,1]                [,2]
statistic.t        "3"                 "3"
parameter.df       "1"                 "1"
p.value            "0.204832764699133" "0.204832764699133"
conf.int1          "-4.85310236808735" "-4.85310236808735"
conf.int2          "7.85310236808735"  "7.85310236808735"
estimate.mean of x "1.5"               "1.5"
null.value.mean    "0"                 "0"
alternative        "two.sided"         "two.sided"
method             "One Sample t-test" "One Sample t-test"
data.name          "newX[, i]"         "newX[, i]"

Or maybe

> sapply(lapply(t,"[",1:6),unlist)
                         [,1]       [,2]
statistic.t         3.0000000  3.0000000
parameter.df        1.0000000  1.0000000
p.value             0.2048328  0.2048328
conf.int1          -4.8531024 -4.8531024
conf.int2           7.8531024  7.8531024
estimate.mean of x  1.5000000  1.5000000
null.value.mean     0.0000000  0.0000000

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907

______________________________________________
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