-
Notifications
You must be signed in to change notification settings - Fork 43
/
README.Rmd
443 lines (316 loc) · 13.8 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include=FALSE}
library(magrittr)
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
library(texPreview)
tex_opts$set(
density = 600 # High resolution LaTeX output
)
# Include HTML images when knitting with github_document
if (isTRUE(getOption('knitr.in.progress'))) {
tex_opts$set(
returnType = "html"
)
}
# Don't use Helvetica in examples
# This could go in tex_opts$set, but it doesn't seem to actually get used there
usrPackages <- "\\renewcommand*\\familydefault{\\rmdefault}"
```
# equatiomatic <img src="man/figures/logo.png" align="right"/>
<!-- badges: start -->
[![lifecycle](https://lifecycle.r-lib.org/articles/figures/lifecycle-stable.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable) [![R build status](https://github.com/datalorax/equatiomatic/workflows/R-CMD-check/badge.svg)](https://github.com/datalorax/equatiomatic/actions) [![Codecov test coverage](https://codecov.io/gh/datalorax/equatiomatic/branch/master/graph/badge.svg)](https://app.codecov.io/gh/datalorax/equatiomatic?branch=master) [![Covrpage Summary](https://img.shields.io/badge/covrpage-Last_Build_2020_08_24-brightgreen.svg)](https://github.com/datalorax/equatiomatic/tree/master/tests) [![](https://cranlogs.r-pkg.org/badges/grand-total/equatiomatic)](https://cranlogs.r-pkg.org/badges/grand-total/equatiomatic)
<!-- badges: end -->
The goal of **equatiomatic** is to reduce the pain associated with writing LaTeX
code from a fitted model. The package aims to support any model
supported by [**broom**](https://cran.r-project.org/package=broom). See the [introduction to equatiomatic](https://datalorax.github.io/equatiomatic/articles/equatiomatic.html#other-models) for currently supported models.
## Installation
Install from CRAN with
```{r install-cran, eval=FALSE}
install.packages("equatiomatic")
```
Or get the development version from GitHub with
```{r install-command, eval=FALSE}
remotes::install_github("datalorax/equatiomatic")
```
## Basic usage
![](https://github.com/datalorax/equatiomatic/raw/master/img/equatiomatic.gif)
The gif above shows the basic functionality.
To convert a model to LaTeX, feed a model object to `extract_eq()`:
```{r example-basic, warning=FALSE, message=FALSE, eval=FALSE}
library(equatiomatic)
# Fit a simple model
mod1 <- lm(mpg ~ cyl + disp, mtcars)
# Give the results to extract_eq
extract_eq(mod1)
```
```{r example-basic-preview, echo=FALSE}
library(equatiomatic)
mod1 <- lm(mpg ~ cyl + disp, mtcars)
extract_eq(mod1) %>%
print()
extract_eq(mod1) %>%
tex_preview(usrPackages = usrPackages)
```
The model can be built in any standard way. It can handle shortcut syntax:
```{r example-shortcut, eval=FALSE}
mod2 <- lm(mpg ~ ., mtcars)
extract_eq(mod2)
```
```{r example-shortcut-preview, echo=FALSE}
mod2 <- lm(mpg ~ ., mtcars)
extract_eq(mod2) %>%
print()
extract_eq(mod2) %>%
tex_preview(usrPackages = usrPackages)
```
When using categorical variables, it will include the levels of the variables as subscripts. Here, we use data from the [{palmerpenguins}](https://allisonhorst.github.io/palmerpenguins/index.html) dataset.
```{r example-categorical, eval=FALSE}
mod3 <- lm(body_mass_g ~ bill_length_mm + species, penguins)
extract_eq(mod3)
```
```{r example-categorical-preview, echo=FALSE}
mod3 <- lm(body_mass_g ~ bill_length_mm + species, penguins)
extract_eq(mod3) %>%
print()
extract_eq(mod3) %>%
tex_preview(usrPackages = usrPackages)
```
It helpfully preserves the order the variables are supplied in the formula:
```{r example-preserve-order, eval=FALSE}
set.seed(8675309)
d <- data.frame(cat1 = rep(letters[1:3], 100),
cat2 = rep(LETTERS[1:3], each = 100),
cont1 = rnorm(300, 100, 1),
cont2 = rnorm(300, 50, 5),
out = rnorm(300, 10, 0.5))
mod4 <- lm(out ~ cont1 + cat2 + cont2 + cat1, d)
extract_eq(mod4)
```
```{r example-preserve-order-preview, echo=FALSE}
set.seed(8675309)
d <- data.frame(cat1 = rep(letters[1:3], 100),
cat2 = rep(LETTERS[1:3], each = 100),
cont1 = rnorm(300, 100, 1),
cont2 = rnorm(300, 50, 5),
out = rnorm(300, 10, 0.5))
mod4 <- lm(out ~ cont1 + cat2 + cont2 + cat1, d)
extract_eq(mod4) %>%
print()
extract_eq(mod4) %>%
tex_preview(usrPackages = usrPackages)
```
## Appearance
You can wrap the equations so that a specified number of terms appear on the right-hand side of the equation using `terms_per_line` (defaults to 4):
```{r example-wrap, eval=FALSE}
extract_eq(mod2, wrap = TRUE)
```
```{r example-wrap-preview, echo=FALSE}
extract_eq(mod2, wrap = TRUE) %>%
print()
extract_eq(mod2, wrap = TRUE) %>%
tex_preview(usrPackages = usrPackages)
```
```{r example-wrap-longer, eval=FALSE}
extract_eq(mod2, wrap = TRUE, terms_per_line = 6)
```
```{r example-wrap-longer-preview, echo=FALSE}
extract_eq(mod2, wrap = TRUE, terms_per_line = 6) %>%
print()
extract_eq(mod2, wrap = TRUE, terms_per_line = 6) %>%
tex_preview(usrPackages = usrPackages)
```
When wrapping, you can change whether the lines end with trailing math operators like `+` (the default), or if they should begin with them using `operator_location = "end"` or `operator_location = "start"`:
```{r example-wrap-longer-location, eval=FALSE}
extract_eq(mod2, wrap = TRUE, terms_per_line = 4, operator_location = "start")
```
```{r example-wrap-longer-location-preview, echo=FALSE}
extract_eq(mod2, wrap = TRUE, terms_per_line = 4, operator_location = "start") %>%
print()
extract_eq(mod2, wrap = TRUE, terms_per_line = 4, operator_location = "start") %>%
tex_preview(usrPackages = usrPackages)
```
By default, all text in the equation is wrapped in `\operatorname{}`. You can optionally have the variables themselves be italicized (i.e. not be wrapped in `\operatorname{}`) with `ital_vars = TRUE`:
```{r example-italics, eval=FALSE}
extract_eq(mod2, wrap = TRUE, ital_vars = TRUE)
```
```{r example-italics-preview, echo=FALSE}
extract_eq(mod2, wrap = TRUE, ital_vars = TRUE) %>%
print()
extract_eq(mod2, wrap = TRUE, ital_vars = TRUE) %>%
tex_preview(usrPackages = usrPackages)
```
## R Markdown and previewing
If you include `extract_eq()` in an R Markdown chunk, **knitr** will render the equation. If you'd like to see the LaTeX code wrap the call in `print()`.
You can also use the `tex_preview()` function from the [**texPreview**](https://cran.r-project.org/package=texPreview) package to preview the equation in RStudio:
```{r example-preview, eval=FALSE}
tex_preview(extract_eq(mod1))
```
![](man/figures/preview1.png)
Both `extract_eq()` and `tex_preview()` work with **magrittr** pipes, so you can do something like this:
```{r pipe-example, eval=FALSE}
library(magrittr) # or library(tidyverse) or any other package that exports %>%
extract_eq(mod1) %>%
tex_preview()
```
## Extra options
There are several extra options you can enable with additional arguments to `extract_eq()`.
### Actual coefficients
You can return actual numeric coefficients instead of Greek letters with `use_coefs = TRUE`:
```{r use-coefs, eval=FALSE}
extract_eq(mod1, use_coefs = TRUE)
```
```{r use-coefs-preview, echo=FALSE}
extract_eq(mod1, use_coefs = TRUE) %>%
print()
extract_eq(mod1, use_coefs = TRUE) %>%
tex_preview(usrPackages = usrPackages)
```
By default, it will remove doubled operators like "+ -", but you can keep those in (which is often useful for teaching) with `fix_signs = FALSE`:
```{r fix-signs, eval=FALSE}
extract_eq(mod1, use_coefs = TRUE, fix_signs = FALSE)
```
```{r fix-signs-preview, echo=FALSE}
extract_eq(mod1, use_coefs = TRUE, fix_signs = FALSE) %>%
print()
extract_eq(mod1, use_coefs = TRUE, fix_signs = FALSE) %>%
tex_preview(usrPackages = usrPackages)
```
This works in longer wrapped equations:
```{r fix-signs-long, eval=FALSE}
extract_eq(mod2, wrap = TRUE, terms_per_line = 3,
use_coefs = TRUE, fix_signs = FALSE)
```
```{r fix-signs-long-preview, echo=FALSE}
extract_eq(mod2, wrap = TRUE, terms_per_line = 3,
use_coefs = TRUE, fix_signs = FALSE) %>%
print()
extract_eq(mod2, wrap = TRUE, terms_per_line = 3,
use_coefs = TRUE, fix_signs = FALSE) %>%
tex_preview(usrPackages = usrPackages)
```
## Beyond `lm()`
You're not limited to just `lm` models! **equatiomatic** supports many other models, including logistic regression, probit regression, and ordered logistic regression (with `MASS::polr()`).
### Logistic regression with `glm()`
```{r example-logit, eval=FALSE}
model_logit <- glm(sex ~ bill_length_mm + species,
data = penguins, family = binomial(link = "logit"))
extract_eq(model_logit, wrap = TRUE, terms_per_line = 3)
```
```{r example-logit-preview, echo=FALSE}
model_logit <- glm(sex ~ bill_length_mm + species,
data = penguins, family = binomial(link = "logit"))
extract_eq(model_logit, wrap = TRUE, terms_per_line = 3) %>%
print()
extract_eq(model_logit, wrap = TRUE, terms_per_line = 3) %>%
tex_preview(usrPackages = usrPackages)
```
### Probit regression with `glm()`
```{r example-probit, eval=FALSE}
model_probit <- glm(sex ~ bill_length_mm + species,
data = penguins, family = binomial(link = "probit"))
extract_eq(model_probit, wrap = TRUE, terms_per_line = 3)
```
```{r example-probit-preview, echo=FALSE}
model_probit <- glm(sex ~ bill_length_mm + species,
data = penguins, family = binomial(link = "probit"))
extract_eq(model_probit, wrap = TRUE, terms_per_line = 3) %>%
print()
extract_eq(model_probit, wrap = TRUE, terms_per_line = 3) %>%
tex_preview(usrPackages = usrPackages)
```
### Ordered logistic regression with `MASS::polr()`
```{r example-polr, eval=FALSE}
set.seed(1234)
df <- data.frame(outcome = factor(rep(LETTERS[1:3], 100),
levels = LETTERS[1:3],
ordered = TRUE),
continuous_1 = rnorm(300, 100, 1),
continuous_2 = rnorm(300, 50, 5))
model_ologit <- MASS::polr(outcome ~ continuous_1 + continuous_2,
data = df, Hess = TRUE, method = "logistic")
model_oprobit <- MASS::polr(outcome ~ continuous_1 + continuous_2,
data = df, Hess = TRUE, method = "probit")
extract_eq(model_ologit, wrap = TRUE)
```
```{r example-ologit-preview, echo=FALSE}
set.seed(1234)
df <- data.frame(outcome = factor(rep(LETTERS[1:3], 100),
levels = LETTERS[1:3],
ordered = TRUE),
continuous_1 = rnorm(300, 100, 1),
continuous_2 = rnorm(300, 50, 5))
model_ologit <- MASS::polr(outcome ~ continuous_1 + continuous_2,
data = df, Hess = TRUE, method = "logistic")
model_oprobit <- MASS::polr(outcome ~ continuous_1 + continuous_2,
data = df, Hess = TRUE, method = "probit")
extract_eq(model_ologit, wrap = TRUE) %>%
print()
extract_eq(model_ologit, wrap = TRUE) %>%
tex_preview(usrPackages = usrPackages)
```
```{r example-polr-probit, eval=FALSE}
extract_eq(model_oprobit, wrap = TRUE)
```
```{r example-polr-probit-preview, echo=FALSE}
extract_eq(model_oprobit, wrap = TRUE) %>%
print()
extract_eq(model_oprobit, wrap = TRUE) %>%
tex_preview(usrPackages = usrPackages)
```
### Ordered regression (logit and probit) with `ordinal::clm()`
```{r example-clm, eval=FALSE}
set.seed(1234)
df <- data.frame(outcome = factor(rep(LETTERS[1:3], 100),
levels = LETTERS[1:3],
ordered = TRUE),
continuous_1 = rnorm(300, 1, 1),
continuous_2 = rnorm(300, 5, 5))
model_ologit <- ordinal::clm(outcome ~ continuous_1 + continuous_2,
data = df, link = "logit")
model_oprobit <- ordinal::clm(outcome ~ continuous_1 + continuous_2,
data = df, link = "probit")
extract_eq(model_ologit, wrap = TRUE)
```
```{r example-clm-ologit-preview, echo=FALSE}
set.seed(1234)
df <- data.frame(outcome = factor(rep(LETTERS[1:3], 100),
levels = LETTERS[1:3],
ordered = TRUE),
continuous_1 = rnorm(300, 1, 1),
continuous_2 = rnorm(300, 5, 5))
model_ologit <- ordinal::clm(outcome ~ continuous_1 + continuous_2,
data = df, link = "logit")
model_oprobit <- ordinal::clm(outcome ~ continuous_1 + continuous_2,
data = df, link = "probit")
extract_eq(model_ologit, wrap = TRUE) %>%
print()
extract_eq(model_ologit, wrap = TRUE)
```
```{r example-clm-probit, eval=FALSE}
extract_eq(model_oprobit, wrap = TRUE)
```
```{r example-clm-oprobit-preview, echo=FALSE}
extract_eq(model_oprobit, wrap = TRUE) %>%
print()
extract_eq(model_oprobit, wrap = TRUE)
```
## Extension
If you would like to contribute to this package, we'd love your help!
We are particularly interested in extending to more models. We hope to support
any model supported by [**broom**](https://cran.r-project.org/package=broom) in
the future.
## Code of Conduct
Please note that the 'equatiomatic' project is released with a
[Contributor Code of Conduct](https://github.com/datalorax/equatiomatic/blob/master/CODE_OF_CONDUCT.md).
By contributing to this project, you agree to abide by its terms.
## A note of appreciation
We'd like to thank the authors of the [{palmerpenguins}](https://allisonhorst.github.io/palmerpenguins/index.html) dataset for generously allowing us to incorporate the `penguins` dataset in our package for example usage.
Horst AM, Hill AP, Gorman KB (2020). *palmerpenguins: Palmer Archipelago (Antarctica) penguin data*. R package version 0.1.0. https://allisonhorst.github.io/palmerpenguins/