-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOR_Sim_RandomTest.R
More file actions
209 lines (177 loc) · 6.41 KB
/
OR_Sim_RandomTest.R
File metadata and controls
209 lines (177 loc) · 6.41 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
# Sys.setenv(LANG = "en")
# remotes::install_github("bbolker/bbmle")
library(dplyr)
library(tidyr)
library(ggplot2); theme_set(theme_bw())
library(viridis)
library(bbmle)
library(broom)
source("mle2_tidy.R")
# Set Seeds
set.seed(13519)
## Random parameter values
# number of tests
n=100
### Constants for all tests
N <- 1e6 ## pop size
tmax <- 39 ## max simulation time/number of observations
### uninfected testing prob, from 0 to .25
T_B <- runif(2*n,0,0.25)
# 2n: Generate both true values and initial values for fitting
param_mat <- (expand.grid(T_B=T_B)
%>% as_tibble()
# Odds ratio part
## infected testing prob, must larger than T_B, smaller than 1
%>% mutate(T_Y = runif(2*n,T_B,1))
%>% mutate(B= T_B/(1-T_B))
%>% mutate(Phi = (T_Y/(1-T_Y))/B)
%>% mutate(log_B = log(B))
%>% mutate(log_Phi = log(Phi))
# Infection dynamic part
%>% mutate(Y_0 = runif(2*n,0,25e-3))
%>% mutate(log_Y0 = log(Y_0))
## initial value from 0 to .025 of population
# logistic growth rate
%>% mutate(r = log(2)/runif(2*n,1,5))
## random doubling time from 1 to 5
)
## separate simulation and fit values
param_true <- dplyr::slice(param_mat,1:n)
param_fit <- dplyr::slice(param_mat,(n+1):(2*n))
## Simulate the observed data
dat_func <- function(param_vec, tmax, N){
T_B <- param_vec$T_B
T_Y <- param_vec$T_Y
r <- param_vec$r
Y_0 <- param_vec$Y_0
t <- c(0:tmax)
dat <- tibble(t=t
, pY = 1/(1+(1/Y_0-1)*exp(-r*t)) ## Prevalence based on Logistic growth
, T_prop = (1-pY)*T_B+pY*T_Y ## Expected test proportion
, pos = pY*T_Y/T_prop ## Expected test positivity
, OT = rbinom(t,N,T_prop) ## Observed number of test
, OP = rbinom(t,OT,pos) ## Observed number of positive test
)
return(dat)
}
### function to calculate negative log-likelihood:
LL <- function(log_B, log_Phi, logY_0, r, dat, tmax, N, debug = FALSE) {
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) ## Exponential growth
, pY = 1/(1+(1/Y_0-1)*exp(-r*t)) ## Prevalence based on Logistic growth
, T_prop = (1-pY)*T_B+pY*T_Y ## Expected test proportion
, pos = pY*T_Y/T_prop ## Expected test positivity
)
# 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")
# }
ObsTest_nll <- -sum(dbinom(dat$OT, N, sim$T_prop, log = TRUE))
ObsPos_nll <- -sum(dbinom(dat$OP, dat$OT, sim$pos, log = TRUE))
out <- ObsTest_nll + ObsPos_nll
if (debug) {
cat(B, Phi, logY_0, r, ObsTest_nll, ObsPos_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)
}
### fitting procedure
fit_proc <- function(dat,param_fit,tmax,N,debug=F){
log_B <-param_fit$log_B
log_Phi <- param_fit$log_Phi
logY_0 <- param_fit$log_Y0
r <- param_fit$r
param <- list(log_B=log_B, log_Phi=log_Phi, logY_0=logY_0, r=r)
fit <- mle2(LL
, start = param
, data = list(dat=dat
, N=N
, tmax=tmax
, debug = debug)
, control = list(maxit=15000, reltol = 1e-10)
, method = "Nelder-Mead"
)
return(fit)
}
true_logLik <- c(0)
fit_logLik <- c(0)
fit_logB <- c(0)
fit_logPhi <- c(0)
fit_logY0 <- c(0)
fit_r <- c(0)
inCI <- c(0)
results_list <- list(0)
for (i in c(1:n)) {
dat <- dat_func(param_true[i,], tmax, N)
true_logLik[i] <- LL(param_true[i,]$log_B,param_true[i,]$log_Phi,param_true[i,]$log_Y0,param_true[i,]$r,dat,tmax,N)
fit <- fit_proc(dat,param_fit[i,],tmax,N)
fit_logLik[i] <- -logLik(fit)
fit_logB[i] <- coef(fit)[1]
fit_logPhi[i] <- coef(fit)[2]
fit_logY0[i] <- coef(fit)[3]
fit_r[i] <- coef(fit)[4]
true_value <- c(param_true[i,]$log_B,param_true[i,]$log_Phi,param_true[i,]$log_Y0,param_true[i,]$r)
results<- tidy(fit, conf.int = TRUE) |>
full_join(data.frame(term = tidy(fit)$term, true.value = true_value),
by = "term") |>
select(term, estimate, true.value, conf.low, conf.high)
results_list[[i]]<-results
inCI[i] <- min(results$true.value<=results$conf.high & results$true.value>=results$conf.low)
}
inCI
length(which(inCI==1))
length(which(is.na(inCI)))
NA_case <- which(is.na(inCI))
Zero_case <- which(inCI==0)
NA_case
Zero_case
### Checking cases
case <- 90
dat <- dat_func(param_true[case,], tmax, N)
dat
# matplot(dat$t, dat[,-1], type = "l", log = "y")
# legend("center", col = 1:4, lty = 1:4,
# legend = names(dat)[-1])
# fit<-fit_proc(dat, param_fit[case,], tmax, N,debug=T)
select(param_true[case,],-log_Y0)
select(param_fit[case,],-log_Y0)
#
# -logLik(fit)
# true_loglik
#
# param_true[case,]
fit_logLik[case]
true_logLik[case]
true_value <- c(param_true[case,]$log_B,param_true[case,]$log_Phi,param_true[case,]$log_Y0,param_true[case,]$r)
## one way to present results ...
results_case <- results_list[[case]]
# results_case
inCI_case <- min(results_case$true.value<=results_case$conf.high & results_case$true.value>=results_case$conf.low)
inCI_case
ggplot(results_case, aes(y = term)) +
geom_pointrange(aes(x = estimate, xmin = conf.low, xmax = conf.high)) +
geom_point(aes(x=true.value), colour = "red") +
facet_wrap(~term, ncol = 1, scale = "free")