-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipl12_sk_01.15.21.R
897 lines (727 loc) · 40.2 KB
/
ipl12_sk_01.15.21.R
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
## ----setup, include=FALSE---------------------------------------------------------------------------------------------------------------------------------
knitr::opts_chunk$set(message = FALSE, warning = FALSE, echo = FALSE, fig.retina = 4)
knitr::opts_chunk$set(fig.pos = 'H')
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
# Libraries
pacman::p_load(tidyverse, readxl, janitor, lubridate, extrafont, hrbrthemes, sf, ggcorrplot, plotly, ggradar, estimatr, texreg, estimatr)
extrafont::loadfonts()
# Parameters
df_baseline <-
read_csv(here::here("data", "Rapid_response_baseline_cleaned_all_consent.csv")) %>%
inner_join(read_csv(here::here("data", "weights.csv")), by = "study_ID") %>%
filter(yearArrive >= 2010) %>%
mutate(
years_in_us = 2020 - yearArrive,
age_at_arrival = yearArrive - birthYear,
female = recode(gender, "Male" = "0", "Female" = "1", .default = NA_character_) %>% as.character %>% parse_integer(),
unemploymentBeneBaseline = if_else(unemploymentBeneBaseline == "Yes", "1", unemploymentBeneBaseline),
unemploymentBeneBaseline = if_else(unemploymentBeneBaseline == "No", "0", unemploymentBeneBaseline),
unemployment = unemploymentBeneBaseline %>% as.integer()
)
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
# extracting data for dlaitin on box
# df_baseline %>%
# select(-contains(c("name", "numb", "phone", "cell", "mobile", "mail", "contact", "address"))) %>%
# relocate(ResponseID, ResponseSet, weight) %>%
# write_csv(here::here("data", "baseline_data_with_weights_de-identified_11.03.2020.csv"))
#df_baseline %>% select(contains("12"))
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
select(contains("12s")) %>%
summarize_all(~ mean(., na.rm = T)) %>%
rename_all(~ str_c(., "_mean")) %>%
relocate(ipl12s_mean)
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
select(contains("12s"), weight) %>%
summarize_at(vars(contains("12s")), ~ weighted.mean(., w = weight, na.rm = T)) %>%
rename_all(~ str_c(., "_mean")) %>%
relocate(ipl12s_mean)
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
select(contains("12s")) %>%
summarize_all(~ median(., na.rm = T)) %>%
rename_all(~ str_c(., "_median")) %>%
relocate(ipl12s_median)
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
select(contains("12s")) %>%
summarize_all(~ sd(., na.rm = T)) %>%
rename_all(~ str_c(., "_sd")) %>%
relocate(ipl12s_sd)
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
df_baseline %>% count(female)
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
mutate(
read = case_when(
read == "Very well" ~ 5,
read == "Well" ~ 4,
read == "Moderately well" ~ 3,
read == "Not well" ~ 2,
read == "Not well at all" ~ 1
),
speak = case_when(
speak == "Very well" ~ 5,
speak == "Well" ~ 4,
speak == "Moderately well" ~ 3,
speak == "Not well" ~ 2,
speak == "Not well at all" ~ 1
),
citizen = recode(citizen, "No" = "0", "Yes" = "1", default = NA_character_) %>% as.integer(),
cosponsorship = recode(cosponsorship, "No" = "0", "Yes" = "1", default = NA_character_) %>% as.integer()
) %>%
drop_na(female) %>%
group_by(female) %>%
summarise_at(
vars(years_in_us, citizen, age, age_at_arrival, houseHoldIncome, unemployment, cosponsorship, householdChildren, householdSize, educationLevelNumeric, contains("12s")),
~ weighted.mean(., weight, na.rm = T)
) %>%
pivot_longer(-female) %>%
pivot_wider(names_from = female, values_from = value) %>%
select(variable = name, male = `0`, female = `1`) %>%
mutate_at(vars(contains("ale")), ~ round(., 4))
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
ggplot(aes(ipl12s, stat(density), weight = weight)) +
geom_histogram(color = "black", fill = "grey") +
geom_density() +
theme_ipsum()
## ---- out.width = "100%"----------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
pivot_longer(
cols = contains("12s"),
names_to = "measure",
values_to = "score"
) %>%
filter(measure != "ipl12s") %>%
ggplot(aes(score, weight = weight)) +
geom_density(color = "black", fill = "grey", alpha = 0.5) +
facet_wrap(vars(measure)) +
theme_ipsum()
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
x <- df_baseline %>% select(contains("12s"))
ggcorrplot(cor(x, use = "pairwise.complete.obs"), hc.order = TRUE, type = "lower", p.mat = cor_pmat(x), insig = "blank", lab = TRUE)
rm(x)
## ---- out.width = "100%"----------------------------------------------------------------------------------------------------------------------------------
plot <-
ussf::boundaries(geography = "state") %>%
select(stateCurrent = NAME) %>%
left_join(
df_baseline %>%
mutate(
stateCurrent = if_else(stateCurrent == "Refused", NA_character_, stateCurrent),
stateCurrent = if_else(stateCurrent == "I do not reside in the United States", NA_character_, stateCurrent)
) %>%
group_by(stateCurrent) %>%
summarize(
ipl12s = mean(ipl12s, na.rm = T) %>% round(2),
weight = mean(weight, na.rm = T)
),
by = "stateCurrent"
) %>%
ggplot(aes(fill = ipl12s, weight = weight, text = stateCurrent)) +
geom_sf(size = 0.3, show.legend = F) +
scale_fill_viridis_c() +
theme_minimal() +
theme(legend.position = "bottom")
ggplotly(plot, tooltip = c("stateCurrent", "ipl12s")) %>%
layout(xaxis = list(autorange = TRUE), yaxis = list(autorange = TRUE))
## ---- out.width = "100%"----------------------------------------------------------------------------------------------------------------------------------
plot <-
ussf::boundaries(geography = "state") %>%
select(stateCurrent = NAME) %>%
left_join(
df_baseline %>%
mutate(
stateCurrent = if_else(stateCurrent == "Refused", NA_character_, stateCurrent),
stateCurrent = if_else(stateCurrent == "I do not reside in the United States", NA_character_, stateCurrent)
) %>%
group_by(stateCurrent) %>%
summarize(
ling12s = mean(ling12s, na.rm = T) %>% round(2),
weight = mean(weight, na.rm = T)
),
by = "stateCurrent"
) %>%
ggplot(aes(fill = ling12s, weight = weight, text = stateCurrent)) +
geom_sf(size = 0.3, show.legend = F) +
scale_fill_viridis_c() +
theme_minimal() +
theme(legend.position = "bottom")
ggplotly(plot, tooltip = c("stateCurrent", "ling12s")) %>%
layout(xaxis = list(autorange = TRUE), yaxis = list(autorange = TRUE))
## ---- out.width = "100%"----------------------------------------------------------------------------------------------------------------------------------
plot <-
ussf::boundaries(geography = "state") %>%
select(stateCurrent = NAME) %>%
left_join(
df_baseline %>%
mutate(
stateCurrent = if_else(stateCurrent == "Refused", NA_character_, stateCurrent),
stateCurrent = if_else(stateCurrent == "I do not reside in the United States", NA_character_, stateCurrent)
) %>%
group_by(stateCurrent) %>%
summarize(
pol12s = mean(pol12s, na.rm = T) %>% round(2),
weight = mean(weight, na.rm = T)
),
by = "stateCurrent"
) %>%
ggplot(aes(fill = pol12s, weight = weight, text = stateCurrent)) +
geom_sf(size = 0.3, show.legend = F) +
scale_fill_viridis_c() +
theme_minimal() +
theme(legend.position = "bottom")
ggplotly(plot, tooltip = c("stateCurrent", "pol12s")) %>%
layout(xaxis = list(autorange = TRUE), yaxis = list(autorange = TRUE))
## ---- out.width = "100%"----------------------------------------------------------------------------------------------------------------------------------
plot <-
ussf::boundaries(geography = "state") %>%
select(stateCurrent = NAME) %>%
left_join(
df_baseline %>%
mutate(
stateCurrent = if_else(stateCurrent == "Refused", NA_character_, stateCurrent),
stateCurrent = if_else(stateCurrent == "I do not reside in the United States", NA_character_, stateCurrent)
) %>%
group_by(stateCurrent) %>%
summarize(
soc12s = mean(soc12s, na.rm = T) %>% round(2),
weight = mean(weight, na.rm = T)
),
by = "stateCurrent"
) %>%
ggplot(aes(fill = soc12s, weight = weight, text = stateCurrent)) +
geom_sf(size = 0.3, show.legend = F) +
scale_fill_viridis_c() +
theme_minimal() +
theme(legend.position = "bottom")
ggplotly(plot, tooltip = c("stateCurrent", "soc12s")) %>%
layout(xaxis = list(autorange = TRUE), yaxis = list(autorange = TRUE))
## ---- out.width = "100%"----------------------------------------------------------------------------------------------------------------------------------
plot <-
ussf::boundaries(geography = "state") %>%
select(stateCurrent = NAME) %>%
left_join(
df_baseline %>%
mutate(
stateCurrent = if_else(stateCurrent == "Refused", NA_character_, stateCurrent),
stateCurrent = if_else(stateCurrent == "I do not reside in the United States", NA_character_, stateCurrent)
) %>%
group_by(stateCurrent) %>%
summarize(
econ12s = mean(econ12s, na.rm = T) %>% round(2),
weight = mean(weight, na.rm = T)
),
by = "stateCurrent"
) %>%
ggplot(aes(fill = econ12s, weight = weight, text = stateCurrent)) +
geom_sf(size = 0.3, show.legend = F) +
scale_fill_viridis_c() +
theme_minimal() +
theme(legend.position = "bottom")
ggplotly(plot, tooltip = c("stateCurrent", "econ12s")) %>%
layout(xaxis = list(autorange = TRUE), yaxis = list(autorange = TRUE))
## ---- out.width = "100%"----------------------------------------------------------------------------------------------------------------------------------
plot <-
ussf::boundaries(geography = "state") %>%
select(stateCurrent = NAME) %>%
left_join(
df_baseline %>%
mutate(
stateCurrent = if_else(stateCurrent == "Refused", NA_character_, stateCurrent),
stateCurrent = if_else(stateCurrent == "I do not reside in the United States", NA_character_, stateCurrent)
) %>%
group_by(stateCurrent) %>%
summarize(
psy12s = mean(psy12s, na.rm = T) %>% round(2),
weight = mean(weight, na.rm = T)
),
by = "stateCurrent"
) %>%
ggplot(aes(fill = psy12s, weight = weight, text = stateCurrent)) +
geom_sf(size = 0.3, show.legend = F) +
scale_fill_viridis_c() +
theme_minimal() +
theme(legend.position = "bottom")
ggplotly(plot, tooltip = c("stateCurrent", "psy12s")) %>%
layout(xaxis = list(autorange = TRUE), yaxis = list(autorange = TRUE))
## ---- out.width = "100%"----------------------------------------------------------------------------------------------------------------------------------
plot <-
ussf::boundaries(geography = "state") %>%
select(stateCurrent = NAME) %>%
left_join(
df_baseline %>%
mutate(
stateCurrent = if_else(stateCurrent == "Refused", NA_character_, stateCurrent),
stateCurrent = if_else(stateCurrent == "I do not reside in the United States", NA_character_, stateCurrent)
) %>%
group_by(stateCurrent) %>%
summarize(
nav12s = mean(nav12s, na.rm = T) %>% round(2),
weight = mean(weight, na.rm = T)
),
by = "stateCurrent"
) %>%
ggplot(aes(fill = nav12s, weight = weight, text = stateCurrent)) +
geom_sf(size = 0.3, show.legend = F) +
scale_fill_viridis_c() +
theme_minimal() +
theme(legend.position = "bottom")
ggplotly(plot, tooltip = c("stateCurrent", "nav12s")) %>%
layout(xaxis = list(autorange = TRUE), yaxis = list(autorange = TRUE))
## ---- eval=F----------------------------------------------------------------------------------------------------------------------------------------------
## df_baseline %>% select(contains("age"))
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
group_by(years_in_us) %>%
summarise(
ipl12s = mean(ipl12s, na.rm = TRUE),
weight = mean(weight, na.rm = TRUE)
) %>%
drop_na() %>%
filter(years_in_us <= 10) %>%
ggplot(aes(years_in_us, ipl12s, weight = weight)) +
geom_line() +
geom_point() +
scale_x_continuous(breaks = seq(0, 10, 1)) +
theme_ipsum()
## ---- out.width="100%"------------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
group_by(years_in_us) %>%
summarise_at(vars(contains("12s"), weight), mean, na.rm = T) %>%
drop_na() %>%
arrange(ipl12s) %>%
pivot_longer(
cols = contains("12s"),
names_to = "measure",
values_to = "score"
) %>%
filter(measure != "ipl12s") %>%
drop_na(years_in_us) %>%
filter(years_in_us <= 10) %>%
ggplot(aes(years_in_us, score, weight = weight)) +
geom_line() +
geom_point() +
scale_x_continuous(breaks = seq(0, 10, 1)) +
theme_ipsum() +
facet_wrap(vars(measure))
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
group_by(female) %>%
summarise(
ipl12s = mean(ipl12s, na.rm = TRUE),
weight = mean(weight, na.rm = TRUE)
) %>%
drop_na() %>%
ggplot(aes(factor(female), ipl12s, weight = weight)) +
geom_col() +
theme_ipsum()
## ---- out.width="100%"------------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
group_by(female) %>%
summarise_at(vars(contains("12s"), weight), mean, na.rm = T) %>%
drop_na() %>%
arrange(ipl12s) %>%
mutate(female = fct_inorder(factor(female), ipl12s) %>% fct_rev()) %>%
pivot_longer(
cols = contains("12s"),
names_to = "measure",
values_to = "score"
) %>%
filter(measure != "ipl12s") %>%
ggplot(aes(factor(female), score, weight = weight)) +
geom_col() +
facet_wrap(vars(measure)) +
theme_ipsum()
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
select(female, contains("12s"), -ipl12s) %>%
group_by(female) %>%
summarize_all(mean, na.rm = T) %>%
ggradar(values = c("0", "0.4", "0.8"), grid.mid = 0.4, grid.max = 0.8, legend.title = "Female")
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
group_by(citizen) %>%
summarise(
ipl12s = mean(ipl12s, na.rm = TRUE),
weight = mean(weight, na.rm = TRUE)
) %>%
drop_na() %>%
ggplot(aes(factor(citizen), ipl12s, weight = weight)) +
geom_col() +
theme_ipsum()
## ---- out.width="100%"------------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
group_by(citizen) %>%
summarise_at(vars(contains("12s"), weight), mean, na.rm = T) %>%
drop_na() %>%
arrange(ipl12s) %>%
mutate(citizen = fct_inorder(factor(citizen), ipl12s) %>% fct_rev()) %>%
pivot_longer(
cols = contains("12s"),
names_to = "measure",
values_to = "score"
) %>%
filter(measure != "ipl12s") %>%
ggplot(aes(factor(citizen), score, weight = weight)) +
geom_col() +
facet_wrap(vars(measure)) +
theme_ipsum()
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
group_by(educationLevel) %>%
summarise(
ipl12s = mean(ipl12s, na.rm = TRUE),
weight = mean(weight, na.rm = TRUE)
) %>%
drop_na() %>%
arrange(ipl12s) %>%
mutate(educationLevel = fct_inorder(educationLevel, ipl12s) %>% fct_rev()) %>%
ggplot(aes(educationLevel, ipl12s, weight = weight)) +
geom_col() +
theme_ipsum() +
coord_flip()
## ---- out.width="100%"------------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
group_by(educationLevel) %>%
summarise_at(vars(contains("12s"), weight), mean, na.rm = T) %>%
drop_na() %>%
arrange(ipl12s) %>%
mutate(speak = fct_inorder(educationLevel, ipl12s) %>% fct_rev()) %>%
pivot_longer(
cols = contains("12s"),
names_to = "measure",
values_to = "score"
) %>%
filter(measure != "ipl12s") %>%
drop_na(educationLevel) %>%
ggplot(aes(educationLevel, score, weight = weight)) +
geom_col() +
theme_ipsum() +
facet_wrap(vars(measure)) +
coord_flip()
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
group_by(speak) %>%
summarise(
ipl12s = mean(ipl12s, na.rm = TRUE),
weight = mean(weight, na.rm = TRUE)
) %>%
drop_na() %>%
arrange(ipl12s) %>%
mutate(speak = fct_inorder(speak, ipl12s) %>% fct_rev()) %>%
ggplot(aes(speak, ipl12s, weight = weight)) +
geom_col() +
theme_ipsum() +
coord_flip()
## ---- out.width = "100%"----------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
group_by(speak) %>%
summarise_at(vars(contains("12s"), weight), mean, na.rm = T) %>%
drop_na() %>%
arrange(ipl12s) %>%
mutate(speak = fct_inorder(speak, ipl12s) %>% fct_rev()) %>%
pivot_longer(
cols = contains("12s"),
names_to = "measure",
values_to = "score"
) %>%
filter(measure != "ipl12s") %>%
ggplot(aes(speak, score, weight = weight)) +
geom_col() +
facet_wrap(vars(measure)) +
theme_ipsum() +
coord_flip()
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
group_by(read) %>%
summarise(
ipl12s = mean(ipl12s, na.rm = TRUE),
weight = mean(weight, na.rm = TRUE)
) %>%
drop_na() %>%
arrange(ipl12s) %>%
mutate(read = fct_inorder(read, ipl12s) %>% fct_rev()) %>%
ggplot(aes(read, ipl12s, weight = weight)) +
geom_col() +
theme_ipsum()
## ---- out.width = "100%"----------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
group_by(read) %>%
summarise_at(vars(contains("12s"), weight), mean, na.rm = T) %>%
drop_na() %>%
arrange(ipl12s) %>%
mutate(read = fct_inorder(read, ipl12s) %>% fct_rev()) %>%
pivot_longer(
cols = contains("12s"),
names_to = "measure",
values_to = "score"
) %>%
filter(measure != "ipl12s") %>%
ggplot(aes(read, score, weight = weight)) +
geom_col() +
facet_wrap(vars(measure)) +
theme_ipsum() +
coord_flip()
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
group_by(income) %>%
summarise(
ipl12s = mean(ipl12s, na.rm = TRUE),
weight = mean(weight, na.rm = TRUE)
) %>%
drop_na() %>%
ggplot(aes(income, ipl12s, weight = weight)) +
geom_col() +
theme_ipsum()
## ---- out.width = "100%"----------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
group_by(income) %>%
summarise_at(vars(contains("12s"), weight), mean, na.rm = T) %>%
drop_na() %>%
arrange(ipl12s) %>%
mutate(
income = as_factor(income),
income = fct_inorder(income, ipl12s) %>% fct_rev()
) %>%
pivot_longer(
cols = contains("12s"),
names_to = "measure",
values_to = "score"
) %>%
filter(measure != "ipl12s") %>%
ggplot(aes(income, score, weight = weight)) +
geom_col() +
facet_wrap(vars(measure)) +
theme_ipsum() +
coord_flip()
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
group_by(unemployment) %>%
summarise(
ipl12s = mean(ipl12s, na.rm = TRUE),
weight = mean(weight, na.rm = TRUE)
) %>%
drop_na() %>%
ggplot(aes(factor(unemployment), ipl12s, weight = weight)) +
geom_col() +
theme_ipsum()
## ---- out.width = "100%"----------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
group_by(unemployment) %>%
summarise_at(vars(contains("12s"), weight), mean, na.rm = T) %>%
drop_na() %>%
arrange(ipl12s) %>%
mutate(
unemployment = as_factor(unemployment),
unemployment = fct_inorder(unemployment, ipl12s) %>% fct_rev()
) %>%
pivot_longer(
cols = contains("12s"),
names_to = "measure",
values_to = "score"
) %>%
filter(measure != "ipl12s") %>%
ggplot(aes(unemployment, score, weight = weight)) +
geom_col() +
facet_wrap(vars(measure)) +
theme_ipsum() +
coord_flip()
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
group_by(bcountryBinned) %>%
summarise(
ipl12s = mean(ipl12s, na.rm = TRUE),
weight = mean(weight, na.rm = TRUE)
) %>%
drop_na() %>%
arrange(ipl12s) %>%
mutate(bcountryBinned = fct_inorder(bcountryBinned, ipl12s) %>% fct_rev()) %>%
ggplot(aes(bcountryBinned, ipl12s, weight = weight)) +
geom_col() +
theme_ipsum()
## ---- out.width = "100%"----------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
group_by(bcountryBinned) %>%
summarise_at(vars(contains("12s"), weight), mean, na.rm = T) %>%
drop_na() %>%
arrange(ipl12s) %>%
mutate(
bcountryBinned = as_factor(bcountryBinned),
bcountryBinned = fct_inorder(bcountryBinned, ipl12s) %>% fct_rev()
) %>%
pivot_longer(
cols = contains("12s"),
names_to = "measure",
values_to = "score"
) %>%
filter(measure != "ipl12s") %>%
ggplot(aes(bcountryBinned, score, weight = weight)) +
geom_col() +
facet_wrap(vars(measure)) +
theme_ipsum() +
coord_flip()
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
filter(householdSize <= 11) %>%
group_by(householdSize) %>%
summarise(
ipl12s = mean(ipl12s, na.rm = TRUE),
weight = mean(weight, na.rm = TRUE)
) %>%
drop_na() %>%
ggplot(aes(householdSize, ipl12s, weight = weight)) +
geom_col() +
theme_ipsum()
## ---- out.width = "100%"----------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
group_by(householdSize) %>%
summarise_at(vars(contains("12s"), weight), mean, na.rm = T) %>%
drop_na() %>%
arrange(ipl12s) %>%
pivot_longer(
cols = contains("12s"),
names_to = "measure",
values_to = "score"
) %>%
filter(measure != "ipl12s") %>%
drop_na(householdSize) %>%
filter(householdSize <= 11) %>%
mutate(householdSize = as.integer(householdSize)) %>%
ggplot(aes(householdSize, score)) +
geom_col() +
scale_x_continuous(breaks = seq(0, 12, 2)) +
theme_ipsum() +
facet_wrap(vars(measure))
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
# preprocessing some vars
df_baseline <-
df_baseline %>%
mutate(
read = case_when(
read == "Very well" ~ 5,
read == "Well" ~ 4,
read == "Moderately well" ~ 3,
read == "Not well" ~ 2,
read == "Not well at all" ~ 1
),
speak = case_when(
speak == "Very well" ~ 5,
speak == "Well" ~ 4,
speak == "Moderately well" ~ 3,
speak == "Not well" ~ 2,
speak == "Not well at all" ~ 1
),
educationTerciles = factor(educationTerciles, levels = c("Low education", "Medium education", "High education")),
citizen = factor(citizen, levels = c("No", "Yes"))
)
## ---- results='asis'--------------------------------------------------------------------------------------------------------------------------------------
lm1 <- df_baseline %>% lm_robust(ipl12s ~ female + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm2 <- df_baseline %>% lm_robust(ling12s ~ female + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm3 <- df_baseline %>% lm_robust(pol12s ~ female + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm4 <- df_baseline %>% lm_robust(soc12s ~ female + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm5 <- df_baseline %>% lm_robust(econ12s ~ female + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm6 <- df_baseline %>% lm_robust(psy12s ~ female + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm7 <- df_baseline %>% lm_robust(nav12s ~ female + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
knitreg(list(lm1, lm2, lm3, lm4, lm5, lm6, lm7), include.ci = FALSE, custom.model.names = c("Overall", "Linguistic", "Political", "Social", "Economic", "Psychological", "Navigational"), stars = c(0.01, 0.05, 0.1), digits = 3)
## ---- results='asis'--------------------------------------------------------------------------------------------------------------------------------------
lm1 <- df_baseline %>% lm_robust(ipl12s ~ years_in_us, data = ., se_type = "stata", weights = weight)
lm2 <- df_baseline %>% lm_robust(ling12s ~ years_in_us, data = ., se_type = "stata", weights = weight)
lm3 <- df_baseline %>% lm_robust(pol12s ~ years_in_us, data = ., se_type = "stata", weights = weight)
lm4 <- df_baseline %>% lm_robust(soc12s ~ years_in_us, data = ., se_type = "stata", weights = weight)
lm5 <- df_baseline %>% lm_robust(econ12s ~ years_in_us, data = ., se_type = "stata", weights = weight)
lm6 <- df_baseline %>% lm_robust(psy12s ~ years_in_us, data = ., se_type = "stata", weights = weight)
lm7 <- df_baseline %>% lm_robust(nav12s ~ years_in_us, data = ., se_type = "stata", weights = weight)
knitreg(list(lm1, lm2, lm3, lm4, lm5, lm6, lm7), include.ci = FALSE, custom.model.names = c("Overall", "Linguistic", "Political", "Social", "Economic", "Psychological", "Navigational"), stars = c(0.01, 0.05, 0.1), digits = 3)
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
filter(yearArrive >= 2010) %>%
ggplot() +
geom_smooth(aes(years_in_us, ipl12s), method = "loess") +
scale_x_continuous(breaks = seq(0, 10, 1)) +
theme_ipsum()
## ---- out.width = "100%"----------------------------------------------------------------------------------------------------------------------------------
df_baseline %>%
group_by(years_in_us) %>%
summarise_at(vars(contains("12s"), weight), mean, na.rm = T) %>%
drop_na() %>%
arrange(ipl12s) %>%
pivot_longer(
cols = contains("12s"),
names_to = "measure",
values_to = "score"
) %>%
filter(measure != "ipl12s") %>%
drop_na(years_in_us) %>%
filter(years_in_us <= 10) %>%
ggplot() +
geom_smooth(aes(years_in_us, score), method = "loess") +
scale_x_continuous(breaks = seq(0, 10, 2)) +
theme_ipsum() +
theme(axis.text.x = element_text(angle = -45, hjust = 0)) +
facet_wrap(vars(measure))
## ---- results='asis'--------------------------------------------------------------------------------------------------------------------------------------
lm1 <- df_baseline %>% lm_robust(ipl12s ~ years_in_us + female + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm2 <- df_baseline %>% lm_robust(ling12s ~ years_in_us + female + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm3 <- df_baseline %>% lm_robust(pol12s ~ years_in_us + female + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm4 <- df_baseline %>% lm_robust(soc12s ~ years_in_us + female + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm5 <- df_baseline %>% lm_robust(econ12s ~ years_in_us + female + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm6 <- df_baseline %>% lm_robust(psy12s ~ years_in_us + female + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm7 <- df_baseline %>% lm_robust(nav12s ~ years_in_us + female + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
knitreg(list(lm1, lm2, lm3, lm4, lm5, lm6, lm7), include.ci = FALSE, custom.model.names = c("Overall", "Linguistic", "Political", "Social", "Economic", "Psychological", "Navigational"), stars = c(0.01, 0.05, 0.1), digits = 3)
## ---- results='asis'--------------------------------------------------------------------------------------------------------------------------------------
lm1 <- df_baseline %>% lm_robust(ipl12s ~ years_in_us + female + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight, fixed_effects = ~ stateCurrent)
lm2 <- df_baseline %>% lm_robust(ling12s ~ years_in_us + female + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight, fixed_effects = ~ stateCurrent)
lm3 <- df_baseline %>% lm_robust(pol12s ~ years_in_us + female + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight, fixed_effects = ~ stateCurrent)
lm4 <- df_baseline %>% lm_robust(soc12s ~ years_in_us + female + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight, fixed_effects = ~ stateCurrent)
lm5 <- df_baseline %>% lm_robust(econ12s ~ years_in_us + female + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight, fixed_effects = ~ stateCurrent)
lm6 <- df_baseline %>% lm_robust(psy12s ~ years_in_us + female + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight, fixed_effects = ~ stateCurrent)
lm7 <- df_baseline %>% lm_robust(nav12s ~ years_in_us + female + read + speak + householdSize +bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight, fixed_effects = ~ stateCurrent)
knitreg(list(lm1, lm2, lm3, lm4, lm5, lm6, lm7), include.ci = FALSE, custom.model.names = c("Overall", "Linguistic", "Political", "Social", "Economic", "Psychological", "Navigational"), stars = c(0.01, 0.05, 0.1), digits = 3)
## ---- results='asis'--------------------------------------------------------------------------------------------------------------------------------------
lm1 <- df_baseline %>% lm_robust(ipl12s ~ female * years_in_us + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm2 <- df_baseline %>% lm_robust(ling12s ~ female * years_in_us + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm3 <- df_baseline %>% lm_robust(pol12s ~ female * years_in_us + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm4 <- df_baseline %>% lm_robust(soc12s ~ female * years_in_us + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm5 <- df_baseline %>% lm_robust(econ12s ~ female * years_in_us + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm6 <- df_baseline %>% lm_robust(psy12s ~ female * years_in_us + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm7 <- df_baseline %>% lm_robust(nav12s ~ female * years_in_us + read + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
knitreg(list(lm1, lm2, lm3, lm4, lm5, lm6, lm7), include.ci = FALSE, custom.model.names = c("Overall", "Linguistic", "Political", "Social", "Economic", "Psychological", "Navigational"), stars = c(0.01, 0.05, 0.1), digits = 3)
## ---- results='asis'--------------------------------------------------------------------------------------------------------------------------------------
lm1 <- df_baseline %>% lm_robust(ipl12s ~ female + read * years_in_us + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm2 <- df_baseline %>% lm_robust(ling12s ~ female + read * years_in_us + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm3 <- df_baseline %>% lm_robust(pol12s ~ female + read * years_in_us + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm4 <- df_baseline %>% lm_robust(soc12s ~ female + read * years_in_us + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm5 <- df_baseline %>% lm_robust(econ12s ~ female + read * years_in_us + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm6 <- df_baseline %>% lm_robust(psy12s ~ female + read * years_in_us + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm7 <- df_baseline %>% lm_robust(nav12s ~ female + read * years_in_us + speak + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
knitreg(list(lm1, lm2, lm3, lm4, lm5, lm6, lm7), include.ci = FALSE, custom.model.names = c("Overall", "Linguistic", "Political", "Social", "Economic", "Psychological", "Navigational"), stars = c(0.01, 0.05, 0.1), digits = 3)
## ---- results='asis'--------------------------------------------------------------------------------------------------------------------------------------
lm1 <- df_baseline %>% lm_robust(ipl12s ~ female + read + speak * years_in_us + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm2 <- df_baseline %>% lm_robust(ling12s ~ female + read + speak * years_in_us + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm3 <- df_baseline %>% lm_robust(pol12s ~ female + read + speak * years_in_us + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm4 <- df_baseline %>% lm_robust(soc12s ~ female + read + speak * years_in_us + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm5 <- df_baseline %>% lm_robust(econ12s ~ female + read + speak * years_in_us + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm6 <- df_baseline %>% lm_robust(psy12s ~ female + read + speak * years_in_us + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm7 <- df_baseline %>% lm_robust(nav12s ~ female + read + speak * years_in_us + householdSize + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
knitreg(list(lm1, lm2, lm3, lm4, lm5, lm6, lm7), include.ci = FALSE, custom.model.names = c("Overall", "Linguistic", "Political", "Social", "Economic", "Psychological", "Navigational"), stars = c(0.01, 0.05, 0.1), digits = 3)
## ---- results='asis'--------------------------------------------------------------------------------------------------------------------------------------
lm1 <- df_baseline %>% lm_robust(ipl12s ~ female + read + speak + householdSize * years_in_us + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm2 <- df_baseline %>% lm_robust(ling12s ~ female + read + speak + householdSize * years_in_us + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm3 <- df_baseline %>% lm_robust(pol12s ~ female + read + speak + householdSize * years_in_us + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm4 <- df_baseline %>% lm_robust(soc12s ~ female + read + speak + householdSize * years_in_us + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm5 <- df_baseline %>% lm_robust(econ12s ~ female + read + speak + householdSize * years_in_us + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm6 <- df_baseline %>% lm_robust(psy12s ~ female + read + speak + householdSize * years_in_us + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
lm7 <- df_baseline %>% lm_robust(nav12s ~ female + read + speak + householdSize * years_in_us + bcountryBinned + educationTerciles, data = ., se_type = "stata", weights = weight)
knitreg(list(lm1, lm2, lm3, lm4, lm5, lm6, lm7), include.ci = FALSE, custom.model.names = c("Overall", "Linguistic", "Political", "Social", "Economic", "Psychological", "Navigational"), stars = c(0.01, 0.05, 0.1), digits = 3)
## ---- results='asis'--------------------------------------------------------------------------------------------------------------------------------------
lm1 <- df_baseline %>% lm_robust(ipl12s ~ female + read + speak + householdSize + bcountryBinned + educationTerciles * years_in_us, data = ., se_type = "stata", weights = weight)
lm2 <- df_baseline %>% lm_robust(ling12s ~ female + read + speak + householdSize + bcountryBinned + educationTerciles * years_in_us, data = ., se_type = "stata", weights = weight)
lm3 <- df_baseline %>% lm_robust(pol12s ~ female + read + speak + householdSize + bcountryBinned + educationTerciles * years_in_us, data = ., se_type = "stata", weights = weight)
lm4 <- df_baseline %>% lm_robust(soc12s ~ female + read + speak + householdSize + bcountryBinned + educationTerciles * years_in_us, data = ., se_type = "stata", weights = weight)
lm5 <- df_baseline %>% lm_robust(econ12s ~ female + read + speak + householdSize + bcountryBinned + educationTerciles * years_in_us, data = ., se_type = "stata", weights = weight)
lm6 <- df_baseline %>% lm_robust(psy12s ~ female + read + speak + householdSize + bcountryBinned + educationTerciles * years_in_us, data = ., se_type = "stata", weights = weight)
lm7 <- df_baseline %>% lm_robust(nav12s ~ female + read + speak + householdSize + bcountryBinned + educationTerciles * years_in_us, data = ., se_type = "stata", weights = weight)
knitreg(list(lm1, lm2, lm3, lm4, lm5, lm6, lm7), include.ci = FALSE, custom.model.names = c("Overall", "Linguistic", "Political", "Social", "Economic", "Psychological", "Navigational"), stars = c(0.01, 0.05, 0.1), digits = 3)