Chapter 13 Writing

Good quantitative analysis is not only about doing stuff in R. In fact, you will not be showing your audience your results directly in R, but export the out in form of tables, figures, numbers, equations etc. Here, we will provide some advice on how to report your analysis.

13.1 Equations

To transform statistical models into equations, i.e. to ensure that you can communicate the equation of the model you are estimating, we recommend that you use the equatiomatic package (Anderson, Heiss, & Rosenberg, 2019). You can read more about the package on https://github.com/datalorax/equatiomatic. First, load the package (and install it if you haven’t already done so).

library("equatiomatic")

To illustrate how the package works, let us estimate a multivariate OLS regression model with mpg as the outcome and cyl and disp as our predictors and save it in the object mod1.

mod1 <- lm(mpg ~ cyl + disp, data = mtcars)

To get the equation for this model, simply use extract_eq() on the object (in this case mod1).

extract_eq(mod1)
## $$
## \text{mpg} = \alpha + \beta_{1}(\text{cyl}) + \beta_{2}(\text{disp}) + \epsilon
## $$

We can then take this LaTeX equation and put it into our document and get the equation (the built-in equation editor in Word also works with LaTeX equations):

\[ \text{mpg} = \alpha + \beta_{1}(\text{cyl}) + \beta_{2}(\text{disp}) + \epsilon \]

If we would like to report the actual coefficients rather than alpha and betas, we can set the option use_coefs to TRUE.

extract_eq(mod1, use_coefs = TRUE)
## $$
## \text{mpg} = 34.66 - 1.59(\text{cyl}) - 0.02(\text{disp}) + \epsilon
## $$

The function works with all models supported by tidy() in the broom package. Noteworthy, it also works well with interaction terms. To show this, we can specify an interaction between cyl and disp in a linear model saved in the object mod2.

mod2 <- lm(mpg ~ cyl*disp, mtcars)

Again, we can use extract_eq() to get the equation for the model:

extract_eq(mod2)
## $$
## \text{mpg} = \alpha + \beta_{1}(\text{cyl}) + \beta_{2}(\text{disp}) + \beta_{3}(\text{cyl} \times \text{disp}) + \epsilon
## $$

This LaTeX equation can, as pointed out above, simply be placed into our document:

\[ \text{mpg} = \alpha + \beta_{1}(\text{cyl}) + \beta_{2}(\text{disp}) + \beta_{3}(\text{cyl} \times \text{disp}) + \epsilon \]