Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
866 views
in Technique[技术] by (71.8m points)

r - How to save summary(lm) to a file?

I'm using R for a pharmacodynamic analysis and I'm fairly new to programming.

The thing is, I'm carrying out linear regression analysis and in the future I will perform more advanced methods. Because I'm performing a large number of analysis (and I'm too lazy to manually copy paste every time I run the script), I would like to save the summaries of the analysis to a file. I've tried different methods, but nothing seems to work.

What I'm looking for is the following as (preferably) a text file:

X_Y <- lm(X ~ Y)
sum1 <- summary(X_Y)

> sum1

Call:
lm(formula = AUC_cumulative ~ LVEF)

Residuals:
    Min      1Q  Median      3Q     Max 
-910.59 -434.11  -89.17  349.39 2836.81 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) 1496.4215   396.5186   3.774 0.000268 ***
LVEF           0.8243     7.3265   0.113 0.910640    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 619.9 on 104 degrees of freedom
  (32 observations deleted due to missingness)
Multiple R-squared:  0.0001217, Adjusted R-squared:  -0.009493 
F-statistic: 0.01266 on 1 and 104 DF,  p-value: 0.9106

I've searched for methods to save summary functions to a .csv or .txt, but those files don't represent the data in a way I can understand it.

Things I've tried:

fileConn <- file("output.txt")
writeLines(sum1, fileConn)
close(fileConn)

This returns:

Error in writeLines(sum1, fileConn) : invalid 'text' argument

An attempt using the write.table command gave:

> write.table(Sum1, 'output.csv', sep=",", row.names=FALSE, col.names=TRUE, quote=FALSE)
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class ""summary.lm"" to a data.frame

Using the write command:

> write(sum1, 'output.txt')
Error in cat(list(...), file, sep, fill, labels, append) : argument 1 (type 'list') cannot be handled by 'cat'

Then I was getting closer with the following:

> write.table(sum1, 'output.csv', sep=",", row.names=FALSE, col.names=TRUE, quote=FALSE)

But this file did not have the same readable information as the printed summary

I hope someone can help, because this is way to advanced programming for me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think one option could be sink() which will output the results to a text file rather than the console. In the absence of your dataset I've used cars for an example:

sink("lm.txt")
print(summary(lm(cars$speed ~ cars$dist)))
sink()  # returns output to the console

lm.txt now looks like this:

Call:
lm(formula = cars$speed ~ cars$dist)

Residuals:
    Min      1Q  Median      3Q     Max 
-7.5293 -2.1550  0.3615  2.4377  6.4179 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  8.28391    0.87438   9.474 1.44e-12 ***
cars$dist    0.16557    0.01749   9.464 1.49e-12 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 3.156 on 48 degrees of freedom
Multiple R-squared:  0.6511,    Adjusted R-squared:  0.6438 
F-statistic: 89.57 on 1 and 48 DF,  p-value: 1.49e-12

@Roland 's suggestion of knitr is a bit more involved, but could be worth it because you can knit input, text output, and figures in to one report or html file easily.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...