-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOR_Sim.R
More file actions
280 lines (235 loc) · 8.66 KB
/
OR_Sim.R
File metadata and controls
280 lines (235 loc) · 8.66 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# Sys.setenv(LANG = "en")
# remotes::install_github("bbolker/bbmle")
library(dplyr)
library(tidyr)
library(ggplot2); theme_set(theme_bw())
library(viridis)
library(bbmle)
library(tidyverse)
# Set Seeds
set.seed(13519)
## Initial true values:
T_B <- 0.04 ## uninf testing prob
T_Y <- 0.5 ## inf testing prob
B <- T_B/(1-T_B) ## baseline odds of testing
Phi <- (T_Y/(1-T_Y))/B ## inf vs uninf testing odds ratio
print(B)
print(Phi)
Y_0 <- 1e-4 ## initial prevalence
N <- 1e6 ## pop size
NY_0 <- N*Y_0 ## initial number infected
r <- log(2)/3 ## growth rate (doubling time = 3)
tmax <- 39 ## max simulation time
t <- c(0:tmax)
pts <- length(t) ## number of time points
true_param <- c("log_B"=log(B),"log_Phi"=log(Phi),"logY_0"=log(Y_0),"r"=r)
## Simulate the data
dat <- tibble(t=t
## , pY = pmin(Y_0*exp(r*t), 1) ## Exponential growth
, pY = 1/(1+(1/Y_0-1)*exp(-r*t))
## , NY = rbinom(pts, N, pY)
, NY = round(N*pY)
, posTests = rbinom(pts, NY, T_Y)
, negTests = rbinom(pts, N-NY, T_B)
)
matplot(dat$t, dat[,-1], type = "l", log = "y")
legend("center", col = 1:4, lty = 1:4,
legend = names(dat)[-1])
long_dat <- (dat
|> select(-pY)
|> pivot_longer(-t)
)
print(ggplot(long_dat)
+ aes(t, value, color=name)
+ geom_line()
+ scale_y_log10()
)
### function to calculate negative log-likelihood:
LL <- function(log_B, log_Phi, logY_0, r, dat, N, tmax, debug = FALSE,
debug_plot = FALSE, plot_sleep = 1) {
Y_0 <- exp(logY_0)
B <- exp(log_B)
Phi <- exp(log_Phi)
T_B <- B/(1+B)
T_Y <- B*Phi/(1+B*Phi)
t <- c(0:tmax)
pts <- length(t)
## simulated time series
sim <- tibble(t=t
##, pY = pmin(Y_0*exp(r*t), 1)
, pY = 1/(1+(1/Y_0-1)*exp(-r*t))
### round here???
, NY = round(N*pY)
# , NY = rbinom(pts, N, pY)
)
if(max(sim$pY) == 1 || any(sim$NY<dat$posTests) || any((N-sim$NY)<dat$negTests) || any(N<sim$NY)) return(NA)
if (any(sim$NY<dat$posTests)) {
cat("Underestimated infected population, pos tests > infected population", "\n")
}
if (any((N-sim$NY)<dat$negTests)) {
cat("Overestimated infected population, neg tests > uninfected population", "\n")
}
postest_nll <- -sum(dbinom(dat$posTests, sim$NY, T_Y, log = TRUE))
negtest_nll <- -sum(dbinom(dat$negTests, N-sim$NY, T_B, log = TRUE))
out <- postest_nll + negtest_nll
if (debug) {
cat(B, Phi, logY_0, r, postest_nll, negtest_nll,
out, "\n")
}
if (debug_plot) {
par(mfrow= c(1,2), las = 1)
ylim <- range(c(dat$posTests, dat$negTests,
sim$NY*T_Y, (N-sim$NY)*T_B))
matplot(dat$t, dat[c("posTests", "negTests")], type = "p",
pch = 1:2, log = "y",
ylim = ylim)
matlines(dat$t, cbind(sim$NY*T_Y, (N-sim$NY)*T_B))
LLhist <<- c(LLhist, out)
plot(LLhist - min(LLhist) + 1e-3, type = "b", log = "y")
Sys.sleep(plot_sleep)
}
return(out)
}
real_ML <- LL(log(B),log(Phi),log(Y_0),r,dat,N,tmax)
print(real_ML)
LL(log(B),log(Phi),log(Y_0),0.23,dat,N,tmax)
LLhist <- numeric(0)
fit1 <- mle2(LL
, start = list(log_B=log(B)
, log_Phi=log(Phi)
, logY_0=log(Y_0)
, r=r)
, data = list(dat=dat
, N=N
, tmax=tmax
, debug = F
, debug_plot = F)
, control = list(maxit=10000
### parscale??
#, parscale = c(log(B), log(Phi), log(Y_0), r)
)
, method = "Nelder-Mead"
#, hessian.method = "optimHess"
, skip.hessian = TRUE ## TRUE to skip Hessian calculation ...
)
## ?? not getting Re(ev) error any more?
print(real_ML)
print(-1*logLik(fit1))
coef(fit1)
true_param
### Testing with parameters away from real value
### Disturb B
param <- list(log_B=log(0.01), log_Phi=log(Phi), logY_0=log(Y_0), r=r)
# param <- list(log_B=log(0.1), log_Phi=log(Phi), logY_0=log(Y_0), r=r)
## Identify true_param pretty well after shift to logistic.
## Hessian still not work
## Converge problem does not repeat for t=59
## When t=39 fit1 convergence failure: code=10 (degenerate Nelder-Mead simplex)
## However, fit2 works without error for t=39: conv problem is not necessary caused by initial values
### Disturb Phi
# param <- list(log_B=log(B), log_Phi=log(Phi+20), logY_0=log(Y_0), r=r)
# param <- list(log_B=log(B), log_Phi=log(Phi-15), logY_0=log(Y_0), r=r)
## Identify true_param pretty well after shift to logistic.
## Converge problem does not repeat for t=59, t=39
### Disturb Y_0
# param <- list(log_B=log(B), log_Phi=log(Phi), logY_0=log(Y_0+2e-4), r=r)
## Identify true_param pretty well after shift to logistic.
## Converge problem does not repeat for t=59, t=39
# param <- list(log_B=log(B), log_Phi=log(Phi), logY_0=log(Y_0-5e-5), r=r)
# function cannot be evaluated at initial parameters
# param <- list(log_B=log(B), log_Phi=log(Phi), logY_0=log(Y_0-3e-5), r=r)
# Works now
### Disturb r
# param <- list(log_B=log(B), log_Phi=log(Phi), logY_0=log(Y_0), r=r+0.2)
## function cannot be evaluated at initial parameters
# param <- list(log_B=log(B), log_Phi=log(Phi), logY_0=log(Y_0), r=r+0.1)
## t=39 not converging well, close but not enough log_lik=-547 while real param log_lik=-469
## increase maxit does not help
## t=59 function cannot be evaluated at initial parameters
# param <- list(log_B=log(B), log_Phi=log(Phi), logY_0=log(Y_0), r=r+0.05)
## indentify the init_param well
# param <- list(log_B=log(B), log_Phi=log(Phi), logY_0=log(Y_0), r=r-0.2)
## function cannot be evaluated at initial parameters
# param <- list(log_B=log(B), log_Phi=log(Phi), logY_0=log(Y_0), r=r-0.02)
## identify the init_param well
## sensitive to r
fit2 <- do.call(mle2,list(LL
, start = param
, data = list(dat=dat
, N=N
, tmax=tmax
, debug = F
, debug_plot = F)
, control = list(maxit=15000
### parscale??
#, parscale = c(log(B), log(Phi), log(Y_0), r)
)
, method = "Nelder-Mead"
, skip.hessian = TRUE ## TRUE to skip Hessian calculation ...
))
print(real_ML)
print(-1*logLik(fit2))
#print(fit2)
#summary(fit2)
param
coef(fit2)
true_param
# fit_fun<-function(logB,logPhi,logY_0,r){
# param <- list(log_B=logB, log_Phi=logPhi, logY_0=logY_0, r=r)
# fit <- do.call(mle2,list(LL
# , start = param
# , data = list(dat=dat
# , N=N
# , tmax=tmax
# , debug = F
# , debug_plot = F)
# , control = list(maxit=15000
# )
# , method = "Nelder-Mead"
# , skip.hessian = TRUE ## TRUE to skip Hessian calculation ...
# ))
# out <- as.numeric(-1*logLik(fit))
# return(out)
# }
#
# #tryCatch(fit_fun(log(B),log(Phi),log(Y_0),r-0.01),error=function(e){NaN})
#
# print(c(T_B,T_Y,B,Phi,Y_0,r))
#
# param_mat <- (expand.grid(T_B=seq(from=1e-2, to=9e-2, by=1e-2),
# T_Y=seq(from=1e-2,to=1,by=1e-2),
# Y_0=c(Y_0),
# r=c(r)
# )
# %>% as_tibble()
# %>% mutate(logB=log(T_B/(1-T_B)))
# %>% mutate(logPhi=log((T_Y/(1-T_Y))/B))
# %>% mutate(logY_0=log(Y_0))
# %>% mutate(LogLik=tryCatch(fit_fun(logB,logPhi,logY_0,r),error=function(e){Inf}))
# )
# param_mat
# which(param_mat$LogLik==Inf)
## re-do Hessian calculation with optimHess() ...
fix_hessian <- function(fit) {
## construct vectorized log-likelihood function
lfun <- function(p) {
do.call(c(as.list(p), fit@data), what = fit@minuslogl)
}
hh <- optimHess(coef(fit), fn = lfun)
fit@vcov <- solve(hh)
return(fit)
}
fit2H <- fix_hessian(fit2)
summary(fit2H)
## now try optimHess to see why we get NA values ...
fit2@details$hessian
## hmm, we get a finite hessian from this ...
quit()
hh <- optimHess(coef(fit1), fn = lfun)
vv <- solve(hh)
print(cov2cor(vv))
print(sdvec <- sqrt(diag(vv)))
## mle2 uses numDeriv::hessian() internally instead of optimHess() ...
numDeriv::hessian(lfun, coef(fit1))
warnings()
## still not sure why it's so hard to get a valid Hessian ...