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
* Fixing vignette of LFMCMC
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Removing extra citation message
* Bump version to 0.15.1-1 and add NEWS.md entry for LFMCMC vignette fix
Co-authored-by: gvegayon <893619+gvegayon@users.noreply.github.com>
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: gvegayon <893619+gvegayon@users.noreply.github.com>
Copy file name to clipboardExpand all lines: vignettes/likelihood-free-mcmc.qmd
+25-13Lines changed: 25 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -32,7 +32,7 @@ Our SIR model will have the following characteristics:
32
32
***Virus Name:** COVID-19
33
33
***Initial Virus Prevalence:** 0.01
34
34
***Recovery Rate:** 1/7 (0.14)
35
-
***Transmission Rate:** 0.02
35
+
***Transmission Rate:** 0.04
36
36
***Number of Agents:** 2,000
37
37
38
38
We use the `ModelSIR` and `agents_smallworld` functions to construct the model in epiworldR.
@@ -41,12 +41,12 @@ We use the `ModelSIR` and `agents_smallworld` functions to construct the model i
41
41
#| label: setup-sir
42
42
library(epiworldR)
43
43
44
-
model_seed <- 122
44
+
model_seed <- 221
45
45
46
46
model_sir <- ModelSIR(
47
47
name = "COVID-19",
48
48
prevalence = .01,
49
-
transmission_rate = .02,
49
+
transmission_rate = .04,
50
50
recovery_rate = 1 / 7
51
51
)
52
52
@@ -72,13 +72,17 @@ run(
72
72
)
73
73
74
74
summary(model_sir)
75
+
plot_incidence(model_sir)
75
76
```
76
77
77
-
Note the "Model parameters" and the "Distribution of the population at time 50" from the above output. Our goal is to recover the model parameters (Recovery and Transmission rates) through LFMCMC. We accomplish this by comparing the population distribution from each simulation run to the "observed" distribution from our model. We get this distribution using the `get_today_total` function in epiworldR.
78
+
Note the "Model parameters" and the "Distribution of the population at time 50" from the above output. Our goal is to recover the model parameters (Recovery and Transmission rates) through LFMCMC. We accomplish this by comparing each simulation run to the "observed" data from our model. In addition to the end-of-run population counts from `get_today_total`, we include the mean number of active cases over time using `get_active_cases`. This extra summary statistic provides additional information about the epidemic trajectory and improves the fit.
78
79
79
80
```{r}
80
81
#| label: get-sir-model-data
81
-
model_sir_data <- get_today_total(model_sir)
82
+
model_sir_data <- c(
83
+
get_today_total(model_sir),
84
+
mean(get_active_cases(model_sir)$active_cases)
85
+
)
82
86
```
83
87
84
88
For practical cases, you would use observed data, instead of a model simulation. We use a simulation in our example to show the accuracy of LFMCMC in recovering the model parameters. Whenever we use the term "observed data" below, we are referring to the model distribution (`model_sir_data`).
@@ -87,7 +91,7 @@ For practical cases, you would use observed data, instead of a model simulation.
87
91
88
92
In epiworldR, LFMCMC requires four functions:
89
93
90
-
The **simulation function** runs a model with a given set of parameters and produces output that matches the structure of our observed data. For our example, we set the Recovery and Transmission rate parameters, run an SIR model for 50 days, and return the distribution of the population at the end of the run.
94
+
The **simulation function** runs a model with a given set of parameters and produces output that matches the structure of our observed data. For our example, we set the Recovery and Transmission rate parameters, run an SIR model for 50 days, and return both the end-of-run population distribution and the mean number of active cases.
The **summary function** extracts summary statistics from the given data.
110
-
This should produce the same output format for both the observed data and the simulated data from `simulation_fun`. For our example, since the population distribution is already a summary of the data, our summary function simply passes that data through. With more complicated use cases, you might instead compute summary statistics such as the mean or standard deviation.
118
+
This should produce the same output format for both the observed data and the simulated data from `simulation_fun`. For our example, the output already contains summary quantities (state totals plus mean active cases), so our summary function simply passes that data through. With more complicated use cases, you might instead compute summary statistics such as the mean or standard deviation.
111
119
112
120
```{r}
113
121
#| label: sumfun
@@ -121,7 +129,7 @@ The **proposal function** returns a new set of parameters, which it is "proposin
To run LFMCMC, we need to set the initial model parameters. For our example, we use an initial Recovery rate of 0.3 and an initial Transmission rate of 0.3. We set the kernel epsilon to 1.0 and run the simulation for 2,000 samples (iterations) using the `run_lfmcmc` function.
165
+
To run LFMCMC, we need to set the initial model parameters. For our example, we use an initial Recovery rate of 0.3 and an initial Transmission rate of 0.3. We set the kernel epsilon to 0.25 and run the simulation for 2,000 samples (iterations) using the `run_lfmcmc` function.
158
166
159
167
```{r}
160
168
#| label: lfmcmc-run
161
169
initial_params <- c(0.3, 0.3)
162
-
epsilon <- 1.0
170
+
epsilon <- 0.25
163
171
n_samples <- 2000
164
172
165
173
# Run the LFMCMC simulation
174
+
set.seed(333)
166
175
run_lfmcmc(
167
176
lfmcmc = lfmcmc_model,
168
177
params_init = initial_params,
@@ -178,7 +187,10 @@ To make the printed results easier to read, we use the `set_params_names` and `s
Recall that the observed data came from a model with a Recovery rate of 0.3 and a Transmission rate of 0.3. As the above output shows, LFMCMC made a close approximation of the parameters, which resulted in a close approximation of the observed population distribution. This example highlights the effectiveness of using LFMCMC for highly complex models.
226
+
Recall that the observed data came from a model with a Recovery rate of 1/7 and a Transmission rate of 0.04. As the above output shows, LFMCMC makes a close approximation of the parameters, which results in a close match to both the observed population distribution and the average number of active cases. This example highlights how adding informative summary statistics can improve likelihood-free inference in complex models.
0 commit comments