-2

So, in the R Console I run a regression and then print the coefficients with:

summary(lm1)$coefficients

The result is nice and pretty. It gets messy when I copy and paste but it looks good. The name of each regressor is listed on a separate row and then coefficients follow, each in a nice column with a column header at the top.

In a script that I run in terminal (on my macbook, btw) I use this command:

dput(summary(lm1)$coefficients)

The result (I'm only going to paste part of it) is

>structure(c(-0.0206181857293946, -0.0017031360313225, 0.000315645616785813,
>0.000599803702896499, 0.000329152314647127, -0.000359972132038397,
>0.0069812874814682, -0.000468798197004485, 0.000260098693454015,
>
>...
>
>0.0292457238789278, 8.66406612930307e-11, 0.316665800834179,
>0.0207690630122154, 0.0080390491577215, 0.0141105679984487, >7.13568902235773e-06
>), .Dim = c(41L, 4L), .Dimnames = list(c("RatiotoSPY", >"fiveDayRatioStdDev",
>"RANKfiveDayRatioStdDev", "slopeFiveDayRatioStdDev", >"RANKslopeFiveDayRatioStdDev",
>
>....

So, a few things:

  1. The numbers themselves that are printed by the script do not match the numbers printed by the console. Frankly, I'm not sure what the script is printing.
  2. Some rows have three numbers separated by commas whereas other rows have four. The console has 4: estimate, std.error, t value, Pr(>|t|). Again, the script isn't printing the same values as the console at all.
  3. There is no label for the rows, it seems like the row labels are all aggregated into a list at the bottom.

Is there an easy way to print out the regression coefficients and statistics nicely?

mfitzp
  • 15,275
  • 7
  • 50
  • 70
DanS.
  • 13
  • 4
  • Nothing here is [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so we have no idea what your script is printing and what numbers aren't matching up. Everyone has different definitions of what "printing nicely" might mean so you need to be very explicit about what you need. – MrFlick Feb 23 '15 at 21:57
  • Show us the actual output that's showing discrepancies. Your dput result is messed up and not usable. Could the difference in printing be attributed to rounding? – Roman Luštrik Feb 24 '15 at 09:20
  • Sorry for disappearing. So, I want total control over the output. The end result should be a comma delimited file with the dependent variable name, variable estimate, std. error, t-value, P value. I know that in the R console I can do summary(lm1)$coefficients and SORT OF get this but in the script ... well you can see what it prints. – DanS. Mar 09 '15 at 15:22

1 Answers1

1

You need to check out the incredible broom package.

install.packages("broom")
lmfit <- lm(mpg ~ wt, mtcars)
lmfit

## 
## Call:
## lm(formula = mpg ~ wt, data = mtcars)
## 
## Coefficients:
## (Intercept)           wt  
##       37.29        -5.34

library(broom)
tidy(lmfit)

##          term estimate std.error statistic   p.value
## 1 (Intercept)   37.285   1.8776    19.858 8.242e-19
## 2          wt   -5.344   0.5591    -9.559 1.294e-10

If you need the output from summary(lmfit), use glance().

glance(lmfit)

##   r.squared adj.r.squared    sigma statistic      p.value df    logLik      AIC      BIC deviance df.residual
## 1 0.7528328     0.7445939 3.045882  91.37533 1.293959e-10  2 -80.01471 166.0294 170.4266 278.3219          30

Additional information available via the vignettes: browseVignettes(package="broom")

JasonAizkalns
  • 20,243
  • 8
  • 57
  • 116
  • Unfortunately, the output on the script is still funky. I'm getting totally different numbers and tidy() still prints the output in an awkward way :-/ hmm..... – DanS. Feb 23 '15 at 21:40