-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path02_tables.Rpres
318 lines (219 loc) · 6.76 KB
/
02_tables.Rpres
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
Bonus Slides: Making pretty regression tables with pipes
========================================================
autosize: true
author: Tristan Mahr, @tjmahr
date: March 18, 2015
css: assets/custom.css
Madison R Users Group
Repository for this talk: https://github.com/tjmahr/MadR_Pipelines
```{r, echo = FALSE}
options(
width = 45,
dplyr.width = 44,
dplyr.print_min = 4,
dplyr.print_max = 4)
```
Plan
========================================================
Earlier, I [covered pipelines][magrittr_talk].
Then, I [used pipelines with dplyr][dplyr_talk].
Now, I will demonstrate off how I use pipelines to make
pretty-printed regression tables.
```{r}
library("magrittr")
library("dplyr")
library("broom")
library("stringr")
library("knitr")
```
[magrittr_talk]: http://rpubs.com/tjmahr/pipelines_2015
[dplyr_talk]: http://rpubs.com/tjmahr/dplyr_2015
linear regression
========================================================
title: false
I want to print a regression summary table.
```{r}
model <- lm(Sepal.Length ~ Sepal.Width, iris)
summary(model)
```
broom::tidy
========================================================
`broom`'s `tidy` function extracts summary values into a data-frame.
```{r}
m_table <- tidy(model)
m_table %>% print(digits = 3)
```
knitr::kable
========================================================
`knitr`'s `kable` function prints a data-frame as a Markdown table,
which rmarkdown/pandoc can convert to HTML or Word.
```{r, eval = FALSE}
kable(m_table, digits = 3,
col.names = c("Param", "B", "SE", "t", "p"))
```
Raw Markdown output
```
`r kable(m_table, digits = 3,
col.names = c("Param", "B", "SE", "t", "p"))`
```
========================================================
title: false
Which renders as:
```{r}
kable(m_table, digits = 3,
col.names = c("Param", "B", "SE", "t", "p"))
```
Almost there
========================================================
incremental: true
Two more things that I want.
1. APA style for printing numbers
* Two digits of precision for estimates, errors and statistics.
* Three digits for p-values.
* Bounded values (correlations, p-values) don't need leading zero.
* Tiny p-values should not be 0.000. Use `< .001` instead.
2. Flexibility for many related models. (I don't know the
final in-text model beforehand.)
Approach
========================================================
incremental: true
- Build a set of generic, reusable functions for formatting numbers.
- Make a custom pipeline to format the `term` column.
- Assemble a pipeline to convert a model into a pretty table.
Digit Formatters
========================================================
The `formatC` function is big and confusing, but after I
figured out something that works well enough, I extracted a
function. Now I don't have to worry about solving that
problem again.
```{r}
# Print with n digits of precision
fixed_digits <- function(xs, n = 2) {
formatC(xs, digits = n, format = "f")
}
# Want .100 to print with two digits
c(1000.012, 0.0001, 0.100) %>% fixed_digits(2)
```
Remove leading zeros
========================================================
Here I do a sanity check to see if this operation is necessary.
```{r}
# Don't print leading zero on bounded numbers.
remove_leading_zero <- function(xs) {
# Problem if any value is greater than 1.0
digit_matters <- xs %>% as.numeric %>%
abs %>% is_greater_than(1)
if (any(digit_matters)) {
warning("Non-zero leading digit")
}
str_replace(xs, "^(-?)0", "\\1")
}
c(1.00, -0.12, 0.87, 0.82) %>% remove_leading_zero
```
p-value formatter
========================================================
I format p-values with the previous two functions and overwrite the
really small ones with the `< .001` template.
```{r}
# Print three digits of a p-value, but use
# the "< .001" notation on tiny values.
format_pval <- function(ps, html = FALSE) {
tiny <- ifelse(html, "< .001", "< .001")
ps_chr <- ps %>% fixed_digits(3) %>%
remove_leading_zero
ps_chr[ps < 0.001] <- tiny
ps_chr
}
m_table$p.value %>% format_pval
```
Renaming variables
=======================================================
A few packages already solve the pretty-table problem--stargazer,
texreg, xtable--and they do amazing things. But their functions
usually rely on an argument where you can specify custom labels
for the regression predictors. This is fine for one-off tables.
When I have a bunch of related models, I prefer to use a
string-manipulation pipeline to convert R names into long-hand names.
Renaming variables
=======================================================
```{r}
fix_names <- . %>%
str_replace(".Intercept.", "Intercept") %>%
str_replace("Species", "") %>%
# Capitalize species names
str_replace("setosa", "Setosa") %>%
str_replace("versicolor", "Versicolor") %>%
str_replace("virginica", "Virginica") %>%
# Clean up special characters
str_replace_all(".Width", " Width") %>%
str_replace_all(".Length", " Length") %>%
str_replace_all(":", " x ")
```
Formatting pipeline
=======================================================
1. Format the numbers
2. Rename the terms
3. Rename the column headings
```{r}
two_digits <- . %>% fixed_digits(2)
table_names <- c("Parameter", "Estimate", "SE",
"_t_", "_p_")
format_model_table <- . %>%
mutate_each(funs(two_digits),
-term, -p.value) %>%
mutate(term = fix_names(term),
p.value = format_pval(p.value)) %>%
set_colnames(table_names)
```
Overall pipeline
=======================================================
1. Fit a model
2. Extract values for summary table
3. Format table
4. Print as markdown
```{r, eval = FALSE}
lm(..., iris) %>%
tidy %>%
format_model_table %>%
kable
```
========================================================
title: false
```{r, results = 'asis'}
lm(Sepal.Length ~ Sepal.Width, iris) %>%
tidy %>%
format_model_table %>%
kable(align = "r")
```
========================================================
title: false
```{r, results = 'asis'}
lm(Sepal.Length ~ Species * Sepal.Width, iris) %>%
tidy %>%
format_model_table %>%
kable(align = "r")
```
========================================================
title: false
```{r, results = 'asis'}
lm(Sepal.Length ~ Petal.Length * Species, iris) %>%
tidy %>%
format_model_table %>%
kable(align = "r")
```
========================================================
title: false
```{r, results = 'asis'}
lm(Sepal.Length ~ Species * Sepal.Width + Petal.Length, iris) %>%
tidy %>%
format_model_table %>%
kable(align = "r")
```
========================================================
title: false
```{r, results = 'asis'}
lm(Sepal.Length ~ Species * Sepal.Width * Petal.Length, iris) %>%
tidy %>%
format_model_table %>%
kable(align = "r")
```