-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrm_mv_model_testing.R
189 lines (164 loc) · 8.19 KB
/
brm_mv_model_testing.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
# Simulate multivariate normal data in two levels: maternal taxa abundance means are MVN,
# then offspring taxa abundance means are MVN from that.
library(mvtnorm)
library(brms)
options(mc.cores = 4, brms.backend = 'cmdstanr', brms.file_refit = 'on_change')
# Start with manageable number of taxa.
n_mothers <- 10
n_taxa <- 5
offspring_per_mother <- 10 # 5 will be retained for traits, 5 for microbiome
set.seed(1)
X_maternal <- rmvnorm(n_mothers, mean = rep(0, n_taxa), sigma = diag(n_taxa))
sigma_maternal <- cov(X_maternal)
# Coefficients indicating which taxa predict the outcome.
# We will not include any interaction effect.
beta <- c(5, -5, 0, 0, 0)
y_maternal <- 10 + X_maternal %*% beta + rnorm(n_mothers, 0, 1)
# To get offspring microbiome, take multivariate normal draws from the mean vector for each mother (rows of X_maternal)
X_offspring <- apply(X_maternal, 1, function(Xi) rmvnorm(offspring_per_mother, mean = Xi, sigma = sigma_maternal), simplify = FALSE)
# Use regression coefficients (beta) to get value for offspring trait, plus noise
y_offspring <- lapply(X_offspring, function(Xoi) Xoi %*% beta + rnorm(offspring_per_mother, 0, 1))
# Combine together
dt <- data.frame(
maternal_id = factor(rep(1:n_mothers, each = offspring_per_mother)),
offspring_id = 1:offspring_per_mother,
do.call(rbind, X_offspring),
y = do.call(c, y_offspring)
)
# Within each mother, set half of the values to be missing for x, and the other half for y.
xmiss <- lapply(1:nrow(dt), function(i) {
if (dt[i, 'offspring_id'] %in% 1:(offspring_per_mother/2)) {
setNames(dt[i, paste0('X', 1:n_taxa)], paste0('Xmiss',1:n_taxa))
} else {
setNames(rep(NA, n_taxa), paste0('Xmiss', 1:n_taxa))
}
})
dt <- cbind(dt, do.call(rbind, xmiss))
dt$ymiss <- ifelse(dt$offspring_id %in% 1:(offspring_per_mother/2), NA, dt$y)
# Model without missing data ----------------------------------------------
# Model without missing data, including regularized horseshoe prior
# Let's see if the coefficients can be recovered.
# Interactions between taxa aren't included.
get_prior(
bf(mvbind(X1, X2, X3, X4, X5) ~ (1||maternal_id)) + bf(y ~ X1 + X2 + X3 + X4 + X5 + (1||maternal_id)) + set_rescor(FALSE),
data=dt
)
# First try without regularization prior.
modmv_nomiss <- brm(
bf(mvbind(X1, X2, X3, X4, X5) ~ (1||maternal_id)) + bf(y ~ X1 + X2 + X3 + X4 + X5 + (1||maternal_id)) + set_rescor(FALSE),
prior = c(
prior(gamma(1, 1), class = sd, resp = X1),
prior(gamma(1, 1), class = sd, resp = X2),
prior(gamma(1, 1), class = sd, resp = X3),
prior(gamma(1, 1), class = sd, resp = X4),
prior(gamma(1, 1), class = sd, resp = X5),
prior(gamma(1, 1), class = sd, resp = y),
prior(gamma(1, 1), class = sigma, resp = X1),
prior(gamma(1, 1), class = sigma, resp = X2),
prior(gamma(1, 1), class = sigma, resp = X3),
prior(gamma(1, 1), class = sigma, resp = X4),
prior(gamma(1, 1), class = sigma, resp = X5),
prior(gamma(1, 1), class = sigma, resp = y),
prior(normal(0, 5), class = b, resp = y) # Only y carries any fixed effects.
),
data = dt,
chains = 4, iter = 2000, warmup = 1000,
file = 'project/fits/brmtest_mv_nomiss'
)
# Second try, with regularization prior
modmv_nomiss_reghorseshoe <- brm(
bf(mvbind(X1, X2, X3, X4, X5) ~ (1||maternal_id)) + bf(y ~ X1 + X2 + X3 + X4 + X5 + (1||maternal_id)) + set_rescor(FALSE),
prior = c(
prior(gamma(1, 1), class = sd, resp = X1),
prior(gamma(1, 1), class = sd, resp = X2),
prior(gamma(1, 1), class = sd, resp = X3),
prior(gamma(1, 1), class = sd, resp = X4),
prior(gamma(1, 1), class = sd, resp = X5),
prior(gamma(1, 1), class = sd, resp = y),
prior(gamma(1, 1), class = sigma, resp = X1),
prior(gamma(1, 1), class = sigma, resp = X2),
prior(gamma(1, 1), class = sigma, resp = X3),
prior(gamma(1, 1), class = sigma, resp = X4),
prior(gamma(1, 1), class = sigma, resp = X5),
prior(gamma(1, 1), class = sigma, resp = y),
prior(horseshoe(df = 1, df_global = 1, scale_slab = 20, df_slab = 4, par_ratio = 2/3), class = b, resp = y) # Only y carries any fixed effects.
# We are setting par_ratio to 2/3 because we "know" that is the ratio of nonzero to zero we are going for. Otherwise default values are used.
),
data = dt,
chains = 4, iter = 2000, warmup = 1000,
file = 'project/fits/brmtest_mv_nomiss_reghorseshoe'
)
# Model with missing data -------------------------------------------------
# First attempt. No regularization prior is used.
modmv_miss <- brm(
bf(mvbind(Xmiss1, Xmiss2, Xmiss3, Xmiss4, Xmiss5) | mi() ~ 0 + (1||maternal_id)) + bf(ymiss | mi() ~ mi(Xmiss1) + mi(Xmiss2) + mi(Xmiss3) + mi(Xmiss4) + mi(Xmiss5) + (1||maternal_id)) + set_rescor(FALSE),
prior = c(
prior(gamma(1, 1), class = sd, resp = Xmiss1),
prior(gamma(1, 1), class = sd, resp = Xmiss2),
prior(gamma(1, 1), class = sd, resp = Xmiss3),
prior(gamma(1, 1), class = sd, resp = Xmiss4),
prior(gamma(1, 1), class = sd, resp = Xmiss5),
prior(gamma(1, 1), class = sd, resp = ymiss),
prior(gamma(1, 1), class = sigma, resp = Xmiss1),
prior(gamma(1, 1), class = sigma, resp = Xmiss2),
prior(gamma(1, 1), class = sigma, resp = Xmiss3),
prior(gamma(1, 1), class = sigma, resp = Xmiss4),
prior(gamma(1, 1), class = sigma, resp = Xmiss5),
prior(gamma(1, 1), class = sigma, resp = ymiss),
prior(normal(0, 5), class = b, resp = ymiss) # Only y carries any fixed effects.
),
data = dt,
chains = 4, iter = 4000, warmup = 2000,
file = 'project/fits/brmtest_mv_miss'
)
# With regularized horseshoe prior on fixed effects.
modmv_miss_reghorseshoe <- brm(
bf(mvbind(Xmiss1, Xmiss2, Xmiss3, Xmiss4, Xmiss5) | mi() ~ (1||maternal_id)) + bf(ymiss | mi() ~ mi(Xmiss1) + mi(Xmiss2) + mi(Xmiss3) + mi(Xmiss4) + mi(Xmiss5) + (1||maternal_id)) + set_rescor(FALSE),
prior = c(
prior(gamma(1, 1), class = sd, resp = Xmiss1),
prior(gamma(1, 1), class = sd, resp = Xmiss2),
prior(gamma(1, 1), class = sd, resp = Xmiss3),
prior(gamma(1, 1), class = sd, resp = Xmiss4),
prior(gamma(1, 1), class = sd, resp = Xmiss5),
prior(gamma(1, 1), class = sd, resp = ymiss),
prior(gamma(1, 1), class = sigma, resp = Xmiss1),
prior(gamma(1, 1), class = sigma, resp = Xmiss2),
prior(gamma(1, 1), class = sigma, resp = Xmiss3),
prior(gamma(1, 1), class = sigma, resp = Xmiss4),
prior(gamma(1, 1), class = sigma, resp = Xmiss5),
prior(gamma(1, 1), class = sigma, resp = ymiss),
prior(horseshoe(df = 1, df_global = 1, scale_slab = 20, df_slab = 4, par_ratio = 2/3), class = b, resp = ymiss)
),
data = dt,
chains = 4, iter = 2000, warmup = 1000,
file = 'project/fits/brmtest_mv_miss_reghorseshoe'
)
### Attempt to fit the missing data model with no missing data.
### This results in the same model as the one fit to the full data so we do not have to worry about two separate model specifications, just change the data.
modmv_missfull_reghorseshoe <- brm(
bf(mvbind(X1, X2, X3, X4, X5) | mi() ~ (1||maternal_id)) + bf(y | mi() ~ mi(X1) + mi(X2) + mi(X3) + mi(X4) + mi(X5) + (1||maternal_id)) + set_rescor(FALSE),
prior = c(
prior(gamma(1, 1), class = sd, resp = X1),
prior(gamma(1, 1), class = sd, resp = X2),
prior(gamma(1, 1), class = sd, resp = X3),
prior(gamma(1, 1), class = sd, resp = X4),
prior(gamma(1, 1), class = sd, resp = X5),
prior(gamma(1, 1), class = sd, resp = y),
prior(gamma(1, 1), class = sigma, resp = X1),
prior(gamma(1, 1), class = sigma, resp = X2),
prior(gamma(1, 1), class = sigma, resp = X3),
prior(gamma(1, 1), class = sigma, resp = X4),
prior(gamma(1, 1), class = sigma, resp = X5),
prior(gamma(1, 1), class = sigma, resp = y),
prior(horseshoe(df = 1, df_global = 1, scale_slab = 20, df_slab = 4, par_ratio = 2/3), class = b, resp = y)
),
data = dt,
chains = 4, iter = 2000, warmup = 1000,
file = 'project/fits/brmtest_mv_missmodelfulldata_reghorseshoe'
)
# Export stan code
stancode_nomiss <- make_stancode(modmv_nomiss_reghorseshoe)
write(stancode_nomiss, file = 'project/stancode/nomiss_horseshoe_5taxa.stan')
stancode_miss <- make_stancode(modmv_miss_reghorseshoe)
write(stancode_miss, file = 'project/stancode/miss_horseshoe_5taxa.stan')
standata_miss <- make_standata(modmv_miss_reghorseshoe)