Skip to content

Commit f689df2

Browse files
authored
chapter 8 (#9)
1 parent 19f0047 commit f689df2

File tree

2 files changed

+188
-4
lines changed

2 files changed

+188
-4
lines changed

08_essential-r-packages-for-machine-learning.Rmd

Lines changed: 186 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,194 @@
22

33
**Learning objectives:**
44

5-
- THESE ARE NICE TO HAVE BUT NOT ABSOLUTELY NECESSARY
5+
- Key R-packages for ML
6+
- Case Studies:
7+
- how to use `mlr3`
8+
- how to use `keras3`
69

7-
## SLIDE 1 {-}
810

9-
- ADD SLIDES AS SECTIONS (`##`).
10-
- TRY TO KEEP THEM RELATIVELY SLIDE-LIKE; THESE ARE NOTES, NOT THE BOOK ITSELF.
11+
## Key R-packages for ML
12+
13+
- Meta-Packages:
14+
- `tidymodels`
15+
- `mlr3`
16+
- Engines:
17+
- `xgboost`
18+
- `ranger`
19+
- `keras`
20+
- Specialized Packages for:
21+
- Time Series Analysis:
22+
- `forecast`
23+
- `prophet`
24+
- Bayesian Analysis:
25+
- `brms`
26+
- `rstanarm`
27+
- Spatial Analysis:
28+
- `INLA`
29+
30+
31+
## How to use mlr3
32+
33+
### DALYs due to Dengue
34+
35+
```{r}
36+
#| message: false
37+
#| warning: false
38+
library(hmsidwR)
39+
library(dplyr)
40+
library(tidyr)
41+
```
42+
43+
44+
```{r}
45+
dalys_dengue <- hmsidwR::infectious_diseases %>%
46+
arrange(year) %>%
47+
filter(cause_name == "Dengue",
48+
year<=2016,
49+
!location_name %in% c("Eswatini", "Lesotho")) %>%
50+
drop_na() %>%
51+
group_by(location_id) %>%
52+
select(-location_name, -cause_name)
53+
54+
dalys_dengue %>%
55+
head()
56+
```
57+
58+
Load mlr3 packages:
59+
```{r}
60+
#| eval: false
61+
library(mlr3)
62+
library(mlr3learners)
63+
library(mlr3viz)
64+
library(mlr3verse)
65+
library(data.table)
66+
# library(xgboost)
67+
```
68+
69+
Set a Task:
70+
```{r}
71+
#| eval: false
72+
task <- TaskRegr$new(id = "DALYs",
73+
backend = dalys_dengue,
74+
target = "DALYs"
75+
)
76+
```
77+
78+
Specify two models:
79+
```{r}
80+
#| eval: false
81+
learner_cv_glmnet <- lrn("regr.cv_glmnet",
82+
alpha = 0.5,
83+
s = 0.1)
84+
learner_xgboost <- lrn("regr.xgboost",
85+
nrounds = 1000,
86+
max_depth = 6,
87+
eta = 0.01)
88+
```
89+
90+
91+
## How to use keras3
92+
93+
keras3 is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano. It allows for easy and fast prototyping, supports both convolutional networks and recurrent networks, and runs seamlessly on CPU and GPU.
94+
95+
### General Infection
96+
97+
In this example, we will use the `keras3` package to build a simple `neural network model` to predict the number of cases of a general infection based on various features.
98+
99+
We have seen how to simulate infection with the `SEIR` model. We will use the same model function and update the parameters obtained by training a neural network model.
100+
101+
```{r}
102+
#| eval: false
103+
install.packages("keras3")
104+
keras3::install_keras(backend = "tensorflow")
105+
```
106+
107+
The function we will be using are:
108+
109+
keras_model_sequential()
110+
layer_dense()
111+
layer_activation()
112+
compile()
113+
fit()
114+
115+
The scenario is as follows:
116+
117+
- We have a dataset with the number of cases of a general infection and various features
118+
119+
- We build a neural network model to update the SEIR parameters based on certain social interaction features with `keras3`:
120+
121+
adjusted_parameters["beta"] * (1 + mean(predicted_infections))
122+
123+
- Apply the new model to predict the number of infections based on social interaction features
124+
125+
### Neural Network Model
126+
127+
Given an input vector $x=[x_1,x_2,...,x_p]$
128+
129+
1. **First Dense Layer Transformation**:
130+
131+
$$
132+
z^{(1)}=\sum_{i=1}^p{W_i^{(1)} x_i+b^{(1)}}
133+
$$
134+
135+
2. **Activation Function**:
136+
$$
137+
a^{(1)}=ReLu(z^{(1)})=max(0,z^{(1)})
138+
$$
139+
3. **Second Dense Layer Transformation**:
140+
$$
141+
z^{(2)}=\sum_{i=1}^p{W_i^{(2)} x_i+b^{(2)}}
142+
$$
143+
144+
4. **Output Activation (Sigmoid)**:
145+
$$
146+
a^{(2)}=Sigmoid(z^{(2)})= \frac{1} {1+e^{-z^{(2)}}}
147+
$$
148+
149+
### Example Code
150+
```{r}
151+
#| eval: false
152+
model <- keras_model_sequential(input_shape = c(p))
153+
# simple model
154+
model %>%
155+
layer_dense(units = 1) %>%
156+
layer_activation("relu") %>%
157+
layer_dense(units = 1, activation = "sigmoid")
158+
```
159+
160+
Compile the model with a binary crossentropy loss function and an Adam optimizer to match the difference between original data and the model output, and apply model adjustments.
161+
```{r}
162+
#| eval: false
163+
model %>% compile(loss = "binary_crossentropy",
164+
optimizer = optimizer_adam(),
165+
metrics = c("accuracy"))
166+
```
167+
168+
To fit the model to the data, we will use the `fit()` function, this is usually called `history`:
169+
```{r}
170+
#| eval: false
171+
history <- model %>% fit(x = as.matrix(social_data[, 1:p]),
172+
y = social_data$infection,
173+
epochs = 30,
174+
batch_size = 128,
175+
validation_split = 0.2
176+
)
177+
```
178+
179+
History object contains the training and validation loss and accuracy for each epoch.
180+
181+
Finally, we adjust the output parameters of the SEIR model using the predicted values from the neural network model:
182+
```{r}
183+
#| eval: false
184+
adjusted_output <- ode(y = initial_state,
185+
times = times,
186+
func = SEIR,
187+
parms = adjusted_parameters
188+
)
189+
# Convert output to a data frame
190+
adjusted_output <- as.data.frame(adjusted_output)
191+
```
192+
11193

12194
## Meeting Videos {-}
13195

DESCRIPTION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Imports:
1313
deSolve,
1414
DiagrammeR,
1515
dials,
16+
dplyr,
1617
ggtext,
1718
HistData,
1819
hmsidwR,
@@ -23,6 +24,7 @@ Imports:
2324
reprtree (>= 0.6),
2425
rmarkdown,
2526
tidymodels,
27+
tidyr,
2628
tidyverse
2729
Remotes:
2830
reprtree=araastat/reprtree

0 commit comments

Comments
 (0)