-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-Models.qmd
337 lines (260 loc) · 6.65 KB
/
3-Models.qmd
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
# Regression
```{r}
library(wiesbaden)
library(tidyverse)
```
Wiesbaden package to access data from the Genesis database from Statistisches Bundesamt (located in Wiesbaden).
The Credentials to access the API are saved already, test login follows here.
```{r}
test_login(genesis=c(db="regio")) #test login
```
```{r}
wahl_lohn <- readRDS("data/wahl_lohn.Rds")
```
## Simple Regression
$$
\text{afd}_k = \beta_0 + \beta_1 \times \text{min}_k + \epsilon
$$
- $\text{afd}_k$ AfD Vote Share in Landkreis $k$
- $\text{min}_k$ Share of Minimum Wage Receivers in Landkreis $k$
- $\beta_0$ Intercept
- $\beta_1$ Slope
- $\epsilon$ Error
```{r}
model_basic <- lm(
afd_prozent ~ lohn_prozent,
data = wahl_lohn
)
summary(model_basic)
```
## Ost West
```{r}
länder <- wahl_lohn$land %>% unique()
ost_names <- c("BB", "BE", "MV", "SN", "ST", "TH")
wahl_lohn <- wahl_lohn %>%
mutate(
ost = ifelse(land %in% ost_names, 1, 0)
)
```
Dummy for ost
```{r}
model_ost <- lm(
afd_prozent ~ lohn_prozent + ost,
data = wahl_lohn
)
summary(model_ost)
```
## Unemployment Rate
from Regionalstatistik [13211-02-05-4](https://www.regionalstatistik.de/genesis//online?operation=table&code=13211-02-05-4&bypass=true&levelindex=0&levelid=1704816689633#abreadcrumb)
```{r}
d <- retrieve_datalist(tableseries="13211*", genesis=c(db="regio"))
subset(d, grepl("Kreise", description))
```
```{r}
#| output: false
data <- retrieve_data(tablename="13211KJ009", genesis=c(db="regio"))
head(data)
```
```{r}
arbeit_data <- data %>%
filter(JAHR == 2021) %>%
select(kreis = KREISE, arbeitslosenquote = ERWP10_val) %>%
# remove rows with more than 5 digits in kreis
mutate(arbeitslosenquote = arbeitslosenquote / 100) %>%
filter(nchar(kreis) == 5)
```
Merge
```{r}
wahl_lohn <- wahl_lohn %>%
left_join(arbeit_data, by = "kreis")
```
```{r}
model_arbeit <- lm(
afd_prozent ~ lohn_prozent + ost+ arbeitslosenquote,
data = wahl_lohn
)
summary(model_arbeit)
```
## GDP per Capita
Regionalstatistik [82111](https://www.regionalstatistik.de/genesis//online?operation=table&code=82111-01-05-4&bypass=true&levelindex=0&levelid=1704893343488#abreadcrumb)
```{r}
d <- retrieve_datalist(tableseries="82111*", genesis=c(db="regio"))
subset(d, grepl("Kreise", description))
```
```{r}
#| output: false
data <- retrieve_data(tablename="82111KJ008", genesis=c(db="regio"))
head(data)
```
```{r}
gdp_data <- data %>%
filter(JAHR == 2021) %>%
select(kreis = KREISE, gdp = BIP804_val) %>%
# remove rows with more than 5 digits in kreis
filter(nchar(kreis) == 5)
```
```{r}
gdp_data %>%
filter(!kreis %in% wahl_lohn$kreis) %>%
select(kreis) %>%
arrange(kreis)
```
16056 (Eisenach) not present in Wahl_Lohn Data..., because Eisenach Kreis doesnt exist anymore, it is now Wartburgkreis (16063)
Merge
```{r}
wahl_lohn <- wahl_lohn %>%
left_join(gdp_data, by = "kreis")
```
Model
```{r}
model_gdp <- lm(
afd_prozent ~ lohn_prozent + ost+ arbeitslosenquote + log(gdp),
data = wahl_lohn
)
summary(model_gdp)
```
## Avg. Age
```{r}
d <- retrieve_datalist(tableseries="12411*", genesis=c(db="regio"))
subset(d, grepl("Kreise", description))
```
```{r}
#| output: false
data <- retrieve_data(tablename="12411KJ019", genesis=c(db="regio"))
head(data)
```
```{r}
age_data <- data %>%
filter(STAG == "31.12.2021") %>%
select(kreis = KREISE,
age = BEV519_val #avg age
) %>%
# remove rows with more than 5 digits in kreis
filter(nchar(kreis) == 5)
```
Merge
```{r}
wahl_lohn <- wahl_lohn %>%
left_join(age_data, by = "kreis")
```
```{r}
model_age <- lm(
afd_prozent ~ lohn_prozent + ost+ arbeitslosenquote + log(gdp) + age,
data = wahl_lohn
)
summary(model_age)
```
## Population Density
Regionalstatistik [99910](https://www.regionalstatistik.de/genesis//online?operation=table&code=AI002-1-5&bypass=true&levelindex=0&levelid=1704894307140#abreadcrumb)
```{r}
d <- retrieve_datalist(tableseries="99910*", genesis=c(db="regio"))
subset(d, grepl("Bevölkerung", description))
```
```{r}
#| output: false
data <- retrieve_data(tablename="99910KJA02", genesis=c(db="regio"))
head(data)
```
```{r}
pop_data <- data %>%
filter(JAHR == 2021) %>%
select(kreis = KREISE,
pop = AI0201_val, #population density je km2
foreigners = AI0208_val #foreigners rate in %
) %>%
mutate(foreigners = foreigners / 100) %>%
# remove rows with more than 5 digits in kreis
filter(nchar(kreis) == 5)
```
Merge
```{r}
wahl_lohn <- wahl_lohn %>%
left_join(pop_data, by = "kreis")
```
<!-- somehow changes the model...
```{r}
model_foreign <- lm(
afd_prozent ~ lohn_prozent + ost+ arbeitslosenquote + log(gdp)+ age ,
data = wahl_lohn
)
summary(model_foreign)
```
-->
```{r}
model_pop <- lm(
afd_prozent ~ lohn_prozent + ost+ arbeitslosenquote + log(gdp) +age + log(pop),
data = wahl_lohn
)
summary(model_pop)
```
<!--
## Dropping Variables
```{r}
model_drop <- lm(
afd_prozent ~ ost+ arbeitslosenquote + log(gdp) +age+ foreigners + log(pop),
data = wahl_lohn
)
summary(model_drop)
```
hust, lets just ignore that...:)
-->
## Choosing the best Model
via Stepwise Regression, explained [here](https://www.statology.org/stepwise-regression-r/)
not well regarded in Econometrics, because it is prone to overfitting! just for self reference, that lohn_prozent is chosen and therefore valid
```{r}
step(model_basic, direction = "both", scope = ~ lohn_prozent + ost + foreigners+ arbeitslosenquote + log(gdp) + age, trace=0)
```
Seems, best model is: "afd_prozent \~ lohn_prozent + ost + age + arbeitslosenquote + foreigners"
## Interlude: Linke
```{r}
model_linke <- lm(
linke_prozent ~ lohn_prozent,
data = wahl_lohn
)
model_linke_2 <- lm(
linke_prozent ~ lohn_prozent + ost,
data = wahl_lohn
)
model_linke_3 <- lm(
linke_prozent ~ lohn_prozent + ost + arbeitslosenquote,
data = wahl_lohn
)
model_linke_4 <- lm(
linke_prozent ~ lohn_prozent + ost + arbeitslosenquote + log(gdp),
data = wahl_lohn
)
model_linke_5 <- lm(
linke_prozent ~ lohn_prozent + ost + arbeitslosenquote + log(gdp) + age,
data = wahl_lohn
)
model_linke_6 <- lm(
linke_prozent ~ lohn_prozent + ost + arbeitslosenquote + log(gdp) + age + log(pop),
data = wahl_lohn
)
summary(model_linke_6)
```
=\> not significant in all variants, low R2 in basic variant
## Save Data
```{r}
#save all Models
save(
model_basic,
model_ost,
model_arbeit,
model_gdp,
model_age,
model_pop,
file = "data/models.RData")
save(
model_linke,
model_linke_2,
model_linke_3,
model_linke_4,
model_linke_5,
model_linke_6,
file = "data/models_linke.RData"
)
```
```{r}
saveRDS(wahl_lohn, "data/wahl_lohn_mod.RDS")
```