-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.qmd
519 lines (431 loc) · 12.1 KB
/
transform.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
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
---
title: "Thesis - Tranform Variables"
toc: true
number-sections: true
code-fold: true
warning: false
output: false
error: true
format: gfm
editor_options:
markdown:
wrap: sentence
---
# Load libraries
```{r}
library(tidyverse)
```
# Select and name all relevant variables
```{r}
df <- raw_df %>%
select(
name = 1,
muni_id,
district = 3,
type = 4,
distance_ta = 5,
pop = 13,
jew_pct = 14,
arab_pct = 16,
muslim_pct = 17,
christ_pct = 18,
druze_pct = 19,
age_0_4_pct = 22,
age_5_9_pct = 23,
age_10_14_pct = 24,
age_15_19_pct = 25,
age_20_29_pct = 26,
age_30_44_pct = 27,
age_45_59_pct = 28,
age_60_64_pct = 29,
age_65_plus_pct = 30,
age_0_17_pct = 31,
age_75_plus_pct = 32,
dep_ratio = 33,
immig_1990_pct = 41,
unemp_allowance_pct = 87,
income_wage = 116,
wage_num = 122,
min_wage_pct = 123,
freelance_num = 124,
income_freelance = 125,
bagrut_pct = 154,
bagrut_uni_pct = 155,
high_educ_35_55_pct = 156,
ses_c_2015 = 231,
ses_i_2015 = 232,
ses_r_2015 = 233,
peri_c_2015 = 237,
peri_i_2015 = 238,
peri_r_2015 = 239,
last_col(19):last_col(0)
)
```
# Transform types and problematic values of variables
## Change relevant charachter variables to numeric
```{r}
df <- df %>%
mutate(
across(
c(
distance_ta,
jew_pct,
arab_pct,
muslim_pct,
christ_pct,
druze_pct,
immig_1990_pct,
starts_with("ses"),
starts_with("peri")
),
as.numeric
)
)
# Check which variables have NA's after the coercion to numeric variable
check <- df %>%
summarise(
across(
everything(),
~sum(is.na(.))
)
)
```
## Replace Na's with 0's
except for distance from Tel Aviv, which is NA for regional councils
```{r}
df <- df %>%
mutate(
across(
c(
jew_pct,
arab_pct,
muslim_pct,
christ_pct,
druze_pct,
immig_1990_pct,
likud_pct,
coal_pct,
pot_votes
),
replace_na, 0
)
)
```
## Create factor variables
```{r}
df <- df %>%
mutate(
across(
c(
district,
type
),
as_factor
)
)
```
## Round relevant numeric variables
```{r}
df <- df %>%
mutate(
across(
c(
distance_ta,
dep_ratio,
income_wage,
income_freelance,
ends_with("pct")
),
round, 1
)
)
```
# Explore distribution of variables
## District
```{r}
df %>%
ggplot(aes(district)) +
geom_bar() +
geom_text(aes(label = ..count..), stat = "count", vjust = 1.5, colour = "white")
```
## type
```{r}
df %>%
ggplot(aes(type)) +
geom_bar() +
geom_text(aes(label = ..count..), stat = "count", vjust = 1.5, colour = "white")
```
## Population (pop)
```{r}
df %>%
ggplot(aes(pop)) +
geom_histogram(aes(y = ..density..)) +
geom_density()
```
The population variable is right-skewed.
Let's exaimne if a log10 transformation would help.
```{r}
df %>%
ggplot(aes(log10(pop))) +
geom_histogram(aes(y = ..density..)) +
geom_density()
```
As seen, the population variable is right-skewed.
after a log transformation, the variable is more normally distributed.
## Sector (Jewish/Arab/Mixed)
```{r}
df %>%
ggplot(aes(jew_pct)) +
geom_histogram(aes(y = ..density..)) +
geom_density()
```
As seen, the distribution of the jewish population has 3 modes, and therefore we will create a new categorical variable for sector.
Currently the variable is defined as "Arab" if there are more than 50% Arabs, "Mixed" if there are between 10%-50% Arabs, and "Jewish" otherwise.
I consider making a "special sector" variable of some sort, to also include Haredi and Druze.
Should it be another variable or added to the sector variable?
```{r}
df <- df %>%
mutate(
sector = as_factor(case_when(
arab_pct > 50 ~ "arab",
arab_pct > 10 ~ "mixed",
TRUE ~ "jewish"
))
)
df %>%
ggplot(aes(sector)) +
geom_bar() +
geom_text(aes(label = ..count..), stat = "count", vjust = 1.5, colour = "white")
```
## Age distribution
```{r}
df %>%
ggplot(aes(age_65_plus_pct)) +
geom_histogram(aes(y = ..density..)) +
geom_density()
df %>%
ggplot(aes(age_0_17_pct)) +
geom_histogram(aes(y = ..density..)) +
geom_density()
```
It is not clear which one is better, but they are both somewhat normally distributed with some right-skewness.
Let's check if sector has an impact on the distribution.
```{r}
df %>%
pivot_longer(c(age_0_17_pct, age_65_plus_pct), names_to = "var", values_to = "value") %>%
ggplot(aes(value)) +
geom_histogram(aes(y = ..density..)) +
geom_density() +
facet_grid(sector ~ var)
```
It seems like the distribution is not too much effected by sector.
No need to give it a special treatment.
## Dependance ratio
```{r}
df %>%
ggplot(aes(dep_ratio)) +
geom_histogram(aes(y = ..density..)) +
geom_density()
```
The dependence ratio variable is right-skewed and it is possible to perform a log10 transformation, but as of now it does not seem to add more useful information to the age distribution variables, so no need for transformation.
## Percent of immigrants that came to Israel after 1990 from the population
```{r}
df %>%
ggplot(aes(immig_1990_pct)) +
geom_histogram(aes(y = ..density..)) +
geom_density()
```
It seems that the variable is right-skewed, but it is probably due to the fact that Arab municipalities don't have lots of immigrants.
Let's check this hypothesis.
```{r}
df %>%
ggplot(aes(immig_1990_pct)) +
geom_histogram(aes(y = ..density..)) +
geom_density() +
facet_wrap(~ sector, scales = "free_y")
```
First, it seems that indeed Arab municipalities don't have immigrants.
This makes it a good candidate for interaction.
Second, the variable is still right-skewed, even only for Jewish municipalities.
Let's try log10 transformation.
```{r}
df %>%
ggplot(aes(log10(immig_1990_pct))) +
geom_histogram(aes(y = ..density..)) +
geom_density() +
facet_wrap(~ sector, scales = "free_y")
```
Since a lot of Arab municipalities don't have immigrants at all, the variable is not a good candidate for using it in the model in itself.
We will use a log10 transformation but only with interaction with sector variable.
## Unemployment Allowance Percent
```{r}
df %>%
ggplot(aes(unemp_allowance_pct)) +
geom_histogram(aes(y = ..density..)) +
geom_density()
df %>%
arrange(desc(unemp_allowance_pct)) %>%
select(name, unemp_allowance_pct)
```
This does not seem like a good variable to determine true unemployment, for none of the top municipalities have Arab or Haredi municipalities.
## Income
```{r}
df %>%
pivot_longer(c(income_wage, income_freelance, min_wage_pct), names_to = "var", values_to = "value") %>%
ggplot(aes(value)) +
geom_histogram(aes(y = ..density..)) +
geom_density() +
facet_wrap(~ var, scales = "free")
```
It seems that the percent earning under minimum wage is pretty normally distributed, but average income of wage workers is right-skewed.
The freelance income is less relevant for the much lower number of freelancers.
Let's try log10 transformation on wage income.
```{r}
df %>%
ggplot(aes(log10(income_wage))) +
geom_histogram(aes(y = ..density..)) +
geom_density()
```
This is much better, a log10 transformation is needed for wage income.
## Education
Let's begin with checking correlation between the three education variables
```{r}
df %>%
select(bagrut_pct, bagrut_uni_pct, high_educ_35_55_pct) %>%
cor()
```
It seems that the Bagrut variables are highly correlated, but less so with the higher education variable.
Let's check it visually.
```{r}
df %>%
ggplot(aes(bagrut_pct, bagrut_uni_pct)) +
geom_point()
df %>%
ggplot(aes(bagrut_pct, high_educ_35_55_pct)) +
geom_point()
df %>%
ggplot(aes(bagrut_uni_pct, high_educ_35_55_pct)) +
geom_point()
```
The charts reaffirm this understanding.
LEt's examine the distribution of these variables.
```{r}
df %>%
pivot_longer(c(bagrut_pct, bagrut_uni_pct, high_educ_35_55_pct), names_to = "var", values_to = "value") %>%
ggplot(aes(value)) +
geom_histogram(aes(y = ..density..)) +
geom_density() +
facet_wrap(~ var, scales = "free")
```
The bagrut variables are left-skewed while the higher education variable is more bimodal and somewhat evenly distributed.
We will opt to use the higher education variable.
## CBS clusters and indexes
```{r}
df %>%
pivot_longer(c(starts_with("ses"), starts_with("peri")), names_to = "var", values_to = "value") %>%
ggplot(aes(value)) +
geom_histogram(aes(y = ..density..)) +
geom_density() +
facet_wrap(~ var, scales = "free")
```
Obviously, the rank variable is evenly distributed by definition.
We will use the index variable as it is normally distributed and carries the most information with it.
Let's treat some missing values in the periphery data of 6 municipalities. They are municipalities that did not exist in 2004. We will replace their missing values with the 2015 periphery data.
```{r}
na_peri_muni <- c(
"0483", # Bueina
"0628", # Jat
"0490", # Dir ElAssad
"0534", # Osfia
"69", # AlQasum
"68" # Neve Midbar
)
replace_match_na <- function(x, id, na_vec, replace_with) {
case_match(
{{ id }},
na_vec ~ {{ replace_with }},
.default = x
)
}
df <- df %>%
mutate(
peri_2004_i = replace_match_na(peri_2004_i, muni_id, na_peri_muni, peri_i_2015),
peri_2004_r = replace_match_na(peri_2004_r, muni_id, na_peri_muni, peri_r_2015),
peri_2004_c = replace_match_na(peri_2004_c, muni_id, na_peri_muni, peri_c_2015)
)
```
## Voting
```{r}
df %>%
pivot_longer(c(likud_pct, coal_pct), names_to = "var", values_to = "value") %>%
ggplot(aes(value)) +
geom_histogram(aes(y = ..density..)) +
geom_density() +
facet_wrap(~ var, scales = "free")
```
It seems that both voting variables are left skewed.
this might be because of interaction with sector variable.
Let's visualize this.
```{r}
df %>%
pivot_longer(c(likud_pct, coal_pct), names_to = "var", values_to = "value") %>%
ggplot(aes(value)) +
geom_histogram(aes(y = ..density..)) +
geom_density() +
facet_grid(var ~ sector, scales = "free")
```
It seems that indeed the voting variables are almost always very low in the Arab sector.
Also, this suggests that the "mixed" value of the sector variable should be omitted if we check interaction in the model to prevent overfitting.
This is because There are only 13 mixed municipalities.
Again, it is possible to create a "special sector" variable alongside Druze and Haredi.
## Budget
The budget variables would probably be right-skewed, as they are very much affected by the size of the population.
```{r}
df %>%
pivot_longer(starts_with("budget"), names_to = "var", values_to = "value") %>%
ggplot(aes(value)) +
geom_histogram(aes(y = ..density..)) +
geom_density() +
facet_wrap(~ var)
```
This indeed seems like right-skewness.
Let's try to divide the budget by population.
```{r}
df %>%
pivot_longer(starts_with("budget"), names_to = "var", values_to = "value") %>%
ggplot(aes(value / pop)) +
geom_histogram(aes(y = ..density..)) +
geom_density() +
facet_wrap(~ var)
```
After normalizing by population size the distribution is even more right-skewed.
Let's try a log10 transformation.
```{r}
df %>%
pivot_longer(starts_with("budget"), names_to = "var", values_to = "value") %>%
ggplot(aes(log10(1 + value / pop))) +
geom_histogram(aes(y = ..density..)) +
geom_density() +
facet_wrap(~ var)
```
This is much better and the seemingly best transformation to use.
important to note that 1 was added to the calculation to prevent log of 0.
Nevertheless, we will still keep the original budget_approved variable for comparison.
# Transform variables
```{r}
df <- df %>%
mutate(
pop_log10 = log10(pop),
immig_1990_pct_log10 = case_when(
immig_1990_pct == 0 ~ 0,
TRUE ~ log10(immig_1990_pct)
),
income_wage_log10 = log10(income_wage),
budget_approved_capita_log10 = log10(1 + budget_approved / pop),
sector = as_factor(case_when(
arab_pct > 50 ~ "arab",
TRUE ~ "jewish"
)),
pop_2015 = pop_2015 * 1000
)
```