Skip to content

Commit 0c348ac

Browse files
maybe address #351
1 parent bc28842 commit 0c348ac

File tree

5 files changed

+75
-28
lines changed

5 files changed

+75
-28
lines changed

inst/starter_models/hiv/README.Rmd

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ Here is an example trajectory from this model with small [updates](https://canmo
115115

116116
```{r simulations}
117117
outputs = c(sprintf("I%s", 1:4), sprintf("A%s", 1:4))
118+
spec = mp_tmb_update(spec, default = list(lambda0 = 0.36, alpha = 0.2, n = 0.5))
118119
sim = (spec
119-
|> mp_tmb_update(default = list(lambda0 = 0.36, n = 0.2))
120120
|> mp_rk4()
121121
|> mp_simulator(time_steps = 50L, outputs)
122122
)
@@ -140,8 +140,7 @@ To illustrate calibration, we simulate 20 years of data from a population of `1e
140140
set.seed(1L)
141141
spec_for_cal = (spec
142142
|> mp_tmb_update(
143-
default = list(lambda0 = 0.38, n = 0.2)
144-
, inits = list(
143+
inits = list(
145144
S = 1e7 - 4000
146145
, I1 = 1000, I2 = 1000, I3 = 1000, I4 = 1000
147146
, A1 = 0 , A2 = 0 , A3 = 0 , A4 = 0
@@ -154,6 +153,7 @@ spec_for_cal = (spec
154153
))
155154
)
156155
simulated_data = (spec_for_cal
156+
|> mp_rk4()
157157
|> mp_simulator(time_steps = 20L, c("treated", "untreated"))
158158
|> mp_trajectory()
159159
|> mutate(value = rpois(n(), value))
@@ -169,17 +169,17 @@ simulated_data = (spec_for_cal
169169
```
170170

171171

172-
We calibrate this model data simulated from it, but start away from the true parameter values by specifying that the default `lambda0 = 0.2` and `n = 0.5`. We assume a Poisson likelihood to match the Poisson noise that we used to produce the simulated data. For numerical stability, we optimize `lambda0` and `n` on the log and logit scales respectively.
172+
We calibrate this model data simulated from it, but start away from the true parameter values by specifying that the default `lambda0 = 0.2` and `alpha = 0.5`. We assume a Poisson likelihood to match the Poisson noise that we used to produce the simulated data. For numerical stability, we optimize `lambda0` and `alpha` on log scales.
173173
```{r calibration}
174174
calibrator = (spec_for_cal
175-
|> mp_tmb_update(default = list(lambda0 = 0.2, n = 0.5))
175+
|> mp_tmb_update(default = list(lambda0 = 0.2, alpha = 0.5))
176176
|> mp_tmb_calibrator(
177177
data = simulated_data
178178
, traj = list(
179179
treated = mp_pois()
180180
, untreated = mp_pois()
181181
)
182-
, par = c("log_lambda0", "logit_n")
182+
, par = c("log_lambda0", "log_alpha")
183183
)
184184
)
185185
mp_optimize(calibrator)
@@ -190,13 +190,30 @@ The convergence code is `0`, which is good.
190190
mp_optimizer_output(calibrator)$convergence
191191
```
192192

193-
The true parameters, `lambda0 = 0.38` and `n = 0.2`, are in the confidence intervals, although the confidence interval for `n` is quite wide.
193+
But the covariance matrix of the parameter estimates indicate that `lambda0` and `alpha` are highly correlated, indicating that there is little information in the data allowing us to disintangle baseline transmission, `lambda0`, from heterogeneity and mixing, `alpha`.
194+
```{r correlation}
195+
covmat = mp_tmb_fixef_cov(calibrator)
196+
print(covmat)
197+
cov2cor(covmat)
198+
```
199+
200+
Nevertheless, the true parameters, `lambda0 = 0.36` and `alpha = 0.2`, are in the confidence intervals, although the confidence interval for `alpha` is quite wide.
194201
```{r estimates}
195-
(mp_tmb_coef(calibrator, conf.int = TRUE)
196-
|> select(-term, -row, -col, -type)
202+
true_coefs = (spec_for_cal
203+
|> mp_default()
204+
|> filter(matrix %in% c("alpha", "lambda0"))
205+
|> select(-row, -col)
206+
|> rename(mat = matrix, true = value)
197207
)
208+
estimated_coefs = (calibrator
209+
|> mp_tmb_coef(, conf.int = TRUE)
210+
|> select(-term, -row, -col, -type)
211+
)
212+
left_join(estimated_coefs, true_coefs)
198213
```
199214

215+
(note that `default` gives the starting values for the optimizer, `estimate` the estimated value, and `true` the value used in simulation.)
216+
200217
The simulated data (black) that we fit to matches the predictions of the fitted model (red) with 95% confidence intervals for the point prediction).
201218
```{r fit, fig.width = 4}
202219
(calibrator

inst/starter_models/hiv/README.md

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ solver](https://canmod.github.io/macpan2/reference/state_updates).
112112

113113
``` r
114114
outputs = c(sprintf("I%s", 1:4), sprintf("A%s", 1:4))
115+
spec = mp_tmb_update(spec, default = list(lambda0 = 0.36, alpha = 0.2, n = 0.5))
115116
sim = (spec
116-
|> mp_tmb_update(default = list(lambda0 = 0.36, n = 0.2))
117117
|> mp_rk4()
118118
|> mp_simulator(time_steps = 50L, outputs)
119119
)
@@ -143,8 +143,7 @@ and zero untreated individuals. We add Poisson noise to these variables.
143143
set.seed(1L)
144144
spec_for_cal = (spec
145145
|> mp_tmb_update(
146-
default = list(lambda0 = 0.38, n = 0.2)
147-
, inits = list(
146+
inits = list(
148147
S = 1e7 - 4000
149148
, I1 = 1000, I2 = 1000, I3 = 1000, I4 = 1000
150149
, A1 = 0 , A2 = 0 , A3 = 0 , A4 = 0
@@ -157,6 +156,7 @@ spec_for_cal = (spec
157156
))
158157
)
159158
simulated_data = (spec_for_cal
159+
|> mp_rk4()
160160
|> mp_simulator(time_steps = 20L, c("treated", "untreated"))
161161
|> mp_trajectory()
162162
|> mutate(value = rpois(n(), value))
@@ -175,39 +175,39 @@ simulated_data = (spec_for_cal
175175

176176
We calibrate this model data simulated from it, but start away from the
177177
true parameter values by specifying that the default `lambda0 = 0.2` and
178-
`n = 0.5`. We assume a Poisson likelihood to match the Poisson noise
178+
`alpha = 0.5`. We assume a Poisson likelihood to match the Poisson noise
179179
that we used to produce the simulated data. For numerical stability, we
180-
optimize `lambda0` and `n` on the log and logit scales respectively.
180+
optimize `lambda0` and `alpha` on log scales.
181181

182182
``` r
183183
calibrator = (spec_for_cal
184-
|> mp_tmb_update(default = list(lambda0 = 0.2, n = 0.5))
184+
|> mp_tmb_update(default = list(lambda0 = 0.2, alpha = 0.5))
185185
|> mp_tmb_calibrator(
186186
data = simulated_data
187187
, traj = list(
188188
treated = mp_pois()
189189
, untreated = mp_pois()
190190
)
191-
, par = c("log_lambda0", "logit_n")
191+
, par = c("log_lambda0", "log_alpha")
192192
)
193193
)
194194
mp_optimize(calibrator)
195195
#> $par
196196
#> params params
197-
#> -0.9526784 -1.8874755
197+
#> -0.9303394 1.5411393
198198
#>
199199
#> $objective
200-
#> [1] 194.0068
200+
#> [1] 190.3486
201201
#>
202202
#> $convergence
203203
#> [1] 0
204204
#>
205205
#> $iterations
206-
#> [1] 44
206+
#> [1] 101
207207
#>
208208
#> $evaluations
209209
#> function gradient
210-
#> 51 45
210+
#> 119 102
211211
#>
212212
#> $message
213213
#> [1] "relative convergence (4)"
@@ -220,19 +220,49 @@ mp_optimizer_output(calibrator)$convergence
220220
#> [1] 0
221221
```
222222

223-
The true parameters, `lambda0 = 0.38` and `n = 0.2`, are in the
224-
confidence intervals, although the confidence interval for `n` is quite
225-
wide.
223+
But the covariance matrix of the parameter estimates indicate that
224+
`lambda0` and `alpha` are highly correlated, indicating that there is
225+
little information in the data allowing us to disintangle baseline
226+
transmission, `lambda0`, from heterogeneity and mixing, `alpha`.
226227

227228
``` r
228-
(mp_tmb_coef(calibrator, conf.int = TRUE)
229-
|> select(-term, -row, -col, -type)
229+
covmat = mp_tmb_fixef_cov(calibrator)
230+
print(covmat)
231+
#> log_lambda0 log_alpha
232+
#> log_lambda0 0.03654861 0.3828976
233+
#> log_alpha 0.38289764 4.0114311
234+
cov2cor(covmat)
235+
#> log_lambda0 log_alpha
236+
#> log_lambda0 1.0000000 0.9999944
237+
#> log_alpha 0.9999944 1.0000000
238+
```
239+
240+
Nevertheless, the true parameters, `lambda0 = 0.36` and `alpha = 0.2`,
241+
are in the confidence intervals, although the confidence interval for
242+
`alpha` is quite wide.
243+
244+
``` r
245+
true_coefs = (spec_for_cal
246+
|> mp_default()
247+
|> filter(matrix %in% c("alpha", "lambda0"))
248+
|> select(-row, -col)
249+
|> rename(mat = matrix, true = value)
250+
)
251+
estimated_coefs = (calibrator
252+
|> mp_tmb_coef(, conf.int = TRUE)
253+
|> select(-term, -row, -col, -type)
230254
)
231-
#> mat default estimate std.error conf.low conf.high
232-
#> 1 lambda0 0.2 0.3857066 0.02514815 0.339436497 0.4382839
233-
#> 2 n 0.5 0.1315326 0.23314560 0.002765574 0.8921398
255+
left_join(estimated_coefs, true_coefs)
256+
#> Joining with `by = join_by(mat)`
257+
#> mat default estimate std.error conf.low conf.high true
258+
#> 1 lambda0 0.2 0.3944198 0.07540396 0.27116189 0.5737052 0.36
259+
#> 2 alpha 0.5 4.6699078 9.35315167 0.09214556 236.6694535 0.20
234260
```
235261

262+
(note that `default` gives the starting values for the optimizer,
263+
`estimate` the estimated value, and `true` the value used in
264+
simulation.)
265+
236266
The simulated data (black) that we fit to matches the predictions of the
237267
fitted model (red) with 95% confidence intervals for the point
238268
prediction).
6.82 KB
Loading
5.99 KB
Loading
-3.57 KB
Loading

0 commit comments

Comments
 (0)