datalorax / equatiomatic

Convert models to LaTeX equations
https://datalorax.github.io/equatiomatic/
Creative Commons Attribution 4.0 International
618 stars 43 forks source link

Feature Request: add the standard error below the coefficent. #145

Open JiaxiangBU opened 3 years ago

JiaxiangBU commented 3 years ago

I know this package from https://bookdown.org/yihui/rmarkdown-cookbook/equatiomatic.html. Like the actual coefficent output,

#> $$
#> \operatorname{\widehat{mpg}} = 34.66 - 1.59(\operatorname{cyl}) - 0.02(\operatorname{disp})
#> $$

Does this package support the standard error below the coefficient (moreover, with R square)?

datalorax commented 3 years ago

It does not, but I'm happy to consider something like that. I'm not exactly sure how to incorporate that into the equation though. Do you have ideas on what it should look like?

Using your example, we get

library(equatiomatic)
library(lme4)
#> Loading required package: Matrix

m <- lm(mpg ~ cyl + disp, mtcars)
extract_eq(m, use_coef = TRUE)

The coefficients from this model are:

broom::tidy(m)
#> # A tibble: 3 x 5
#>   term        estimate std.error statistic  p.value
#>   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
#> 1 (Intercept)  34.7       2.55       13.6  4.02e-14
#> 2 cyl          -1.59      0.712      -2.23 3.37e- 2
#> 3 disp         -0.0206    0.0103     -2.01 5.42e- 2

So would you want something like

$$ \operatorname{\widehat{mpg}} = 34.66{2.55]} - 1.59{0.71]}(\operatorname{cyl}) - 0.02{[0.01]}(\operatorname{disp}) $$

which would render as

Screen Shot 2021-02-02 at 5 52 51 AM

That seems a little unconventional and a bit confusing to me because it almost looks like the coefficient estimates should be be multiplied by their standard errors.

We could try putting them as subscripts:

Screen Shot 2021-02-02 at 5 54 44 AM

Or as subscripts with annotations:

Screen Shot 2021-02-02 at 5 57 07 AM

But I still don't love the way those look. I am not at all sure how we could include r squared.

Created on 2021-02-02 by the reprex package (v0.3.0)

JaySumners commented 3 years ago

I've never seen the standard error included except as a +/-. Like the other notation though, it won't be standard mathematical notation and may be confusing.

JiaxiangBU commented 3 years ago

image

@datalorax Thanks for your reply, I think the stderr is below the coefficient. Here is an example from Wooldridge (2018,382).

I use the mathpix to capture the latex script, but the alignment should be fixed farther.

$$
\begin{aligned}
\hat{\Delta \log \left(\text { hrwage }_{t}\right)}=&-.0036+.809 \Delta \log (\text { outphr }) \\
&(.0042)(.173) \\
n=& 40, R^{2}=.364, \bar{R}^{2}=.348
\end{aligned}
$$

It is usual to report the fitted regression output with standard error and R square in econometrics textbook at the intro level.

Wooldridge, Jeffrey M. 2018. *Introductory Econometrics: A Modern Approach*. 7th ed. Cengage Learning, Inc.
datalorax commented 3 years ago

Ah... I see. Yeah that might be possible, although it would probably get pretty confusing for anything other than simple models. We could maybe give it a try though. Thanks for the suggestion!

ACC1100 commented 3 years ago

How's the progress on this feature? I just stumbled on this package and was also looking for this feature until I saw this issue.

I've personally been using \underset{bottom}{top} which appears follow the same example as the Wooldridge one the OP showed. Example below.

equation

And the code used to produce it: \widehat{work_i} = \underset{(`r lpm$std.error[1]`)}{`r lpm$coefficients[1]`} + \underset{(`r lpm$std.error[2]`)}{`r lpm$coefficients[2]`}educ_i + \underset{(`r lpm$std.error[3]`)}{`r lpm$coefficients[3]`}male_i + \underset{(`r lpm$std.error[4]`)}{`r lpm$coefficients[4]`}age_i

Ignore the messiness, the general usage would just be \underset{`r [std_error]`}{`r [coefficient]`} which shouldn't be too difficult to implement. I can create a pull request with the changes if this isn't already in the works/has been done.

datalorax commented 3 years ago

HI! The progress on this has been... none! I haven't worked on it at all, to be honest. But I just re-opened the issue and will keep it open as something to implement. I don't think it should be too terribly difficult. I just need to get the time and, unfortunately, this spring term is a bit overwhelming to me as is. But hopefully I can find some time here in the next few weeks to dedicate a day to some of these sorts of issues.