-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinal_slides.qmd
284 lines (218 loc) Β· 9.87 KB
/
Final_slides.qmd
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
---
title: "<span style='color:black;'>Introduction to epiworldRcalibrate package</span>"
author: "<span style='color:#800080;'>Sima Najafzadehkhoei</span>"
format:
revealjs:
scrollable: true
transition: slide
slide-number: true
show-slide-number: print
footer: |
<div style="text-align: right;">
<span style="color:#8B0000;">[email protected]
</div>
# Disable the default footer to add custom content
editor: visual
slide-number: true
show-slide-number: print
---
# [Welcome to **epiworldRcalibrate** ππ]{style="font-size: 60px;"}
[Use epiworldRcalibrate to unlock the potential of simulation-based learning!]{style="font-size: 30px;"}
[**This Package Offers Two Key Capabilities**:]{style="font-size: 30px;"}
[1οΈβ£π Simulate the Calibration Process:]{style="font-size: 30px; font-weight: bold;"}[Visualize how parameters affect outcomes.]{style="font-size: 20px;"}
[2οΈβ£π― Provide Your Data:]{style="font-size: 30px; font-weight: bold;"} [Automatically determine model parameters using cutting-edge machine learning.]{style="font-size: 20px;"}
[π **Perfect for Epidemiologists, Data Scientists, and Researchers!**\
π οΈ Tools built on **R, TensorFlow, and Keras** ensure high performance.]{style="font-size: 20px;"}
``` r
install.packages("epiworldRcalibrate")
library(epiworldRcalibrate)
```
------------------------------------------------------------------------
### What we have done so far? π¦ π
```{r, echo=FALSE, fig.align='center', out.width="180%"}
knitr::include_graphics("~/epiworldRcalibrate/epiworldRcalibrate/misc/plan.jpg")
```
<div style="display: flex; align-items: flex-start; gap: 15px;">
<!-- Left Table -->
::: {style="font-size: 12px; line-height: 1; width: 50%; text-align: left; border: 1px solid #ddd; padding: 10px; border-radius: 3px; background-color: #f9f9f9;"}
### π οΈ **States of the SIR Model**
| **State** | **Description** |
|-----------|----------------------------------------|
| π’ **S** | Susceptible (population at risk) |
| π΄ **I** | Infected (currently sick individuals) |
| π΅ **R** | Recovered (or removed from the system) |
:::
<!-- Right Table -->
::: {style="font-size: 12px; line-height: 1; width: 50%; text-align: left; border: 1px solid #ddd; padding: 8px; border-radius: 3px; background-color: #f9f9f9;"}
### π **Model Parameters**
| **Parameter** | **Description** |
|--------------------------|----------------------------------------------|
| π€ **Contact Rate** | Number of contacts per individual per unit time |
| π **Transmission Rate** | Probability of disease transmission per contact |
| π **Prevalence** | Proportion of the population currently infected |
| π **Recovery Rate** | Rate at which infected individuals recover |
:::
# ππ― epiworldRcalibrate
our Ultimate Tool for Epidemic Insights
```{r, echo=FALSE, fig.align='center'}
knitr::include_graphics("~/epiworldRcalibrate/epiworldRcalibrate/misc/epiworldRcalibrate_scenario.jpg")
```
# π Simulate and calibrate SIR Function
``` {.r .smaller}
N = 2e4
n = 5000
ndays = 50
epochs = 100
ncores = 20
verbose = 2
simulate_calibrate_sir(N, n, ndays, ncores, epochs, verbose)
```
::: {.columns style="font-size:15px;"}
::: {.column width="40%"}
π― [**What Does This Function Do?**]{style="color: #4CAF50;"}
1οΈβ£ [**Data Generation:**]{style="color: #2196F3;"} Create realistic epidemic scenarios
2οΈβ£ [**Simulation:**]{style="color: #FFC107;"} Model the spread of disease over time
3οΈβ£ [**Model Training:**]{style="color: #9C27B0;"} Use CNNs for best-fitting parameters
4οΈβ£ [**Evaluation:**]{style="color: #FF5722;"} Assess performance (e.g., MAE)
5οΈβ£ [**Visualization:**]{style="color: #673AB7;"} Generate intuitive plots
:::
::: {.column width="60%"}
| [Category]{style="color: #4CAF50;"} | [Parameter]{style="color: #2196F3;"} | [Description]{style="color: #FFC107;"} | [Example]{style="color: #9C27B0;"} |
|-------------------|-----------------|-------------------|-----------------|
| [**Simulation Inputs**]{style="color: #4CAF50;"} | N | Total number of simulations to run. | 20,000 |
| | n | Population size for each simulation. | 5,000 |
| | ndays | Duration of the simulation in days. | 50 |
| | ncores | Number of cores for parallel processing. | 20 |
| [**Model Training**]{style="color: #FF5722;"} | epochs | Number of training iterations for the CNN model. | 100 |
| [**Inputs**]{style="color: #FFC107;"} | verbose | Level of detail in the training output. | |
:::
:::
## π Going Deep into the Function
::: columns
::: {.column width="60%"}
```{=html}
<pre><code id="code-block">
simulate_calibrate_sir <- function(N, n, ndays, ncores, epochs, verbose) {
theta <- generate_theta(N, n)
seeds <- sample.int(.Machine$integer.max, N, TRUE)
matrices <- run_simulations(N, n, ndays, ncores, theta, seeds)
filtered_data <- filter_non_null(matrices, theta)
matrices <- filtered_data$matrices
theta <- filtered_data$theta
N <- filtered_data$N
arrays_1d <- prepare_data_for_tensorflow(matrices, N)
theta2 <- data.table::as.data.table(data.table::copy(theta))
theta2$crate <- stats::plogis(theta2$crate / 10)
data_split <- split_data(arrays_1d, theta2, N)
train <- data_split$train
test <- data_split$test
model <- build_cnn_model(dim(arrays_1d)[-1], ncol(theta2))
train_model(model, train, epochs = epochs, verbose = verbose)
eval_results <- evaluate_model(model, test, theta)
pred <- eval_results$pred
MAEs <- eval_results$MAEs
plot_results(pred, test, theta, MAEs, N, floor(N * 0.7))
return(list(pred = pred, MAEs = MAEs))
}
</code></pre>
```
:::
::: {.column width="40%"}
###### π₯ Video Example:
<iframe src="https://drive.google.com/file/d/1O4S4RqJPBQAZF_0MYK5n98yQevXyYZyg/preview" width="100%" height="500" frameborder="0" allowfullscreen>
</iframe>
:::
:::
```{=html}
<script>
const allLines = document.querySelector("#code-block").innerHTML.split("\n");
let currentIndex = 0;
// Function to highlight next line
function highlightNextLine() {
const codeBlock = document.querySelector("#code-block");
const newLines = allLines.map((line, index) => {
if (index === currentIndex && !line.trim().startsWith("#")) {
return `<span style="background-color: #3b82f6; color: white; display: inline-block; width: 100%; border-radius: 5px; padding: 2px;">${line}</span>`;
}
return line;
});
codeBlock.innerHTML = newLines.join("\n");
if (currentIndex < allLines.length - 1) {
currentIndex++;
}
}
// Trigger highlighting on Enter key press
document.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
highlightNextLine();
}
});
</script>
```
## π― Empower Yourself: Perform Your Own Calibration:
Model Calibration Made Easy π―π€
::: columns
::: {.column width="50%" style="font-size: 20px;"}
### π Calibrate Parameters:
1οΈβ£ **Input Your Data**: Provide real-world or simulated SIR data.
2οΈβ£ **CNN Prediction**: The pre-trained model predicts:\
- **Prevalence**: How widespread is the infection?\
- **Case Rate**: How quickly are cases rising?\
- **Transmission Probability**: How infectious is the disease?\
- **Recovery Rate**:Rate at which infected individuals recover
### π§ Smart Model Selection:
- Automatically detects if 30 or 60 models should be used for the input data.
- Loads the correct pre-trained model for optimal predictions.
:::
::: {.column width="50%"}
```{r, echo=FALSE, fig.align='center', out.width="100%"}
knitr::include_graphics("~/epiworldRcalibrate/epiworldRcalibrate/misc/flowchart_calibrate_sir.jpg")
```
:::
:::
``` r
N=1
n=5000
set.seed(123)
theta <- generate_theta(N, n)
ncores=20
ndays=60
seeds=123
# Run simulations
i=1
m <- epiworldR:: ModelSIRCONN(
"mycon",
prevalence = theta$preval[i],
contact_rate = theta$crate[i],
transmission_rate = theta$ptran[i],
recovery_rate = theta$prec[i],
n = n
)
verbose_off(m)
run(m, ndays = ndays)
incidence = epiworldR::plot_incidence(m, plot = FALSE)
data=incidence$Infected
result=calibrate_sir(data)
```
## Results:
### [SIR Model Actual vs Predicted Table]{style="font-size: 20px;"}
| | Prevalence | Contact Rate (crate) | Transmission Rate (ptran) | Recovery Rate (prec) |
|--------------|--------------|--------------|---------------|--------------|
| **Actual** | 0.11900 | 0.64784 | 0.07971 | 0.54576 |
| **Predicted** | 0.22479 | 0.60218 | 0.22618 | 0.54581 |
| **MAE** | 0.10579 | 0.04566 | 0.14647 | 0.00005 |
## [π Visualizing SIR Model Results: Actual vs Predicted Parameters]{style="font-size: 40px;"}
```{r, echo=FALSE, fig.align='center', out.width="180%"}
knitr::include_graphics("~/epiworldRcalibrate/epiworldRcalibrate/misc/plots.png")
```
## π Thank You!
::: {style="text-align: center; font-size: 36px; color: #4CAF50; margin-top: 50px;"}
<p><strong>Thank You for Your Attention!</strong></p>
<p style="font-size: 20px; color: #555;">
Questions? Feel free to ask! π
</p>
:::
::: {style="text-align: center; font-size: 16px; color: #888; margin-top: 30px;"}
<p>Advanced R Programming</p>
<p>Population Health Sciences, University of Utah</p>
:::