This repository has been archived by the owner on Jul 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4a_L_ri.R
150 lines (142 loc) · 6 KB
/
4a_L_ri.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
## likelihood of offspring number ##############################################
function (i, thetaSS, R0, size, delta, epsilon, Data) {
# preallocate
L_ri_1 = 0
L_ri_2 = 0
# observed offspring number
ri_df = as.data.frame(table(factor(Data$vi, levels = Data$i)))
ri = ri_df$Freq
# hospitalization probability
ph = 1
# if ( Data_obs[i,3] | Data_obs[i,5] ) { ph = 1
# } else { ph = 18/22 }
# categories in L_r
# i: "xOOx", "xOMx", "OMOx", "MMOx", "OMMO(D)", "OMMM(D)", "MMMO(D)", "MMMO(R)"
if ( Data_obs[i,2] & Data_obs[i,3] ) { # i: xOOx (1) ====
type.i = "(1) xOOx "
# data of observed cases
tauSH = Data$tauiSH[i]
# exponentially decreasing R
R = R0*exp(-delta*Data$tiS[i])
# numerator and denominator
L_ri_1 = pmf_r(ri[i], R_hat(tauSH, thetaSS, R, epsilon, ph), size)
L_ri_2 = 1
} else if ( Data_obs[i,2] & !Data_obs[i,3] ) { # i: xOMx (2) # unknown hospitalization, except Case 1 ====
type.i = "(2) xOMx "
# intervals
a = 0
b = Data_BH2(i) - Data$tiS[i]
# change of variable
tauSH = a:b
# exponentially decreasing R
R = R0*exp(-delta*Data$tiS[i])
# numerator and denominator
if (i == 1) {
L_ri_1 = pmf_r(ri[i], R, size)
L_ri_2 = 1
}
else {
L_ri_1 = sum( pmf_SH(tauSH)*pmf_r(ri[i], R_hat(tauSH, thetaSS, R, epsilon, ph), size) )
L_ri_2 = sum( pmf_SH(tauSH) )
}
} else if ( Data_obs[i,1] & !Data_obs[i,2] & Data_obs[i,3] ) { # i: OMOx (3) ====
type.i = "(3) OMOx "
# intervals
a = 0
b = Data_BS2(i) - Data$tiE[i]
# change of variable
tauES = a:b
tauSH = Data$tiH[i] - Data$tiE[i] - tauES
# exponentially decreasing R
R = R0*exp(-delta*(Data$tiE[i] + tauES))
# numerator and denominator
L_ri_1 = sum( pmf_ES(tauES)*pmf_r(ri[i], R_hat(tauSH, thetaSS, R, epsilon, ph), size) )
L_ri_2 = sum( pmf_ES(tauES) )
} else if (!Data_obs[i,1] & !Data_obs[i,2] & Data_obs[i,3] ) { # i: MMOx (4) ====
type.i = "(4) MMOx "
# intervals
a = Data$tiH[i] - Data_BS2(i)
b = Data$tiH[i] - Data_BS1(i)
# change of variable
tauSH = a:b
# exponentially decreasing R
R = R0*exp(-delta*(Data$tiH[i] - tauSH))
# numerator and denominator
L_ri_1 = sum( pmf_SH(tauSH)*pmf_r(ri[i], R_hat(tauSH, thetaSS, R, epsilon, ph), size) )
L_ri_2 = sum( pmf_SH(tauSH) )
} else if ( Data_obs[i,1] & !Data_obs[i,2] & !Data_obs[i,3] & Data_obs[i,4]) { # i: OMMO(D) (5D) # unknown hospitalization ====
type.i = "(5) OMMO(D)"
# intervals
for (tauES in 0:(Data_BS2(i) - Data$tiE[i])) {
for (tauHD in 0:(Data$tiD[i] - Data$tiE[i] - tauES)) {
# change of variable
tauSH = Data$tiD[i] - tauHD - Data$tiE[i] - tauES
# exponentially decreasing R
R = R0*exp(-delta*(Data$tiE[i] + tauES))
# numerator and denominator
L_ri_1 = L_ri_1 + pmf_HD(tauHD)*pmf_ES(tauES)*pmf_r(ri[i], R_hat(tauSH, thetaSS, R, epsilon, ph), size)
L_ri_2 = L_ri_2 + pmf_HD(tauHD)*pmf_ES(tauES)
}
}
} else if ( Data_obs[i,1] & !Data_obs[i,2] & !Data_obs[i,3] & !Data_obs[i,4]) { # i: OMMM(D) (6D) # unknown hospitalization ====
type.i = "(6) OMMM(D)"
# intervals
for (tauES in 0:(Data_BS2(i) - Data$tiE[i])) {
for (tauSH in 0:(Data_tEND - Data$tiE[i] - tauES)) {
# exponentially decreasing R
R = R0*exp(-delta*(Data$tiE[i] + tauES))
# numerator and denominator
L_ri_1 = L_ri_1 + pmf_SH(tauSH)*pmf_ES(tauES)*pmf_r(ri[i], R_hat(tauSH, thetaSS, R, epsilon, ph), size)
L_ri_2 = L_ri_2 + pmf_SH(tauSH)*pmf_ES(tauES)
}
}
} else if (!Data_obs[i,1] & !Data_obs[i,2] & !Data_obs[i,3] & Data_obs[i,4]) { # i: MMMO(D) (7D) # unknown hospitalization ====
type.i = "(7) MMMO(D)"
# intervals, 1st part
for (tauHD in 0:(Data$tiD[i] - Data_BS2(i))) {
for (tauSH in (Data$tiD[i] - Data_BS2(i) - tauHD):(Data$tiD[i] - Data_BS1(i) - tauHD)) {
# exponentially decreasing R
R = R0*exp(-delta*(Data$tiD[i] - tauHD - tauSH))
# numerator and denominator
L_ri_1 = L_ri_1 + pmf_SH(tauSH)*pmf_HD(tauHD)*pmf_r(ri[i], R_hat(tauSH, thetaSS, R, epsilon, ph), size)
L_ri_2 = L_ri_2 + pmf_SH(tauSH)*pmf_HD(tauHD)
}
}
# intervals, 2nd part
for (tauHD in (Data$tiD[i] - Data_BS2(i) + 1):(Data$tiD[i] - Data_BS1(i))) {
for (tauSH in 0:(Data$tiD[i] - Data_BS1(i) - tauHD)) {
# exponentially decreasing R
R = R0*exp(-delta*(Data$tiD[i] - tauHD - tauSH))
# numerator and denominator
L_ri_1 = L_ri_1 + pmf_SH(tauSH)*pmf_HD(tauHD)*pmf_r(ri[i], R_hat(tauSH, thetaSS, R, epsilon, ph), size)
L_ri_2 = L_ri_2 + pmf_SH(tauSH)*pmf_HD(tauHD)
}
}
} else if (!Data_obs[i,1] & !Data_obs[i,2] & !Data_obs[i,3] & Data_obs[i,5]) { # i: MMMO(R) (7R) # unknown hospitalization ====
type.i = "(7) MMMO(R)"
# intervals, 1st part
for (tauHR in 0:(Data$tiR[i] - Data_BS2(i))) {
for (tauSH in (Data$tiR[i] - Data_BS2(i) - tauHR):(Data$tiR[i] - Data_BS1(i) - tauHR)) {
# exponentially decreasing R
R = R0*exp(-delta*(Data$tiR[i] - tauHR - tauSH))
# numerator and denominator
L_ri_1 = L_ri_1 + pmf_SH(tauSH)*pmf_HR(tauHR)*pmf_r(ri[i], R_hat(tauSH, thetaSS, R, epsilon, ph), size)
L_ri_2 = L_ri_2 + pmf_SH(tauSH)*pmf_HR(tauHR)
}
}
# intervals, 2nd part
for (tauHR in (Data$tiR[i] - Data_BS2(i) + 1):(Data$tiR[i] - Data_BS1(i))) {
for (tauSH in 0:(Data$tiR[i] - Data_BS1(i) - tauHR)) {
# exponentially decreasing R
R = R0*exp(-delta*(Data$tiR[i] - tauHR - tauSH))
# numerator and denominator
L_ri_1 = L_ri_1 + pmf_SH(tauSH)*pmf_HR(tauHR)*pmf_r(ri[i], R_hat(tauSH, thetaSS, R, epsilon, ph), size)
L_ri_2 = L_ri_2 + pmf_SH(tauSH)*pmf_HR(tauHR)
}
}
} else { print(i); break } # error(?)
# resulting value
L_ri = L_ri_1/L_ri_2
# output
return(L_ri)
}