[R] save the output of summary(lmList(x)) into a dataframe
Daniel Malter
daniel at umd.edu
Tue Jun 16 18:03:51 CEST 2009
Hi, your problem has nothing to do with your specific dataset. Further, it
is - let's say - suboptimal to push a 5 MB dataset through the mailing list.
The posting guides asks to provide an example with self-contained code; this
is what Milton has asked for. Such an example is not out of reach for your
problem (see below)...
Let's say we have 3 subjects (i in {1,2,3}). And they have y(i)=10*i+x*i+e,
where x and e and standard-normal. Then you run lmList, the results of which
are stored in what is called "reg" in this example. You can assess every
element of summary(reg) individually by specifying summary(reg)$coef[n,j,k],
where n, j, and k are index numbers. See the example below. Then you have to
create a matrix that arranges the coefficients and standard errors and what
not in the way you want. For efficiency, you would do this in a loop or
something similar.
However, given the description of the resulting matrix you desire, it is
unclear which SE, t-value, and p-value you would like at the end of each
line of your results matrix. Remember that each of the coefficients has its
own SE, t-value, and p-value. Thus, your desired results matrix seems partly
nonsensical. To just get the coefficients (not SEs, t-values, and p-values)
type "reg" in the prompt (or for your own example just type "z" and not
"summary(z)"). From there you can copy the coefficients directly into MS
Excel, for example.
i=rep(1:3,each=100)
n=100
x=rnorm(n*i,0,1)
e=rnorm(n*i,0,1)
y=10*i+x*i+e
mydata=data.frame(y,x,i)
library(nlme)
reg=lmList(y~x|i,data=mydata)
summary(reg)
summary(reg)$coef[1,1,1] #intercept 1
summary(reg)$coef[2,1,1] #intercept 2
summary(reg)$coef[3,1,1] #intercept 3
summary(reg)$coef[1,1,2] #slope 1
summary(reg)$coef[2,1,2] #slope 2
summary(reg)$coef[3,1,2] #slope 3
summary(reg)$coef[1,2,1] #SE intercept 1
summary(reg)$coef[2,2,1] #SE intercept 2
summary(reg)$coef[3,2,1] #SE intercept 3
summary(reg)$coef[1,2,2] #SE slope 1
summary(reg)$coef[1,2,2] #SE slope 2
summary(reg)$coef[1,2,2] #SE slope 3
#etc.
Best,
Daniel
-------------------------
cuncta stricte discussurus
-------------------------
-----Ursprüngliche Nachricht-----
Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im
Auftrag von Cecilia Carmo
Gesendet: Tuesday, June 16, 2009 10:33 AM
An: milton ruser
Cc: r-help at r-project.org
Betreff: Re: [R] save the output of summary(lmList(x)) into a dataframe
I'm sending the dataframe with my data and the function I have runned.
regaccdis<-read.table(file="regaccdis.txt", sep="\t",
header=TRUE)
w<- lmList(totaccz~lactivoz+varvolnegz| caedois,
na.action=na.omit,regaccdis,subset=ano==1999)
z<-summary(w)
Thank you all,
Cecília
Em Tue, 16 Jun 2009 09:56:26 -0400
milton ruser <milton.ruser at gmail.com> escreveu:
> Hi Cecilia,
>
> Could you send us a reproducible example?
>
> cheers
>
> milton
>
> On Tue, Jun 16, 2009 at 4:29 AM, Cecilia Carmo <cecilia.carmo at ua.pt>
>wrote:
>
>> Hi r-helpers!
>>
>> I need to save the output of summary() function that Ive runned like
>>this:
>> z<- lmList(y~x1+x2| x3,
>>na.action=na.omit,data1,subset=year==1999)
>> w<-summary(z)
>> The output (w) is something like this:
>> Call:
>> Model: y ~ x1 + x2 | x3
>> Data: data1
>> Coefficients:
>> (Intercept)
>> Estimate Std. Error t value Pr(>|t|)
>> 1 0.081110514 1.141352e-01 7.106531e-01 0.4774081
>> 2 0.384008008 NaN NaN NaN
>> 5 -0.127962259 NaN NaN NaN
>> 14 0.828115409 6.888789e-01 5.427381e-02 0.9567241
>>
>> x1
>> Estimate Std. Error t value Pr(>|t|)
>> 1 58.06999452 137.263534154 4.230548e-01 0.6723151
>> 2 -202.45105435 NaN NaN NaN
>> 5 NaN NaN NaN NaN
>> 14 0.12776835 0.047486161 1.262510e-02 0.9899286
>>
>> x2
>> Estimate Std. Error t value Pr(>|t|)
>> 1 -0.173986693 2.126186e-01 -8.183043e-01 4.133115e-01
>> 2 -0.630343567 NaN NaN NaN
>> 5 NaN NaN NaN NaN
>> 14 0.737871169 -3.203230e+01 3.840265e+01 0.000000e+00
Residual
>>standard error: 0.2261486 on 1522 degrees of freedom
>>
>> I would like to have this output in a dataframe like
>>this:
>> x3 intercept x1 x2 Std. Error t value Pr(>|t|)
>> 1
>> 2
>> 5
>> 14
>>
>>
>> Anyone knows if it is possible and how?
>>
>> Thanks in advance,
>>
>> Cecília Carmo (Universidade de Aveiro Portugal)
>>
>> ______________________________________________
>> 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<http://www.r-project.org/p
>>osting-guide.html> and provide commented, minimal, self-contained,
>>reproducible code.
>>
More information about the R-help
mailing list