-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path_demographics.Rmd
168 lines (145 loc) · 6.53 KB
/
_demographics.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
```{r demo-funs}
N_MAIN_GROUPS <- 5
N_AGE_POINTS <- 4
# get means split by demographic features
get_demo_means <- function(data, demo = ~sex, measure = ~production) {
data %>%
group_by(language, form, !!demo, age) %>%
mutate(measure = !!measure) %>%
summarise(median = median(.data$measure / n),
n = n())
}
# get glmrob models on demographic feature
get_demo_models <- function(data, demo = ~sex, measure = ~production,
ref_level = "Male") {
data %>%
mutate(measure = !!measure,
no_measure = n - .data$measure,
demo = !!demo,
demo = fct_relevel(demo, ref_level)) %>%
unite(langform, language, form, sep = "_") %>%
split(.$langform) %>%
map(possibly(function(x) {
# print(x$langform[1])
robustbase::glmrob(cbind(measure, no_measure) ~ age * demo - demo,
family = "binomial",
data = x)
}, otherwise = NULL))
}
# predict from models in the given age range so that you can plot
get_demo_preds <- function(models, data, demo = ~sex) {
demo_name <- rlang::quo_name(demo) # quo
data %>%
unite(langform, language, form, sep = "_") %>%
split(.$langform) %>%
map_df(possibly(function(x) {
print(x$langform[1])
new_data <- list(age = min(x$age):max(x$age),
demo = unique(pull(x, !!demo)),
langform = x$langform[1]) %>%
cross_df()
new_data$pred <- predict(models[[x$langform[1]]],
newdata = new_data, type = "response")
return(new_data)
}, otherwise = tibble())) %>%
rename(!!demo_name := demo) %>%
separate(langform, into = c("language","form"), sep = "_")
}
# get the appropriate coefficient from the model and corresponding p value
get_demo_model_summary <- function(models, level = "Male") {
coef_name <- quo(paste0("age:demo", level))
models %>%
map_df(possibly(function(x) {
# print(x$language[1])
p <- summary(x)$coefficients[rlang::eval_tidy(coef_name), 'Pr(>|z|)']
beta <- summary(x)$coefficients[rlang::eval_tidy(coef_name), 'Estimate']
return(data_frame(beta = beta, p = p))
}, otherwise = tibble(p = NA, beta = NA))) %>%
mutate(langforms = names(models)) %>% # ugly
separate(langforms, into = c("language","form"), sep = "_") %>%
select(language, form, beta, p) %>%
arrange(beta)
}
# compute MMAD effect size measure by the demographic and particular levels
# tricky to filter outliers but important, the current policy is to filter cases
# where there are fewer than N_MAIN_GROUPS in each of the two main groups that are being used for MMAD comparison.
get_mmad_ratio <- function(data, measure = quo(production), demo = quo(sex),
high_level = "Female", low_level = "Male") {
data %>%
group_by(language, form, age, !!demo) %>%
filter(n() > N_MAIN_GROUPS | !(!!demo %in% c(high_level,low_level))) %>%
mutate(measure = !!measure) %>%
summarise(median = median(measure),
mad = mad(measure),
n = n()) %>%
group_by(language, form, age) %>%
filter(any(!! rlang::get_expr(demo) == high_level),
any(!! rlang::get_expr(demo) == low_level)) %>% # one of each necessary level
summarise(mmad = (median[!!(demo) == high_level] - median[!!(demo) == low_level]) /
mean(mad[!!(demo) %in% c(high_level, low_level)]),
n = sum(n)) %>%
filter(is.finite(mmad))
}
# summarise MMAD across development, again for given demoraphic
get_mmad_summary <- function(data) {
data %>%
group_by(language, form) %>%
summarise(mmad_mean = weighted.mean(mmad, w = n),
n = n()) %>%
filter(n > N_AGE_POINTS) %>% # more than four age points - FIXME, arbitrary
ungroup %>%
mutate(language = fct_reorder(language, mmad_mean))
}
```
```{r demo-analyses, eval=FALSE}
# this code block does all the analyses above to all the combos of measures, forms, and demographics
# build all the combos to analyze
analysis_combos <- left_join(
tibble(form = c("WG", "WG", "WS"),
measure = c(quo(comprehension), quo(production), quo(production)),
measure_name = c("comprehension","production","production"),
index = 1:3),
expand.grid(index = 1:3,
demo = c(quo(sex), quo(birth_order), quo(mom_ed)))) %>%
mutate(demo_name = map_chr(demo, rlang::quo_name),
reference_level = case_when(demo_name == "sex" ~ "Male",
demo_name == "birth_order" ~ "Second",
demo_name == "mom_ed" ~ "Secondary"),
high_level = case_when(demo_name == "sex" ~ "Female",
demo_name == "birth_order" ~ "First",
demo_name == "mom_ed" ~ "College and Above"))
# run all the analyses above on all those combos
analyses <- analysis_combos %>%
split(list(.$form, .$measure_name)) %>%
map(function (form_measure_data) {
form_measure_data %>%
split(.$demo_name) %>%
map(possibly(function(demo_data) {
demo <- demo_data$demo[1][[1]]
measure <- demo_data$measure[1][[1]]
form_class <- demo_data$form[1]
# deal with form classes to include TC/IC etc.
if (form_class == "WS") {
forms <- WSs
} else if (form_class == "WG") {
forms <- WGs
}
data <- vocab_data %>%
filter(!is.na(!!demo), form %in% forms)
results <- list()
results$means <- get_demo_means(data, demo = demo, measure = measure)
results$models <- get_demo_models(data, demo = demo, measure = measure,
ref_level = demo_data$reference_level[1])
results$preds <- get_demo_preds(results$models, data, demo = demo)
results$model_summary <- get_demo_model_summary(results$models,
level = demo_data$high_level[1])
results$mmad_ratio <- get_mmad_ratio(data, measure = measure, demo = demo,
high_level = demo_data$high_level[1],
low_level = demo_data$reference_level[1])
results$mmad_summary <- get_mmad_summary(results$mmad_ratio)
return(results)
},
otherwise = tibble())) # otherwise for possibly call
})
save(file = "data/demographics/demo-analyses.Rds", analyses)
```