-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
38-report.Rmd
496 lines (392 loc) · 15.9 KB
/
38-report.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# Report
Structure
- Exploratory analysis
- plots
- preliminary results
- interesting structure/features in the data
- outliers
- Model
- Assumptions
- Why this model/ How is this model the best one?
- Consideration: interactions, collinearity, dependence
- Model Fit
- How well does it fit?
- Are the model assumptions met?
- Residual analysis
- Inference/ Prediction
- Are there different way to support your inference?
- Conclusion
- Recommendation
- Limitation of the analysis
- How to correct those in the future
This chapter is based on the `jtools` package. More information can be found [here.](https://www.rdocumentation.org/packages/jtools/versions/2.1.0)
## One summary table
Packages for reporting:
Summary Statistics Table:
- [qwraps2](https://cran.r-project.org/web/packages/qwraps2/vignettes/summary-statistics.html)
- [vtable](https://cran.r-project.org/web/packages/vtable/vignettes/sumtable.html)
- [gtsummary](http://www.danieldsjoberg.com/gtsummary/)
- [apaTables](https://cran.r-project.org/web/packages/apaTables/apaTables.pdf)
- [stargazer](https://cran.r-project.org/web/packages/stargazer/vignettes/stargazer.pdf)
Regression Table
- [gtsummary](http://www.danieldsjoberg.com/gtsummary/)
- [sjPlot,sjmisc, sjlabelled](https://cran.r-project.org/web/packages/sjPlot/vignettes/tab_model_estimates.html)
- [stargazer](https://cran.r-project.org/web/packages/stargazer/vignettes/stargazer.pdf): recommended ([Example](https://www.jakeruss.com/cheatsheets/stargazer/))
- [modelsummary](https://github.com/vincentarelbundock/modelsummary#a-simple-example)
```{r}
library(jtools)
data(movies)
fit <- lm(metascore ~ budget + us_gross + year, data = movies)
summ(fit)
summ(
fit,
scale = TRUE,
vifs = TRUE,
part.corr = TRUE,
confint = TRUE,
pvals = FALSE
) # notice that scale here is TRUE
#obtain clsuter-robust SE
data("PetersenCL", package = "sandwich")
fit2 <- lm(y ~ x, data = PetersenCL)
summ(fit2, robust = "HC3", cluster = "firm")
```
Model to Equation
```{r, eval = FALSE}
# install.packages("equatiomatic") # not available for R 4.2
fit <- lm(metascore ~ budget + us_gross + year, data = movies)
# show the theoretical model
equatiomatic::extract_eq(fit)
# display the actual coefficients
equatiomatic::extract_eq(fit, use_coefs = TRUE)
```
## Model Comparison
```{r}
fit <- lm(metascore ~ log(budget), data = movies)
fit_b <- lm(metascore ~ log(budget) + log(us_gross), data = movies)
fit_c <- lm(metascore ~ log(budget) + log(us_gross) + runtime, data = movies)
coef_names <- c("Budget" = "log(budget)", "US Gross" = "log(us_gross)",
"Runtime (Hours)" = "runtime", "Constant" = "(Intercept)")
export_summs(fit, fit_b, fit_c, robust = "HC3", coefs = coef_names)
```
Another package is `modelsummary`
```{r}
library(modelsummary)
lm_mod <- lm(mpg ~ wt + hp + cyl, mtcars)
msummary(lm_mod, vcov = c("iid","robust","HC4"))
modelplot(lm_mod, vcov = c("iid","robust","HC4"))
```
Another package is `stargazer`
```{r}
library("stargazer")
stargazer(attitude)
## 2 OLS models
linear.1 <-
lm(rating ~ complaints + privileges + learning + raises + critical,
data = attitude)
linear.2 <-
lm(rating ~ complaints + privileges + learning, data = attitude)
## create an indicator dependent variable, and run a probit model
attitude$high.rating <- (attitude$rating > 70)
probit.model <-
glm(
high.rating ~ learning + critical + advance,
data = attitude,
family = binomial(link = "probit")
)
stargazer(linear.1,
linear.2,
probit.model,
title = "Results",
align = TRUE)
```
```{r eval = FALSE}
# Latex
stargazer(
linear.1,
linear.2,
probit.model,
title = "Regression Results",
align = TRUE,
dep.var.labels = c("Overall Rating", "High Rating"),
covariate.labels = c(
"Handling of Complaints",
"No Special Privileges",
"Opportunity to Learn",
"Performance-Based Raises",
"Too Critical",
"Advancement"
),
omit.stat = c("LL", "ser", "f"),
no.space = TRUE
)
```
```{r}
# ASCII text output
stargazer(
linear.1,
linear.2,
type = "text",
title = "Regression Results",
dep.var.labels = c("Overall Rating", "High Rating"),
covariate.labels = c(
"Handling of Complaints",
"No Special Privileges",
"Opportunity to Learn",
"Performance-Based Raises",
"Too Critical",
"Advancement"
),
omit.stat = c("LL", "ser", "f"),
ci = TRUE,
ci.level = 0.90,
single.row = TRUE
)
```
```{r eval = FALSE}
stargazer(
linear.1,
linear.2,
probit.model,
title = "Regression Results",
align = TRUE,
dep.var.labels = c("Overall Rating", "High Rating"),
covariate.labels = c(
"Handling of Complaints",
"No Special Privileges",
"Opportunity to Learn",
"Performance-Based Raises",
"Too Critical",
"Advancement"
),
omit.stat = c("LL", "ser", "f"),
no.space = TRUE
)
```
Correlation Table
```{r eval = FALSE}
correlation.matrix <-
cor(attitude[, c("rating", "complaints", "privileges")])
stargazer(correlation.matrix, title = "Correlation Matrix")
```
## Changes in an estimate
```{r}
coef_names <- coef_names[1:3] # Dropping intercept for plots
plot_summs(fit, fit_b, fit_c, robust = "HC3", coefs = coef_names)
plot_summs(
fit_c,
robust = "HC3",
coefs = coef_names,
plot.distributions = TRUE
)
```
## Standard Errors
`sandwich` [vignette](cran.r-project.org/web/packages/sandwich/vignettes/sandwich-CL.pdf)
+------------+------------+------------------------------------------------------------------------------------------+--------------------------+
| Type | Applicable | Usage | Reference |
+============+============+==========================================================================================+==========================+
| `const` | | Assume constant variances | |
+------------+------------+------------------------------------------------------------------------------------------+--------------------------+
| `HC` `HC0` | `vcovCL` | Heterogeneity | [@white1980] |
| | | | |
| | | White's estimator | |
| | | | |
| | | All other heterogeneity SE methods are derivatives of this. | |
| | | | |
| | | No small sample bias adjustment | |
+------------+------------+------------------------------------------------------------------------------------------+--------------------------+
| `HC1` | `vcovCL` | Uses a degrees of freedom-based correction | [@mackinnon1985some] |
| | | | |
| | | When the number of clusters is small, `HC2` and `HC3` are better [@cameron2008bootstrap] | |
+------------+------------+------------------------------------------------------------------------------------------+--------------------------+
| `HC2` | `vcovCL` | Better with the linear model, but still applicable for [Generalized Linear Models] | |
| | | | |
| | | Needs a hat (weighted) matrix | |
+------------+------------+------------------------------------------------------------------------------------------+--------------------------+
| `HC3` | `vcovCL` | Better with the linear model, but still applicable for [Generalized Linear Models] | |
| | | | |
| | | Needs a hat (weighted) matrix | |
+------------+------------+------------------------------------------------------------------------------------------+--------------------------+
| `HC4` | `vcovHC` | | [@cribari2004asymptotic] |
+------------+------------+------------------------------------------------------------------------------------------+--------------------------+
| `HC4m` | `vcovHC` | | [@cribari2007inference] |
+------------+------------+------------------------------------------------------------------------------------------+--------------------------+
| `HC5` | `vcovHC` | | [@cribari2011new] |
+------------+------------+------------------------------------------------------------------------------------------+--------------------------+
```{r}
data(cars)
model <- lm(speed ~ dist, data = cars)
summary(model)
lmtest::coeftest(model, vcov. = sandwich::vcovHC(model, type = "HC1"))
```
## Coefficient Uncertainty and Distribution
The `ggdist` allows us to visualize uncertainty under both frequentist and Bayesian frameworks
```{r, eval=FALSE}
library(ggdist)
```
## Descriptive Tables
Export APA theme
```{r, eval = FALSE}
data("mtcars")
library(flextable)
theme_apa(flextable(mtcars[1:5,1:5]))
```
Export to Latex
```{r, eval=FALSE}
print(xtable::xtable(mtcars, type = "latex"),
file = file.path(getwd(), "output", "mtcars_xtable.tex"))
# American Economic Review style
stargazer::stargazer(
mtcars,
title = "Testing",
style = "aer",
out = file.path(getwd(), "output", "mtcars_stargazer.tex")
)
# other styles include
# Administrative Science Quarterly
# Quarterly Journal of Economics
```
However, the above codes do not play well with notes. Hence, I create my own custom code that follows the AMA guidelines
```{r, eval = FALSE}
ama_tbl <- function(data, caption, label, note, output_path) {
library(tidyverse)
library(xtable)
# Function to determine column alignment
get_column_alignment <- function(data) {
# Start with the alignment for the header row
alignment <- c("l", "l")
# Check each column
for (col in seq_len(ncol(data))[-1]) {
if (is.numeric(data[[col]])) {
alignment <- c(alignment, "r") # Right alignment for numbers
} else {
alignment <- c(alignment, "c") # Center alignment for other data
}
}
return(alignment)
}
data %>%
# bold + left align first column
rename_with(~paste("\\multicolumn{1}{l}{\\textbf{", ., "}}"), 1) %>%
# bold + center align all other columns
`colnames<-`(ifelse(colnames(.) != colnames(.)[1],
paste("\\multicolumn{1}{c}{\\textbf{", colnames(.), "}}"),
colnames(.))) %>%
xtable(caption = caption,
label = label,
align = get_column_alignment(data),
auto = TRUE) %>%
print(
include.rownames = FALSE,
caption.placement = "top",
hline.after=c(-1, 0),
# p{0.9\linewidth} sets the width of the column to 90% of the line width, and the @{} removes any extra padding around the cell.
add.to.row = list(pos = list(nrow(data)), # Add at the bottom of the table
command = c(paste0("\\hline \n \\multicolumn{",ncol(data), "}{l} {", "\n \\begin{tabular}{@{}p{0.9\\linewidth}@{}} \n","Note: ", note, "\n \\end{tabular} } \n"))), # Add your note here
# make sure your heading is untouched (because you manually change it above)
sanitize.colnames.function = identity,
# place a the top of the page
table.placement = "h",
file = output_path
)
}
```
```{r, eval = FALSE}
ama_tbl(
mtcars,
caption = "This is caption",
label = "tab:this_is_label",
note = "this is note",
output_path = file.path(getwd(), "output", "mtcars_custom_ama.tex")
)
```
## Visualizations and Plots
You can customize your plots based on your preferred journals. Here, I am creating a custom setting for the American Marketing Association.
American-Marketing-Association-ready theme for plots
```{r}
library(ggplot2)
# check available fonts
# windowsFonts()
# for Times New Roman
# names(windowsFonts()[windowsFonts()=="TT Times New Roman"])
```
```{r}
# Making a theme
amatheme = theme_bw(base_size = 14, base_family = "serif") + # This is Time New Roman
theme(
# remove major gridlines
panel.grid.major = element_blank(),
# remove minor gridlines
panel.grid.minor = element_blank(),
# remove panel border
panel.border = element_blank(),
line = element_line(),
# change font
text = element_text(),
# if you want to remove legend title
# legend.title = element_blank(),
legend.title = element_text(size = rel(0.6), face = "bold"),
# change font size of legend
legend.text = element_text(size = rel(0.6)),
legend.background = element_rect(color = "black"),
# legend.margin = margin(t = 5, l = 5, r = 5, b = 5),
# legend.key = element_rect(color = NA, fill = NA),
# change font size of main title
plot.title = element_text(
size = rel(1.2),
face = "bold",
hjust = 0.5,
margin = margin(b = 15)
),
plot.margin = unit(c(1, 1, 1, 1), "cm"),
# add black line along axes
axis.line = element_line(colour = "black", linewidth = .8),
axis.ticks = element_line(),
# axis title
axis.title.x = element_text(size = rel(1.2), face = "bold"),
axis.title.y = element_text(size = rel(1.2), face = "bold"),
# axis text size
axis.text.y = element_text(size = rel(1)),
axis.text.x = element_text(size = rel(1))
)
```
Example
```{r}
library(tidyverse)
library(ggsci)
data("mtcars")
yourplot <- mtcars %>%
select(mpg, cyl, gear) %>%
ggplot(., aes(x = mpg, y = cyl, fill = gear)) +
geom_point() +
labs(title="Some Plot")
yourplot +
amatheme +
# choose different color theme
scale_color_npg()
yourplot +
amatheme +
scale_color_continuous()
```
Other pre-specified themes
```{r}
library(ggthemes)
# Stata theme
yourplot +
theme_stata()
# The economist theme
yourplot +
theme_economist()
yourplot +
theme_economist_white()
# Wall street journal theme
yourplot +
theme_wsj()
# APA theme
yourplot +
jtools::theme_apa(
legend.font.size = 24,
x.font.size = 20,
y.font.size = 20
)
```