forked from BeSeLuFri/RforISG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
other_regressions.Rmd
295 lines (199 loc) · 7.09 KB
/
other_regressions.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
---
title: "Regressions"
---
```{r echo = F}
knitr::opts_chunk$set(echo = TRUE)
```
Download the script [here](https://raw.githubusercontent.com/BeSeLuFri/RforISG/master/Files/Course%20Scripts/other_regressions_clean.R). There are no exercises so this script contains all necessary code already.
Download the data [here](https://raw.githubusercontent.com/BeSeLuFri/RforISG/master/data/eurostat_data.csv) (same data as on the [DataViz Eurostat page](https://beselufri.github.io/RforISG/dataviz_eurostat.html))
***
# Tl;dr
This is how standard OLS regressions are done:
```{r, eval=FALSE}
ols_object <- lm(regressand ~ regressor1 + regressor2 + regressor3,
data = data.frame)
```
This is how a (two-ways FE) panel regression is done
```{r, eval=FALSE}
library(plm)
panel_object <-
plm(
regressand ~ regressor1 + regressor2 + regressor3,
index = c("unit", "time"),
model = "within",
effect = "twoways",
# for two-ways fixed effect regressions
data = data.frame
)
```
This is how you can get a summary of your regression:
```{r, eval=FALSE}
summary(ols_object, panel_object)
```
This is a nicer way for getting a summary and exporting (journal style)
```{r, eval=FALSE}
library(stargazer)
stargazer(ols_object, panel_object,
type = "html", # either html, latex, or text
out = "output.html") # if a file should be produced
```
# Introduction
## Workflow
```{r echo=FALSE}
DiagrammeR::grViz("Files/lm-modelling.gv", width = 800, height = 400)
```
***
## The `lm` Function
```{r eval=FALSE, echo=TRUE, message=FALSE, warning=FALSE}
lm(formula,
data,
subset,
na.action)
```
`formula` - Specification of our regression model
`data` - The dataset containing the variables of the regression
`subset` - An option to subset the data
`na.action` - Option that specifies how to deal with missing values
***
## The `formula` Argument
We can write our models using the following syntax:
```{r echo=TRUE, eval=FALSE}
model <- formula(regressand ~ regressors)
```
Where `regressand` is just our dependent variable / response usually denoted by $y$ and `model` is our formula of independent variables / regressors, e.g.:
```{r echo=TRUE, eval=FALSE}
kicker_success_formula <- formula(kicker_success ~ experience + training + luck)
```
***
We can construct formulas with the following syntax:
- Adding variables with `+`
```{r eval=FALSE, echo = TRUE}
formula(y ~ a + b)
```
- Interactions with `:`
```{r eval=FALSE, echo = TRUE}
formula(y ~ a + b + a:b)
```
- Crossing: `a * b ` is equivalent to `a + b + a:b`
```{r eval=FALSE, echo = TRUE}
formula(y ~ a + b + a:b) # and
formula(y ~ a * b) # are equivalent
```
- Transformations with `I()`
```{r eval=FALSE, echo = TRUE}
formula(y ~ a + I(a ^ 2)) # quadratic term must be in I() to evaluate correctly
formula(y ~ log(a)) # log can stay by itself
```
- Include all variables in your data with `.`
```{r eval=FALSE, echo = TRUE}
formula(y ~ .) # is equivalent to
formula(y ~ a + b + ... + z) # for a dataset with variables from a to z
```
***
## The `subset` Argument
- Sometimes, we want to run our model on a subset of our data (without changing the data themselves)
- We can specify subsets of certain variables as follows:
```{r echo=TRUE, eval=FALSE}
lm(formula,
data,
subset = age < 30)
```
- Connect multiple subset arguments with logical operators:
```{r echo=TRUE, eval=FALSE}
lm(formula,
data,
subset = age < 30 & height > 180)
```
Note that although this works, a best practice is to subset your data prior to the estimation. By keeping these steps distinct, your code will be much easier for someone else to understand.
***
## The `na.action` Argument
If the data contains missing values, `lm` automatically deletes the whole observation.
- Specify `na.action = na.fail` if you want an error when the data contains missing values
Again, it is a best practice to look for missing values in your data prior to the estimation to keep your code transparent.
- You can use the `missmap` function from the `Amelia` package to get a nice visualisation of missing values in your data
***
## Example Call of `lm` with Eurostat Data
```{r echo=TRUE}
eur_data <- read.csv2("data/eurostat_data.csv")
m1 <-
formula(unemp_workagepop_t ~ gdp_gr + inv_per_empl + immigration_t,
subset = year == 2014)
model <- lm(formula = m1,
data = eur_data)
```
***
## Output of `lm`
The `lm` function returns a list. Relevant components of this list are:
- `call` - the function call that generated the output
- `coefficients` the OLS coefficients
- `residuals`
- `fitted.values` The estimates for our dependent variable (unemployment)
- `model` The model matrix used for estimation
The full list of outputs can be looked up via
- `?lm()`
- `str(model)` where model is our saved output from `lm`
- the `$` operator and `tab`, e.g. `model$...`
Lets look up our coefficients $\beta$, fitted values $\hat{y}$ and OLS residuals $\varepsilon$
```{r echo=TRUE}
model$coefficients
```
```{r echo=TRUE}
model$fitted.values[1:7] # first 7 fitted values
```
```{r echo=TRUE}
model$residuals[1:7] # first 7 residuals
```
We can visualise the results very simply with `hist` or `plot`:
```{r echo=TRUE, fig.width=10}
hist(model$residuals, breaks = 30)
```
```{r echo=TRUE, fig.width=10}
hist(model$fitted.values, breaks = 30)
```
***
## Output of `lm` with the `summary()` function
```{r}
summary(model)
```
***
## Display and Export Tables with `stargazer()`
```{r echo=TRUE, warning=FALSE}
stargazer::stargazer(model, type = "text")
```
Jake Russ has the [ultimate overview](https://www.jakeruss.com/cheatsheets/stargazer/) over all `stargazer()` functions (there are many!!).
***
### Export Stargazer Output to File
```{r, echo=TRUE, eval = FALSE}
stargazer::stargazer(model,
type = "html",
out = "model.html")
```
***
## Compare different Models
```{r echo=TRUE}
model2 <- lm(unemp_workagepop_t ~ gdp_gr,
data = eur_data)
```
```{r, results="asis"}
stargazer::stargazer(model, model2,
type = "html")
```
Specify the folder and file were your table should be saved as `"path/name.type"`
1. Output as `.html` : Open the file in your web browser and copy it into Word
2. Output as `.tex` : Include in LaTeX
***
# Panel Regression
Just a very quick glimpse into panel regressions with plm.
```{r}
library(plm)
panel_object <- plm(
unemp_workagepop_t ~ gdp_gr,
data = eur_data,
index = c("geo_code", "time"),
model = "within",
effect = "twoways"
) # for two-ways fixed effect regressions
```
For further information on panel regressions, I recommend
* the [slides](https://www.princeton.edu/~otorres/Panel101R.pdf) by Oscar Torres-Reyna
* the [Chapter 13](http://www.urfie.net/read/mobile/index.html#p=208) from the Econometrics with R book