-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstan_functions_v2
162 lines (129 loc) · 4.64 KB
/
stan_functions_v2
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
## Stan code for bivariate model
## Output: p_vacc, p_inf, ratio_p_vacc_p_inf (risk ratio)
model_stan_Ngroup_2pvacc_priorData <- '
data {
int n_groups;
int A_plus_C[n_groups]; // Vitros+, Roche+
int B[n_groups]; // Vitros+, Roche-
int D[n_groups]; // Vitros-, Roche-
int n_draws;
real y_obs[n_groups,n_draws];
}
transformed data {
int N[n_groups];
for(i in 1:n_groups) N[i] = A_plus_C[i] + B[i] + D[i];
}
parameters {
real<lower=0.0, upper=1.0> p_vacc_given_inf[n_groups];
real<lower=0.0, upper=1.0> p_vacc_given_uninf[n_groups];
real<lower=0.0, upper=1.0> p_inf[n_groups];
real<lower=0.0, upper=1.0> mu_p_vacc[n_groups];
real<lower=0.0> kappa_p_vacc[n_groups];
}
transformed parameters {
simplex[3] p[n_groups];
for(i in 1:n_groups) {
p[i,1] = p_inf[i];
p[i,2] = (1.0-p_inf[i]) * p_vacc_given_uninf[i];
p[i,3] = (1.0-p_inf[i]) * (1.0-p_vacc_given_uninf[i]);
}
}
model {
for(i in 1:n_groups) {
target += binomial_lpmf(A_plus_C[i] | N[i], p[i,1]);
target += binomial_lpmf(B[i] | N[i], p[i,2]);
target += binomial_lpmf(D[i] | N[i], p[i,3]);
// Priors based on reported SF vacc data
target += beta_proportion_lpdf((p_inf[i]*p_vacc_given_inf[i]) + p[i,2] | mu_p_vacc[i], kappa_p_vacc[i]);
// Hyper-priors
target += beta_proportion_lpdf(y_obs[i] | mu_p_vacc[i], kappa_p_vacc[i]);
}
}
generated quantities {
real p_vacc[n_groups];
real ratio_p_vacc_p_inf[n_groups];
for(i in 1:n_groups) {
p_vacc[i] = (p_inf[i]*p_vacc_given_inf[i]) + p[i,2];
ratio_p_vacc_p_inf[i] = p_vacc[i] / p_inf[i];
}
}
'
## Stan code for bivariate model
## Output: p_vacc, p_inf, ratio_p_vacc_p_inf (risk ratio), RRR_vs_white (relative risk ratio), p_inf_vs_white, p_vacc_vs_white
model_stan_Ngroup_2pvacc_priorData_with_RRR_vs_white <- '
data {
int n_groups;
int A_plus_C[n_groups]; // Vitros+, Roche+
int B[n_groups]; // Vitros+, Roche-
int D[n_groups]; // Vitros-, Roche+
int n_draws;
real y_obs[n_groups,n_draws];
}
transformed data {
int N[n_groups];
for(i in 1:n_groups) N[i] = A_plus_C[i] + B[i] + D[i];
}
parameters {
real<lower=0.0, upper=1.0> p_vacc_given_inf[n_groups];
real<lower=0.0, upper=1.0> p_vacc_given_uninf[n_groups];
real<lower=0.0, upper=1.0> p_inf[n_groups];
real<lower=0.0, upper=1.0> mu_p_vacc[n_groups];
real<lower=0.0> kappa_p_vacc[n_groups];
}
transformed parameters {
simplex[3] p[n_groups];
for(i in 1:n_groups) {
p[i,1] = p_inf[i];
p[i,2] = (1.0-p_inf[i]) * p_vacc_given_uninf[i];
p[i,3] = (1.0-p_inf[i]) * (1.0-p_vacc_given_uninf[i]);
}
}
model {
for(i in 1:n_groups) {
target += binomial_lpmf(A_plus_C[i] | N[i], p[i,1]);
target += binomial_lpmf(B[i] | N[i], p[i,2]);
target += binomial_lpmf(D[i] | N[i], p[i,3]);
// Priors based on reported SF vacc data
target += beta_proportion_lpdf((p_inf[i]*p_vacc_given_inf[i]) + p[i,2] | mu_p_vacc[i], kappa_p_vacc[i]);
// Hyper-priors
target += beta_proportion_lpdf(y_obs[i] | mu_p_vacc[i], kappa_p_vacc[i]);
}
}
generated quantities {
real p_vacc[n_groups];
real ratio_p_vacc_p_inf[n_groups];
real RRR_vs_white[n_groups]; // Preserve the order of the groups
real p_inf_vs_white[8];
real p_vacc_vs_white[8];
for(i in 1:n_groups) {
p_vacc[i] = (p_inf[i]*p_vacc_given_inf[i]) + p[i,2];
ratio_p_vacc_p_inf[i] = p_vacc[i] / p_inf[i];
}
// Compared to: 18-64 & White
RRR_vs_white[1] = ratio_p_vacc_p_inf[1] / ratio_p_vacc_p_inf[4];
RRR_vs_white[2] = ratio_p_vacc_p_inf[2] / ratio_p_vacc_p_inf[4];
RRR_vs_white[3] = ratio_p_vacc_p_inf[3] / ratio_p_vacc_p_inf[4];
RRR_vs_white[4] = ratio_p_vacc_p_inf[4] / ratio_p_vacc_p_inf[4];
p_inf_vs_white[1] = p_inf[1] / p_inf[4];
p_inf_vs_white[2] = p_inf[2] / p_inf[4];
p_inf_vs_white[3] = p_inf[3] / p_inf[4];
p_inf_vs_white[4] = p_inf[4] / p_inf[4];
p_vacc_vs_white[1] = p_vacc[1] / p_vacc[4];
p_vacc_vs_white[2] = p_vacc[2] / p_vacc[4];
p_vacc_vs_white[3] = p_vacc[3] / p_vacc[4];
p_vacc_vs_white[4] = p_vacc[4] / p_vacc[4];
// Compared to: 65+ & White
RRR_vs_white[5] = ratio_p_vacc_p_inf[5] / ratio_p_vacc_p_inf[8];
RRR_vs_white[6] = ratio_p_vacc_p_inf[6] / ratio_p_vacc_p_inf[8];
RRR_vs_white[7] = ratio_p_vacc_p_inf[7] / ratio_p_vacc_p_inf[8];
RRR_vs_white[8] = ratio_p_vacc_p_inf[8] / ratio_p_vacc_p_inf[8];
p_inf_vs_white[5] = p_inf[5] / p_inf[8];
p_inf_vs_white[6] = p_inf[6] / p_inf[8];
p_inf_vs_white[7] = p_inf[7] / p_inf[8];
p_inf_vs_white[8] = p_inf[8] / p_inf[8];
p_vacc_vs_white[5] = p_vacc[5] / p_vacc[8];
p_vacc_vs_white[6] = p_vacc[6] / p_vacc[8];
p_vacc_vs_white[7] = p_vacc[7] / p_vacc[8];
p_vacc_vs_white[8] = p_vacc[8] / p_vacc[8];
}
'