forked from mikenguyen13/data_analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path32-report.Rmd
207 lines (167 loc) · 5.4 KB
/
32-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
# 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
<br>
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}
# install.packages("equatiomatic")
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)
```