You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Before reading this article on calibrating models to data, please first look at the [quickstart guide](https://canmod.github.io/macpan2/articles/quickstart) and the article on the [model library](https://canmod.github.io/macpan2/articles/example_models).
34
34
35
-
## Hello World
35
+
## Hello, World
36
36
37
37
We'll do the first thing you should always do when trying out a new fitting procedure: simulate clean, nice data from the model and see if you can recover something close to the true parameters.
@@ -87,14 +88,14 @@ The next step is to produce an object that can be calibrated through optimizatio
87
88
```{r calibrator}
88
89
sir_calibrator = mp_tmb_calibrator(sir_spec
89
90
, data = sir_prevalence
90
-
, traj = "I"
91
+
, traj = list(I = mp_poisson()),
91
92
, par = c("beta", "R")
92
93
, default = list(N = 300)
93
94
)
94
95
print(sir_calibrator)
95
96
```
96
97
97
-
Note that the calibrator has a few new expressions that deal with comparisons with data. In particular it is the objective function that we will optimize. But before that we can do a sanity check to make sure that the default values give a reasonable-looking trajectory.
98
+
The calibrator has a few new expressions that deal with comparisons with data; in particular, it defines the objective function that we will optimize. But before that we can do a sanity check to make sure that the default values give a reasonable-looking trajectory.
98
99
99
100
```{r plot1}
100
101
(sir_calibrator
@@ -107,32 +108,34 @@ Note that the calibrator has a few new expressions that deal with comparisons wi
107
108
108
109
### Step 2: do the fit
109
110
110
-
Doing the fit is straightforward.
111
+
Doing the fit is straightforward; this calls a nonlinear optimizer built into base R (`nlminb` by default), starting from the default values specified in the calibrator.
111
112
112
113
```{r sir_fit, results = "hide"}
113
114
mp_optimize(sir_calibrator)
114
115
```
115
116
116
-
Note that the `mp_optimize` function has modified the `sir_calibrator` object, which now contains the new fitted parameter values and the results of the optimization.
117
+
The `mp_optimize` function has **modified** the `sir_calibrator` object in place; it now contains the new fitted parameter values and the results of the optimization.
117
118
118
119
### Step 3: check the fit
119
120
120
-
We can print the results of the optimizer (`nlminb` in this case) using the `mp_optimizer_output` function. **Always check the value of the convergence code** (if it's not 0, then something *may* have gone wrong ...).
121
+
We can print the results of the optimizer (`nlminb` in this case) using the `mp_optimizer_output` function. **Always check the value of the convergence code** (if it's not 0, then something may have gone wrong ...).
121
122
122
123
```{r check_fit}
123
124
mp_optimizer_output(sir_calibrator)
124
125
mp_optimize(sir_calibrator)
125
126
mp_optimizer_output(sir_calibrator, what="all")
126
127
```
127
128
128
-
As mentioned above, the best-fit parameters are stored internally, and we can get information about
129
-
them using the `mp_tmb_coef` function. (Note that if you get a message about the `broom.mixed` package, please install it. `mp_tmb_coef` is a wrapper for this function).
129
+
As mentioned above, the best-fit parameters are stored internally; we can get information about
130
+
them using the `mp_tmb_coef` function. (If you get a message about the `broom.mixed` package, please install it. `mp_tmb_coef` is a wrapper for `broom.mixed::tidy()`).
Given these assumptions we choose $\mathbf b$ to maximize the resulting likelihood function, and use functionality from the `TMB` package (and sometimes the `tmbstan`/`rstan` packages) to do statistical inference on the fitted parameters and trajectories.
175
178
176
-
We recognize that this statistical model will often be overly restrictive. The `macpan2` package has a developer interface that is much more flexible, allowing for more detailed control over `TMB`, `tmbstan`, and `rstan`. This interface allows for arbitrary likelihood functions, prior distributions, parameter transformations, flexible parameter time-variation models, random effects and more. See [here](https://canmod.github.io/macpan2/articles/calibration_advanced.html) and [here](https://canmod.github.io/macpan2/articles/time_varying_parameters.html) for more information, although because these guides describe a developer interface the instructions may be unclear to some or many readers. Our plan is to continue adding interface layers, such as the interface described in this vignette, so that more of `macpan2` can be exposed to users.
179
+
This statistical model will often be too simple. The `macpan2` package has an extremely flexible developer interface that allows for more detailed control over `TMB`, `tmbstan`, and `rstan`. This interface allows for arbitrary likelihood functions, prior distributions, parameter transformations, flexible parameter time-variation models, random effects and more. See [here](https://canmod.github.io/macpan2/articles/calibration_advanced.html) and [here](https://canmod.github.io/macpan2/articles/time_varying_parameters.html) for more information, although because these guides describe a developer interface the instructions may be unclear to some or many readers. Our plan is to continue adding interface layers, such as the interface described in this vignette, so that more of `macpan2` can be exposed to users.
0 commit comments