Export summary from regression output

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Export summary from regression output

fxen3k
Hi there,

I tried it many times but didn't get it worked.

I just want to export the summary of a OLS regression (lm() function) into a csv-file including the "call"-formula", "coefficients", "r-squared", " adjusted r-squared"  and "f statistic".

I know I can export:
write.csv2(Regression_60d_ann$coefficients, "Regression_60d_ann.csv")
But then I only get the coefficients, but not all the other output...

I tried creating a matrix and I wanted to put in Regression_60d_ann$coefficients, Regression_60d_ann$adj.r.squared, Regression_60d_ann$r.squared, etc. but it didn't work due to different length of rows.


Can anyone help or has a better solution?

Thanks in advance
Felix
Reply | Threaded
Open this post in threaded view
|

Re: Export summary from regression output

PIKAL Petr
Hi

section Arguments of write.table help page clearly says

x the object to be written, preferably a matrix or data frame. If not, it is attempted to coerce x to a data frame.

summary object from lm is highly structured list an AFAIK can not be easily coerced to data frame. So either copy console output to some word processing software by Ctrl-C Ctrl-V (in Windows environment) or dissect summary output to pieces which can be coerced to data frames and save it one by one by any write.* function.

Regards
Petr


> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of fxen3k
> Sent: Tuesday, October 23, 2012 1:28 PM
> To: [hidden email]
> Subject: [R] Export summary from regression output
>
> Hi there,
>
> I tried it many times but didn't get it worked.
>
> I just want to export the summary of a OLS regression (lm() function)
> into a csv-file including the "call"-formula", "coefficients", "r-
> squared", "
> adjusted r-squared"  and "f statistic".
>
> I know I can export:
> write.csv2(Regression_60d_ann$coefficients, "Regression_60d_ann.csv")
> But then I only get the coefficients, but not all the other output...
>
> I tried creating a matrix and I wanted to put in
> Regression_60d_ann$coefficients, Regression_60d_ann$adj.r.squared,
> Regression_60d_ann$r.squared, etc. but it didn't work due to different
> length of rows.
>
>
> Can anyone help or has a better solution?
>
> Thanks in advance
> Felix
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Export-
> summary-from-regression-output-tp4647109.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Export summary from regression output

Brian Diggs
On 10/23/2012 5:59 AM, PIKAL Petr wrote:

> Hi
>
> section Arguments of write.table help page clearly says
>
> x the object to be written, preferably a matrix or data frame. If
> not, it is attempted to coerce x to a data frame.
>
> summary object from lm is highly structured list an AFAIK can not be
> easily coerced to data frame. So either copy console output to some
> word processing software by Ctrl-C Ctrl-V (in Windows environment) or
> dissect summary output to pieces which can be coerced to data frames
> and save it one by one by any write.* function.

Alternatively, if you don't care about the tabular nature of the
coefficients table, you can just do:

mdl <- lm(Sepal.Width ~ Sepal.Length, data=iris)
write.table(capture.output(print(mdl)), file="")

That is, use capture.output on the print method and write that out. For
this example, the file would look like

"x"
"1" ""
"2" "Call:"
"3" "lm(formula = Sepal.Width ~ Sepal.Length, data = iris)"
"4" ""
"5" "Coefficients:"
"6" " (Intercept)  Sepal.Length  "
"7" "     3.41895      -0.06188  "
"8" ""



> Regards Petr
>
>
>> -----Original Message----- From: [hidden email]
>> [mailto:r-help-bounces@r- project.org] On Behalf Of fxen3k Sent:
>> Tuesday, October 23, 2012 1:28 PM To: [hidden email] Subject:
>> [R] Export summary from regression output
>>
>> Hi there,
>>
>> I tried it many times but didn't get it worked.
>>
>> I just want to export the summary of a OLS regression (lm()
>> function) into a csv-file including the "call"-formula",
>> "coefficients", "r- squared", " adjusted r-squared"  and "f
>> statistic".
>>
>> I know I can export: write.csv2(Regression_60d_ann$coefficients,
>> "Regression_60d_ann.csv") But then I only get the coefficients, but
>> not all the other output...
>>
>> I tried creating a matrix and I wanted to put in
>> Regression_60d_ann$coefficients, Regression_60d_ann$adj.r.squared,
>> Regression_60d_ann$r.squared, etc. but it didn't work due to
>> different length of rows.
>>
>>
>> Can anyone help or has a better solution?
>>
>> Thanks in advance Felix
>>
>>
>>
>> -- View this message in context:
>> http://r.789695.n4.nabble.com/Export-
>> summary-from-regression-output-tp4647109.html Sent from the R help
>> mailing list archive at Nabble.com.
>>
>> ______________________________________________ [hidden email]
>> 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.
>


--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Export summary from regression output

arun kirshna
In reply to this post by fxen3k
HI,
May be this helps:
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
      trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
      group <- gl(2,10,20, labels=c("Ctl","Trt"))
      weight <- c(ctl, trt)
      lm.D9 <- lm(weight ~ group)



fun1<-function(x){
     res<-c(paste(as.character(summary(x)$call),collapse=" "),
     x$coefficients[1],
     x$coefficients[2],
     length(x$model),
     summary(x)$coefficients[2,2],
     summary(x)$r.squared,
     summary(x)$adj.r.squared,
     summary(x)$fstatistic,
     pf(summary(x)$fstatistic[1],summary(x)$fstatistic[2],summary(x)$fstatistic[3],lower.tail=FALSE))
     names(res)<-c("call","intercept","slope","n","slope.SE","r.squared","Adj. r.squared",
     "F-statistic","numdf","dendf","p.value")
     return(res)} 
res2<-fun1(lm.D9)

write.csv(res2,"newregsummary.csv")
 

 x
call lm weight ~ group
intercept 5.032
slope -0.371
n 2
slope.SE 0.3114348514
r.squared 0.073077599
Adj. r.squared 0.02158191
F-statistic 1.4191012974
numdf 1
dendf 18
p.value 0.249023166

A.K.




----- Original Message -----
From: fxen3k <[hidden email]>
To: [hidden email]
Cc:
Sent: Tuesday, October 23, 2012 7:28 AM
Subject: [R] Export summary from regression output

Hi there,

I tried it many times but didn't get it worked.

I just want to export the summary of a OLS regression (lm() function) into a
csv-file including the "call"-formula", "coefficients", "r-squared", "
adjusted r-squared"  and "f statistic".

I know I can export:
write.csv2(Regression_60d_ann$coefficients, "Regression_60d_ann.csv")
But then I only get the coefficients, but not all the other output...

I tried creating a matrix and I wanted to put in
Regression_60d_ann$coefficients, Regression_60d_ann$adj.r.squared,
Regression_60d_ann$r.squared, etc. but it didn't work due to different
length of rows.


Can anyone help or has a better solution?

Thanks in advance
Felix



--
View this message in context: http://r.789695.n4.nabble.com/Export-summary-from-regression-output-tp4647109.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.