-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoissonModelAge.stan
242 lines (206 loc) · 4.9 KB
/
PoissonModelAge.stan
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
functions{
real gamma_discrete_cdf(int y1, real alpha, real beta, int maxv){
real a;
a= (gamma_cdf(y1, alpha, beta)-gamma_cdf(y1-1,alpha,beta))/(gamma_cdf(maxv, alpha, beta));
return(a);
}
real dresponse(int x, real lambda){
real a;
if(x==0)
a = 0;
if(x>0){
a=exp(poisson_lpmf(x| lambda))/(1-exp(-lambda)) ;
}
return(a);
}
real dcrossreactivity(int x, int size, real lambda){
real a;
a = dresponse(x,lambda);
return(a);
}
real likelihood_titer(real pC,real pN,
int CHIKV, int ONNV, int m,
real s1C, real s1N,
real pCN, real pNC, real sCN, real sNC){
real A;
real B;
real P00;
real P10;
real P01;
real P11;
real P0;
real P1;
real P2;
real P3;
real P4;
real P5;
real V1;
V1=0;
P00 = (1-pC)*(1-pN);
P10 = pC*(1-pN);
P01 = (1-pC)*pN;
P11 = pC*pN;
P0=0;
P1=0;
if(CHIKV==0){
P0=1;
}
if(ONNV==0){
P1=1;
}
# Probability not infected
A = P00*P0*P1;
# Probability infected by CHIKV and not by ONNV
P2= dresponse(CHIKV, s1C);
P5= dcrossreactivity(ONNV, CHIKV, sCN);
A = A+ P10*P2*(P5*pCN+P1*(1-pCN));
# Probability infected by ONNV and not by CHIKV
P3 = dcrossreactivity(CHIKV, ONNV, sNC);
P4= dresponse(ONNV, s1N);
A = A+ P01*P4*(P3*pNC+P0*(1-pNC));
# Probability infected by both ONNV and CHIKV
# 1 if there is no cross reactivity
B=(1-pCN)*(1-pNC)*P2*P4;
# 2 if CHIKV induces a response in ONNV but not the opposite
V1=0;
for(k in 0:ONNV){
V1=V1+dresponse(k,s1N)*dcrossreactivity(ONNV-k, CHIKV, sCN) ;
}
B=B + pCN*(1-pNC)*P2*V1;
# 3 if ONNV induces a response in CHIKV but not the opposite
V1=0;
for(k in 0:CHIKV){
V1=V1+dresponse(k,s1C)*dcrossreactivity(CHIKV-k, ONNV,sNC) ;
}
B=B +(1-pCN)*pNC*P4*V1;
# 4 if both CHIKV and ONNV induce a cross-reactive response
V1=0;
for(kC in 0:CHIKV){
for(kN in 0:ONNV){
V1=V1+dresponse(kC, s1C)*dresponse(kN, s1N)*dcrossreactivity(CHIKV-kC, kN, sNC)*dcrossreactivity(ONNV-kN, kC, sCN);
}
}
B=B + pCN*pNC*V1;
A=A+B*P11;
return(A);
}
}
data {
int N; // number of data
int m; // maximal observable titer
int location[N];
int sex[N];
int CHIKV_obs[N];
int ONNV_obs[N];
int ages[N];
}
parameters {
// real<lower=0> alpha;
real<lower=0> s1C;
real<lower=0> s1N;
real<lower=0> sCN;
real<lower=0> sNC;
real<lower=0> pCN;
real<lower=0> pNC;
real<lower=0> qC;
real<lower=0> qN;
real<lower=0> qC_Martinique;
real<lower=0> qN_Martinique;
real sexC;
real sexN;
real locC;
real locN;
}
transformed parameters{
}
model {
real L;
int CHIKV;
int ONNV;
int LOC;
int SEX;
real A;
real pC;
real pN;
int age;
s1C ~ uniform(0,10);
s1N ~ uniform(0,10);
sNC ~ uniform(0,10);
sCN ~ uniform(0,10);
qC ~ uniform(0,10);
qN ~ uniform(0,10);
qC_Martinique ~ uniform(0,10);
qN_Martinique ~ uniform(0,10);
sexC ~ normal(0,3);
sexN ~ normal(0,3);
locC ~ normal(0,3);
locN ~ normal(0,3);
pNC ~ uniform(0,1);
pCN ~ uniform(0,1);
for(i in 1:N){
A = 0;
CHIKV = CHIKV_obs[i];
ONNV = ONNV_obs[i];
LOC = location[i];
SEX = sex[i];
age = ages[i];
if(LOC==1 && SEX==1){
pC= 1-exp(-age*qC);
pN= 1-exp(-age*qN);
}
if(LOC==1 && SEX==2){
pC=1-exp(-age*qC*exp(sexC));
pN=1-exp(-age*qN*exp(sexN));
}
if(LOC==2 && SEX==1){
pC=1-exp(-age*qC*exp(locC));
pN=1-exp(-age*qN*exp(locN));
}
if(LOC==2 && SEX==2){
pC=1-exp(-age*qC*exp(sexC)*exp(locC));
pN= 1-exp(-age*qN*exp(sexN)*exp(locN)) ;
}
if(LOC==3){
pC= 1-exp(-qC_Martinique); # no age available for Martinique
pN= 1-exp(-qN_Martinique);
}
A = likelihood_titer(pC,pN,
CHIKV, ONNV, m,
s1C, s1N,
pCN,pNC,sCN,sNC);
if(CHIKV==m && ONNV==m){
A=0;
for(i1 in m:10){
for(i2 in m:10){
A = A+ likelihood_titer(pC,pN,
i1, i2,m,
s1C, s1N,
pCN,pNC,
sCN,sNC);
}
}
}
if(CHIKV==m && ONNV<m){
A=0;
for(i1 in m:10){
A = A+ likelihood_titer(pC,pN, i1, ONNV,m,
s1C, s1N,
pCN,pNC,
sCN,sNC);
}
}
if(CHIKV<m && ONNV==m){
A=0;
for(i2 in m:10){
A = A+ likelihood_titer(pC,pN, CHIKV, i2,m,
s1C, s1N,
pCN,pNC,
sCN,sNC);
}
}
if(A==0){
A = -10000;
}
target+= log(A);
}
}