Carmen O
Carmen O

Reputation: 23

Transfer regression output to a .cvs or .txt table

I am trying to export the output of my regression model into a .txt or .csv file. I was able to export the main information from summary() the terms (e.g., intercept), p-values etc. However, the exported file does not give me residuals standard information, R2 and F statistics, which appears at the bottom of the output.

Here is the code that I am using model = lm(cs~group + sex, data = metadata) #adjusting for sex. Output

Call: lm(formula = cs ~ group + sex, data = metadata)

Residuals: Min 1Q Median 3Q Max -16.917 -7.733 -2.506 6.934 32.494

Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 14.051 3.767 3.731 0.000555 * groupHFD -3.987 4.853 -0.822 0.415880 groupHFD +Exe 1.571 4.853 0.324 0.747817 groupHFD+Exe+Gen 4.874 4.989 0.977 0.334131 groupHFD+Gen -1.189 4.853 -0.245 0.807610 sexMale 9.633 3.104 3.103 0.003378
--- Signif. codes: 0 ‘*
’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 10.85 on 43 degrees of freedom Multiple R-squared: 0.2286, Adjusted R-squared: 0.139 F-statistic: 2.549 on 5 and 43 DF, p-value: 0.04168

#Save the regression output as table in cvs table. write.table(csregression, file = 'csregression.csv', sep = ",", row.names = F, col.names = T) The .csv file shows the following

               Estimate Std. Error t value Pr(>|t|)     (Intercept)        14.051      3.767   3.731 0.000555  groupHFD       

-3.987 4.853 -0.822 0.415880 groupHFD +Exe 1.571 4.853 0.324 0.747817 groupHFD+Exe+Gen 4.874 4.989 0.977 0.334131 groupHFD+Gen -1.189 4.853 -0.245 0.807610 sexMale 9.633 3.104 3.103 0.003378

Does someone experience the same issue? How could I export the information missing, the F-statistic and R-squared information?

Upvotes: 1

Views: 1043

Answers (2)

Daniel Hoop
Daniel Hoop

Reputation: 692

The value returned by lm is an object which can be further processed using summary. From the value returned by summary you want to access $coefficients.

mod <- lm(cs~group + sex, data = metadata)
coefs <- summary(mod)$coefficients

# Write to disk with row and colnames (col.names = NA)
write.table(coefs, file="coefficients.csv", sep = ",", col.names=NA)

Upvotes: 1

CWebkas
CWebkas

Reputation: 19

You might want to check out the broom package. You can use it for transferring regression output to csv as shown here

Upvotes: 0

Related Questions