diff --git a/articles/development_patterns.html b/articles/development_patterns.html index 954c1c2d..7bfa1c81 100644 --- a/articles/development_patterns.html +++ b/articles/development_patterns.html @@ -122,7 +122,7 @@
sir_flows_reader = CSVReader(sir_path, "flows.csv")
sir_flows_reader
-#> Classes 'CSVReader', 'Reader', 'Base' <environment: 0x55a2239e9a90>
+#> Classes 'CSVReader', 'Reader', 'Base' <environment: 0x55c7c6d47b60>
#> read : function ()
#> read_base : function ()
We get a print out of the S3
class of the object, which
@@ -241,7 +241,7 @@
The first line in this constructor uses the Base
function to create an environment
called self
.
The second line sets self
s S3 class
to
@@ -256,7 +256,7 @@
This looks identical to the first version, but now we have stored a
value for n
.
diff --git a/articles/likelihood_prior_specs.html b/articles/likelihood_prior_specs.html index b25f1269..58d3ed1d 100644 --- a/articles/likelihood_prior_specs.html +++ b/articles/likelihood_prior_specs.html @@ -185,8 +185,74 @@Distributional Parameter Trans
Priors on distributional parameters
-+-
We can specify priors on distributional parameters by:
++
+- creating a variable in the model for this distributional +parameter
+- referring to the distributional parameter name when specifying the +prior on the focal parameter
+- using the distributional parameter name to specify its own prior in +the usual way
++spec = ( + mp_tmb_library("starter_models", "sir", package = "macpan2") + + ## 1. update the spec to include a new variable named 'my_var' to serve as + ## the standard deviation parameter for the Normal prior on 'beta'. + ## A numeric value of 0.1 is specified as the default for 'my_var', the + ## starting value in optimization. + |> mp_tmb_insert(default = list(my_var = 0.1)) +) +## generate data for calibration +data = mp_simulator(spec, 50, "infection") |> mp_trajectory() + +## set-up calibrator +cal = mp_tmb_calibrator( + spec + , data + , traj = "infection" + ## 2. We set a Normal prior on beta, and set the `sd` argument to 'my_var' + ## (would it ever make sense to use mp_fit for `sd` here?) + , par = list(beta = mp_normal(location = 0.35, sd = mp_nofit("my_var")) + ## 3. setting a log-normal prior on 'my_var' + , my_var = mp_log_normal(1,1) + ) + , default = list(beta = 0.25) +) + +## we can see the prior density for both 'beta' and 'my_var' in the calibration +## objective function +cal$simulator$tmb_model$obj_fn$obj_fn_expr +#> ~-sum(dpois(obs_infection, clamp(sim_infection))) - sum(dnorm(beta, +#> 0.35, exp(my_var))) - sum(dnorm(log(my_var), 1, 1)) +#> <environment: 0x556242881b58> + + +mp_optimize(cal) +#> $par +#> params params +#> 0.2000017 1.0015207 +#> +#> $objective +#> [1] 53.08736 +#> +#> $convergence +#> [1] 0 +#> +#> $iterations +#> [1] 9 +#> +#> $evaluations +#> function gradient +#> 10 10 +#> +#> $message +#> [1] "relative convergence (4)" +mp_tmb_coef(cal) +#> term mat row col default type estimate std.error +#> 1 params beta 0 0 0.25 fixed 0.2000017 0.009166585 +#> 2 params.1 my_var 0 0 0.10 fixed 1.0015207 0.707373865