-
Notifications
You must be signed in to change notification settings - Fork 1
/
workflow_check_bias_Poiss.R
185 lines (146 loc) · 5.35 KB
/
workflow_check_bias_Poiss.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
rm(list=ls(all.names=TRUE))
simulate_negbin <- function(R,n,s,rho,t_max,overdisp) {
# check input
if (length(R)!=1 && length(R)!=n) warning('R must be a vector, and its size must be either 1 or n')
if (length(s)!=1 && length(s)!=n) warning('s must be a vector, and its size must be either 1 or n')
if (length(rho)!=1 && length(rho)!=n) warning('rho must be a vector, and its size must be either 1 or n')
if ((length(n)!=1 | n==0 | n!=round(n))[1]) warning('n must be a positive integer')
if ((length(t_max)!=1 | t_max==0 | t_max!=round(t_max))[1]) warning('t_max must be a positive integer')
# declare incidence matrices
N <- matrix(NA,n,t_max)
N_obs_full <- matrix(NA,n,t_max)
# initial values
N[,1] <- s
N_obs_full[,1] <- rbinom(n,N[,1],rho)
# simulate the Poisson branching process and reporting
for (i in 2:t_max){
# N[,i] <- rpois(n,R*N[,i-1])
N[,i] <- rnbinom(n,mu = R*N[,i-1],size = overdisp)
N_obs_full[,i] <- rbinom(n,N[,i],rho)
}
# check all simulated outbreaks are extinct
n_ongoing <- n - sum(N[,t_max]==0)
if (n_ongoing>0) warning(
paste0('There are ',n_ongoing,' outbreaks that are not extinct,
consider increasing the number of time-steps (especially if R<1).'))
# calculate final sizes, true and observed
z <- rowSums(N)
z_obs_full <- rowSums(N_obs_full)
# correct for unobserved outbreaks
f <- which(z_obs_full==0)
if (length(f)>0){
N_obs <- N_obs_full[-f,]
z_obs <- z_obs_full[-f]
}else{
N_obs <- N_obs_full
z_obs <- z_obs_full
}
# output
return( list(true_incidence = N, reported_incidence = N_obs_full, observed_incidence = N_obs,
true_size = z, reported_size = z_obs_full, observed_size = z_obs) )
}
# source('simulate_poisson.R')
R=.92
s=1
rho=.2
t_max=5e3
Nrep <- 1e2
# Sizes <- rep(NA,Nrep)
# imp <- Rs
# imp_in_95CI <- rep(0,nrep)
n_imp <- c(30,50,70)
nrep <- length(n_imp)
Rs <- matrix(NA,nrep,Nrep)
R_in_95CI <- rep(0,nrep)
imp <- Rs
imp_in_95CI <- R_in_95CI
for (n in 1:nrep){
print(n)
for (k in 1:Nrep){
# print(k)
# Sim_I <- simulate_negbin(R,n_imp[n],s,rho,t_max,overdisp = .5)
Sim_I <- simulate_poisson(R,n_imp[n],s,rho,t_max)
Sizes <- Sim_I$true_size
# hist(Sim_I$true_size)
# mean(Sim_I$true_size)
y_obs <- Sim_I$observed_size
# source('alpha_poisson.R')
# source('element_Lhood_poisson.R')
# source('f_z_poiss.R')
# source('proba_ext.R')
# source('proba_observation.R')
# source('profile_likelihood.R')
# source('R_eff_poisson.R')
# source('theta_max_likelihood.R')
# source('import.R')
profile <- profile_likelihood(y_obs = y_obs,
rho = rho,
accuracy = 0.01,
max_R = 20)
# plot(profile$theta,profile$Likelihood)
R_estimate <- theta_max_likelihood(theta = profile$theta,
likelihood = profile$Likelihood,
threshold_CI = 0.95)
# c(R,R_estimate$theta_max_likelihood,R_estimate$lower_theta,R_estimate$upper_theta)
Rs[n,k] <- R_estimate$theta_max_likelihood
R_in_95CI[n] <- R_in_95CI[n] +
((R_estimate$upper_theta>=R) &
(R_estimate$lower_theta<=R))
import <- import(y_obs = y_obs,
rho = rho,
profile = profile,
threshold_z = 1e3,
threshold_import = 1e3,
CI = 0.95)
# c(n,length(y_obs)+import$theta_max_likelihood,
# length(y_obs)+import$lower_theta,
# length(y_obs)+import$upper_theta)
imp[n,k] <- length(y_obs)+import$theta_max_likelihood
imp_in_95CI[n] <- imp_in_95CI[n] +
(( (length(y_obs)+import$upper_theta) >=n_imp[n]) &
( (length(y_obs)+import$lower_theta)<=n_imp[n]))
}
# hist(R-Rs[n,])
# c(sum(R>Rs[n,])/Nrep, R_in_95CI/Nrep)
# c(median(Rs[n,]), mean(Rs[n,]))
#
# x <- seq(0,max(Sizes))
# Px <- s*x^(x-s-1)*R^(x-s)*exp(-x*R)/factorial(x-s)
# hist(Sizes,x+.5,freq = FALSE)
# lines(x,Px)
#
#
# hist(imp-rho)
# c(sum(imp>rho)/Nrep, imp_in_95CI/Nrep)
#
# sum(profile$theta*exp(profile$Likelihood)/sum(exp(profile$Likelihood)))
}
# meanRs <- apply(Rs,1,mean)
# plot(1:n,meanRs,ylim = c(0,1))
# lines(c(0,n),c(R,R),col=rgb(1,0,0))
layout(matrix(1:2,1,2))
medianRs <- apply(Rs,1,quantile,c(.5,.025,.975))
library(Hmisc)
errbar(n_imp,medianRs[1,], medianRs[2,],medianRs[3,],ylim = c(0,1),
ylab = 'distribution of R estimates',xlab = 'nb imported cases')
lines(c(0,n_imp[n]),c(R,R),col=rgb(1,0,0))
plot(n_imp,R_in_95CI/Nrep,pch=16,
ylim = c(0,1),ylab = 'Proportion of 95%CI including true R',
xlab = 'nb imported cases')
apply((Rs-R)/R,1,median)
# importations
layout(matrix(1:2,1,2))
medianimp <- apply(imp,1,quantile,c(.5,.025,.975))
library(Hmisc)
errbar(n_imp,medianimp[1,], medianimp[2,],medianimp[3,],ylim = c(0,100),
ylab = 'distribution of importation estimates',xlab = 'nb imported cases')
lines(n_imp,n_imp,col=rgb(1,0,0))
plot(n_imp,imp_in_95CI/Nrep,pch=16,
ylim = c(0,1),ylab = 'Proportion of 95%CI including true importation',
xlab = 'nb imported cases')
apply((imp-matrix(n_imp,3,Nrep))/matrix(n_imp,3,Nrep),1,median)
save.image('Pois_check.Rdata')
# # check fat-tail
# hist(Sizes,x+.5,freq = FALSE)
# lines(x,Px)
# lines(1+x,dnbinom(x = x,size = 1, mu = R),col=rgb(1,0,0))