-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkaty-meta-pred.R
143 lines (110 loc) · 4.18 KB
/
katy-meta-pred.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
################################################################################
# Hack of a function but it seems to work
################################################################################
katyMetaPred <- function(model, vars, new_data, coh_names, method){
coef_names <- model %>%
map(function(x){
dimnames(x$output.summary$study1$coefficients)[[1]]
}) %>%
set_names(coh_names)
var_exists <- coef_names %>%
map(~ vars %in% . == TRUE) %>%
unlist
if(all(var_exists) != TRUE){
stop("The variable(s) you want to meta-analyse don't exist in all models")
}
ref <- tibble(
model = model,
coh_names = coh_names)
coefs <- ref %>%
pmap(function(model, coh_names){
dh.lmTab(
model = model,
type = "lmer_slma",
coh_names = coh_names,
direction = "wide",
ci_format = "separate")$fixed %>%
dplyr::filter(cohort != "combined")
}) %>%
bind_rows %>%
dplyr::filter(variable %in% vars)
model_holder <- coefs %>%
group_by(variable) %>%
group_keys
## ---- Meta-analyse ---------------------------------------------------------
ma.fit <- coefs %>%
group_by(variable) %>%
group_split() %>%
map(
~rma.uni(
yi = .x$est,
sei = .x$se,
method = method,
control=list(stepadj=0.5, maxiter=1000)))
## ---- Put back together ----------------------------------------------------
model_holder <- model_holder %>%
mutate(meta = ma.fit)
ma.out <- model_holder %>%
pmap_df(function(variable, meta){
tibble(
variable = variable,
est = meta$beta[1],
se = meta$se,
lowci = meta$ci.lb,
uppci = meta$ci.ub,
i2 = round(meta$I2, 1),
n_coh = meta$k)
}) %>%
mutate(metafor_obj = model_holder$meta)
coef_names <- ma.out$variable
## ---- Now we get the coefficients for each cohort ----------------------------
coefs_by_cohort <- ma.out %>%
dplyr::select(variable, est) %>%
pivot_wider(
names_from = variable,
values_from = est)
## ---- Make sure the columns are in the correct order -------------------------
coefs_by_cohort %<>% dplyr::select(all_of(coef_names))
newdata_min <- new_data %>% select(all_of(coef_names))
## ---- Now we multiply the coefficients by new data ---------------------------
fixed <- newdata_min %>%
pmap_df(function(...) {
out <- c(...) * coefs_by_cohort
return(out)
}) %>% as_tibble
## ---- Now do the business ----------------------------------------------------
pred <- new_data %>%
mutate(predicted = rowSums(fixed))
return(list(coefs = ma.out, predicted = pred))
}
################################################################################
# 2. Fit separate models for each cohort with differing covariates
################################################################################
cohort_1.fit <- ds.lmerSLMA(
dataName = "analysis_df",
formula = "ext_pc_ ~ 1 + ext_age_ + ext_age__2 + sex + (1|child_id_int)",
datasources = conns["alspac"])
cohort_2.fit <- ds.lmerSLMA(
dataName = "analysis_df",
formula = "ext_pc_ ~ 1 + ext_age_ + ext_age__2 + agebirth_m_y + (1|child_id_int)",
datasources = conns["genr"])
cohort_3.fit <- ds.lmerSLMA(
dataName = "analysis_df",
formula = "ext_pc_ ~ 1 + ext_age_ + ext_age__2 + int_raw_ + (1|child_id_int)",
datasources = conns["moba"])
newdata <- tibble(
ext_age_ = seq(1, 100, 1),
ext_age__2 = seq(1, 100, 1))
################################################################################
# 3. Get predicted values
################################################################################
## This returns a list containing (1) meta-analysed coefficients and (2) the
## predicted values
predicted <- katyMetaPred(
model = list(alspac.fit, genr.fit, dnbc.fit, eden.fit, inma.fit),
vars = c("fcentre_care", "int_age_m_0_5", "int_age_m_2",
"fcentre_care*int_age_m_0_5", "fcentre_care*int_age_m_2"),
new_data = newdata,
coh_names = c("alspac", "genr", "dnbc", "eden", "inma"),
method = "REML")
predicted$pred %>% print(n = Inf)