Rapporter / pander

An R Pandoc Writer: Convert arbitrary R objects into markdown
http://rapporter.github.io/pander/
Open Software License 3.0
294 stars 66 forks source link

pander.lm() and pander.summary.lm(): Reordering of coefficients #232

Closed krlmlr closed 8 years ago

krlmlr commented 8 years ago

The coefficient that appears first in the output of summary.lm() seems to appear last in the output of pander::pander.lm(). It looks like this is by design, but this makes models without intercept look very awkward.

> lm(Petal.Width~Petal.Length+Sepal.Width+Species-1, iris) %>% pander::pander

--------------------------------------------------------------------
                    Estimate   Std. Error   t value   Pr(>|t|) 
----------------------- ---------- ------------ --------- ----------
    **Sepal.Width**       0.202       0.0442      4.571    1.03e-05 

   **Speciessetosa**     -0.6953      0.1424     -4.882   2.739e-06 

 **Speciesversicolor**   0.04165      0.1546     0.2695     0.7879  

 **Speciesvirginica**     0.4806      0.1901      2.529    0.01251  

   **Petal.Length**       0.1701     0.03489      4.876   2.808e-06 
--------------------------------------------------------------------

Table: Fitting linear model: Petal.Width ~ Petal.Length + Sepal.Width + Species - 1

> lm(Petal.Width~Petal.Length+Sepal.Width+Species-1, iris) %>% summary

Call:
lm(formula = Petal.Width ~ Petal.Length + Sepal.Width + Species - 
    1, data = iris)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.55861 -0.08486 -0.00190  0.09073  0.48605 

Coefficients:
                  Estimate Std. Error t value Pr(>|t|)    
Petal.Length       0.17012    0.03489   4.876 2.81e-06 ***
Sepal.Width        0.20203    0.04420   4.571 1.03e-05 ***
Speciessetosa     -0.69529    0.14242  -4.882 2.74e-06 ***
Speciesversicolor  0.04165    0.15457   0.269   0.7879    
Speciesvirginica   0.48064    0.19005   2.529   0.0125 *  

---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.1685 on 145 degrees of freedom
Multiple R-squared:  0.9864,    Adjusted R-squared:  0.9859 
F-statistic:  2100 on 5 and 145 DF,  p-value: < 2.2e-16
daroczig commented 8 years ago

Good catch, thanks for reporting this issue! The above (very trivial, but hopefully OK) commit should resolve this behavior -- looking forward to any feedback.

krlmlr commented 8 years ago

Thanks. I'd prefer not to rearrange parameters at all (or at least make it optional), but you probably have a good reason to do this the way you're doing it.

daroczig commented 8 years ago

@RomanTsegelskyi do you remember the motives behind moving the intercept to the bottom of the table?

RomanTsegelskyi commented 8 years ago

I can't recall the reason right away, so it might be my mistake when I was refactoring the implementation

daroczig commented 8 years ago

I spent some time digging the git history and found d28cf55cd727464aeccb127f918f7e5ee792149f to be the related commit. This is due to #46, and we are moving the intercept to the bottom of the table as this looked appropriate based on the stargazer behavior.

@krlmlr @kovla does it make sense? Not sure if it's needed or not (personally, I do not care if the intercept is on the top or at the bottom of the table), looking forward to any feedback. And of course we can make this optional.

krlmlr commented 8 years ago

IMO, if some journals require this behavior, it's reasonable to support it as an option. Making it default confuses newbies that are used to R's output.

daroczig commented 8 years ago

Makes sense, done with the above commit:

> pander(lm(am ~ wt, mtcars))

--------------------------------------------------------------
     &nbsp;        Estimate   Std. Error   t value   Pr(>|t|) 
----------------- ---------- ------------ --------- ----------
 **(Intercept)**    1.542       0.2256      6.838   1.378e-07 

     **wt**        -0.3532     0.06717     -5.258   1.125e-05 
--------------------------------------------------------------

Table: Fitting linear model: am ~ wt

> pander(lm(am ~ wt, mtcars), move.intercept = TRUE)

--------------------------------------------------------------
     &nbsp;        Estimate   Std. Error   t value   Pr(>|t|) 
----------------- ---------- ------------ --------- ----------
     **wt**        -0.3532     0.06717     -5.258   1.125e-05 

 **(Intercept)**    1.542       0.2256      6.838   1.378e-07 
--------------------------------------------------------------

Table: Fitting linear model: am ~ wt