[R] Export summary from regression output

Brian Diggs diggsb at ohsu.edu
Tue Oct 23 22:40:51 CEST 2012


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: r-help-bounces at r-project.org
>> [mailto:r-help-bounces at r- project.org] On Behalf Of fxen3k Sent:
>> Tuesday, October 23, 2012 1:28 PM To: r-help at r-project.org 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.
>>
>> ______________________________________________ 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.
>


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




More information about the R-help mailing list