-
Notifications
You must be signed in to change notification settings - Fork 1
/
bayesian_regression_and_temporal_modelling.qmd
854 lines (669 loc) · 19.7 KB
/
bayesian_regression_and_temporal_modelling.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
---
title: "Bayesian Regression and Temporal Modelling"
subtitle: "Key ideas and concepts"
author:
- name: "Robbie M. Parks"
email: "robbie.parks@columbia.edu"
institute: "Environmental Health Sciences, Columbia University"
date: 2023-08-14
date-format: medium
title-slide-attributes:
data-background-color: "#f3f4f4"
data-background-image: "../../assets/bmeh_normal.png"
data-background-size: 80%
data-background-position: 60% 120%
format:
revealjs:
slide-number: true
incremental: true
chalkboard:
buttons: false
preview-links: auto
logo: "../../assets/bmeh_normal.png"
theme: [default, ../../assets/style.scss]
---
```{r}
library(here)
library(tidyverse)
library(nimble)
library(bayesplot)
library(posterior)
library(hrbrthemes)
```
# Outline
- Introduction
- Regression models
- Using `NIMBLE` for Bayesian inference
# Overview
## Finding associations from data
::: nonincremental
- Generate some points:
:::
``` R
# Load packages
library(ggplot2)
# Create a dataset
set.seed(100)
data = data.frame(x=rnorm(100),y=rnorm(100))
# Plot and rough fit
p <- ggplot(data, aes(x, y)) +
geom_point()
plot(p)
```
## Finding associations from data
::: nonincremental
- Plot the generated points:
:::
```{r}
#| echo: false
library(ggplot2)
set.seed(100)
data <- data.frame(x = rnorm(100), y = rnorm(100))
p <- ggplot(data, aes(x, y)) +
geom_point()
plot(p)
```
## Finding associations from data
::: nonincremental
- Establish some kind of association:
:::
```{r}
#| echo: false
library(ggplot2)
set.seed(100)
data <- data.frame(x = rnorm(100), y = rnorm(100))
p <- ggplot(data, aes(x, y)) +
geom_point() +
geom_smooth(method = "loess")
plot(p)
```
## Finding associations from data
::: nonincremental
- Better still, use some real data.
:::
``` R
data <- read_csv(here("data", "Spain", "data_spain.csv"))
head(data)
data_national <- data |>
group_by(week, week_of_year) |>
summarise(
deaths = sum(deaths),
population = sum(population),
t2m = mean(t2m),
weekly_t2m_anomaly = mean(weekly_t2m_anomaly)
) |>
mutate(week = dmy(week)) |>
arrange(week) |>
filter(year(week) < 2020) # avoiding COVID for now
ggplot(data = data_national) +
geom_point(aes(x = week, y = deaths))
```
## Finding associations from data
::: nonincremental
- Better still, use some real data.
:::
```{r}
data <- read_csv(here("data", "Spain", "data_spain.csv"))
head(data)
data_national <- data |>
group_by(week, week_of_year) |>
summarise(
deaths = sum(deaths),
population = sum(population),
t2m = mean(t2m),
weekly_t2m_anomaly = mean(weekly_t2m_anomaly)
) |>
mutate(week = dmy(week)) |>
arrange(week) |>
filter(year(week) < 2020) # avoiding COVID for now
```
## Finding associations from data
::: nonincremental
- Better still, use some real data.
:::
```{r}
#| echo: false
ggplot(data = data_national) +
geom_point(aes(x = week, y = deaths))
```
## Finding associations from data
::: nonincremental
- Find some kind of association.
:::
```{r}
#| echo: false
data_national <- data_national |>
mutate(rate = 100000 * deaths / population)
code_linear <- nimbleCode({
# priors
alpha ~ dnorm(0, sd = 10) # prior for alpha
beta_week ~ dnorm(0, sd = 10) # prior for beta_week
# likelihood
for (t in 1:Nw) {
deaths[t] ~ dpois(mu[t])
log(mu[t]) <- log(population[t]) + alpha + beta_week * t
}
# what's the estimated annual rate of change?
beta_year <- exp(52 * beta_week)
})
constants <- list(Nw = nrow(data_national))
data <- list(deaths = data_national$deaths, population = data_national$population)
inits <- list(alpha = 0, beta_week = 0)
parameters_to_monitor <- c("alpha", "beta_week", "beta_year")
nimbleMCMC_samples_linear <- nimbleMCMC(
code = code_linear,
data = data,
constants = constants,
inits = inits,
monitors = parameters_to_monitor,
niter = 10000,
nburnin = 5000,
setSeed = 1,
progressBar = FALSE,
samplesAsCodaMCMC = TRUE
)
linear_fit <- data_national |>
ungroup() |>
mutate(
.death_rate_fit = 100000 * exp(
# add alpha and beta_week * week_number by sample
sweep(
nimbleMCMC_samples_linear[, "beta_week"] %*% t(1:nrow(data_national)),
1,
nimbleMCMC_samples_linear[, "alpha"],
FUN = "+"
)
) |>
# then take the mean of the samples
apply(
FUN = mean,
MARGIN = 2
)
) |>
mutate(residuals = rate - .death_rate_fit)
linear_fit |>
ggplot() +
geom_point(aes(x = week, y = rate), size = 0.6) +
geom_line(aes(x = week, y = .death_rate_fit), size = 0.8, colour = "red")
```
## But do the residuals look right?
```{r}
linear_fit |>
ggplot(aes(x = residuals)) +
geom_histogram()
```
## Regression models
- Most commonly used to assess presence of a relationship between a dependent variable and one or more independent variables.
- Bayesian regression originally used in econometrics.
- Now wide range of fields use regression, including environmental epidemiology.
- Partly why we find ourselves here today!
## Regression models
- Basic steps for regression models:
1. Establish suitable model for observations.
2. Identify type of relationship between predictor(s) and outcome.
3. For Bayesian approach, specify prior distributions etc.
4. Run the model somehow (e.g., `R` with `NIMBLE`).
5. See how well the model has fit.
# Suitable models for observations
## Generalized linear models
- Extension of linear regression to when distribution not necessarily normally distributed.
- Distributions which are part of the exponential family can describe all sort of non-normally distributed variables.
- Many examples commonly encountered in environmental health.
- We'll go through a few now...
## Exponential family of models
- Sometimes dependent variable is not normally distributed.
- Exponential family is family of models described in a certain way (won't get into it in this workshop).
- All of the models we'll look at are exponential family models.
## Some types of models to know about
- Normal (we've seen last lecture)
- Logistic regression (yes/no)
- Binomial (set number of trials)
- Poisson (counts)
## Normal distribution
::: nonincremental
- As seen in first lecture.
:::
$$
\begin{split}
y_i &\sim \text{Normal}(\mu_i, \sigma) \quad i = 1,..., N \\
\mu_i &= \alpha + \beta_1 x_1 + \beta_2 x_2 + \beta_3 x_3
\end{split}
$$
## Logistic regression
::: nonincremental
- Used when we want to classify observations into two groups (zero or one).
- If dependent variable represents sets of trails of yes/no:
:::
$$
\begin{split}
y_i &\sim \text{Bernoulli}(p_i) \quad i = 1,..., N \\
\text{logit}(p_i) &= \alpha + \beta_1 x_1 + \beta_2 x_2 + \beta_3 x_3
\end{split}
$$
## Binomial regression
::: nonincremental
- Used when we want to know how many successes from a set number of trials
- Distributed with Binomial distribution (zero to $n$ successes).
:::
$$
\begin{split}
y_i &\sim \text{Bin}(n,p_i) \quad i = 1,..., N \\
\text{logit}(p_i) &= \alpha + \beta_1 x_1 + \beta_2 x_2 + \beta_3 x_3
\end{split}
$$
## Poisson regression
::: nonincremental
- Used typically with count data (number of events happening within a discrete space and time).
:::
$$
\begin{split}
y_t &\sim \text{Pois}(\mu_t) \quad i = 1,..., T \\
\log(\mu_t) &= \log(P_t) + \alpha + \beta_w t
\end{split}
$$
## Link function
- When modelling, we must make a decision about how the predictors are related to the key parameters in our chosen relationship
- You will see several of the remaining time in the workshop.
- For example, link function on Poisson regression is usually a log-link function to prevent counts from going negative.
$$
\begin{split}
\log(\mu_t) &= \log(P_t) + \alpha + \beta_w t
\end{split}
$$
## Reminder of regression model steps
::: nonincremental
- Basic steps for regression models:
1. Establish suitable model for observations.
2. Identify type of relationship between predictor(s) and outcome.
3. For Bayesian approach, specify prior distributions etc.
4. Run the model somehow (e.g., `R` with `NIMBLE`).
5. See how well the model has fit.
:::
# Relationship between predictor(s) and outcome
## Linear regression
- Simplest regression model.
- Assuming a linear relationship between predictors and the outcome.
- Used in countless different applications.
## Linear regression
$$
\begin{split}
y_i &\sim \text{Normal}(\mu_i, \sigma) \quad i = 1,..., N \\
\mu_i &= \alpha + \beta_1 x_1 + \beta_2 x_2 + \beta_3 x_3
\end{split}
$$
## Linear regression
::: nonincremental
- As a reminder of last lab, in `NIMBLE`, we can write (including priors):
:::
``` R
code <- nimbleCode({
# priors for parameters
alpha ~ dnorm(0, sd = 100) # prior for alpha
beta1 ~ dnorm(0, sd = 100) # prior for beta1
beta2 ~ dnorm(0, sd = 100) # prior for beta2
beta3 ~ dnorm(0, sd = 100) # prior for beta3
sigma ~ dunif(0, 100) # prior for variance components
# regression formula
for (i in 1:n) { # n is the number of observations we have in the data
mu[i] <- alpha + beta1 * x1[i] + beta2 * x2[i] + beta3 * x3[i] # manual entry of linear predictors
y[i] ~ dnorm(mu[i], sd = sigma)
}
})
```
## Non-linear regression
- Seasonality can exist in data, for example, that requires accounting for.
- Also, there can be autocorrelation between neighbouring time points.
## Non-linear regression
```{r}
#| echo: false
ggplot(data = data_national) +
geom_point(aes(x = week, y = deaths))
```
## Non-linear regression
- Random walk.
- Can be over time or over other units.
- Extension is autoregressive structure for longer-term memory.
## Non-linear regression
$$
\begin{split}
y_t &\sim \text{Pois}(\mu_t) \quad i = t,..., T \\
\log(\mu_t) &= \log(P_t) + \alpha + \gamma_t \\
\gamma_t &\sim N(\gamma_{t-1}, \sigma_{rw})
\end{split}
$$
## Non-linear regression
``` R
code_weekly_random_walk <- nimbleCode({
# priors
alpha ~ dnorm(0, sd = 10) # prior for alpha
sigma_rw ~ T(dnorm(0, 1), 0, Inf) # half-normal prior for variance of weekly effects
# likelihood
for (t in 1:Nw) {
deaths[t] ~ dpois(mu[t])
log(mu[t]) <- log(population[t]) + lograte[t]
lograte[t] <- alpha + rw[t]
}
# random walk over time
rw[1] <- 0
for (t in 2:Nw) {
rw[t] ~ dnorm(rw[t - 1], sigma_rw)
}
})
```
## Non-linear regression
```{r}
#| echo: false
code_weekly_random_walk <- nimbleCode({
# priors
alpha ~ dnorm(0, sd = 10) # prior for alpha
sigma_rw ~ T(dnorm(0, 1), 0, Inf) # half-normal prior for variance of weekly effects
# likelihood
for (t in 1:Nw) {
deaths[t] ~ dpois(mu[t])
log(mu[t]) <- log(population[t]) + lograte[t]
lograte[t] <- alpha + rw[t]
}
# random walk over time
rw[1] <- 0
for (t in 2:Nw) {
rw[t] ~ dnorm(rw[t - 1], sigma_rw)
}
})
constants <- list(Nw = nrow(data_national))
data <- list(deaths = data_national$deaths, population = data_national$population)
inits <- list(alpha = -8.0, rw = rep(0, times = nrow(data_national)), sigma_rw = 1)
parameters_to_monitor <- c("alpha", "rw", "lograte")
nimbleMCMC_samples_week_random_walk <- nimbleMCMC(
code = code_weekly_random_walk,
data = data,
constants = constants,
inits = inits,
monitors = parameters_to_monitor,
niter = 10000, # 80000,
nburnin = 5000, # 40000,
setSeed = 1,
progressBar = FALSE,
samplesAsCodaMCMC = TRUE
)
pred_death_rate <- 100000 * exp(
nimbleMCMC_samples_week_random_walk[, str_c("lograte[", seq(nrow(data_national)), "]")]
) |>
apply(
FUN = quantile,
MARGIN = 2,
p = c(0.025, 0.5, 0.975)
)
rw_fit <- data_national |>
ungroup() |>
mutate(
.death_rate_median = pred_death_rate[2, ],
.death_rate_lower = pred_death_rate[1, ],
.death_rate_upper = pred_death_rate[3, ],
) |>
mutate(residuals = rate - .death_rate_median)
rw_fit |>
ggplot(aes(x = week)) +
geom_point(aes(y = rate), size = 2) +
geom_ribbon(aes(ymin = .death_rate_lower, ymax = .death_rate_upper), fill = "red", alpha = 0.1) +
geom_line(aes(y = .death_rate_median), size = 0.4, colour = "red")
```
## Reminder of regression model steps
::: nonincremental
- Basic steps for regression models:
1. Establish suitable model for observations.
2. Identify type of relationship between predictor(s) and outcome.
3. For Bayesian approach, specify prior distributions etc.
4. Run the model somehow (e.g., `R` with `NIMBLE`).
5. See how well the model has fit.
:::
# Prior distributions
## Setting priors
- Need to do this on:
- The regression parameters (e.g., the $\beta$ parameters).
- Variance of outcome (e.g., $\sigma^2$).
- In absence of information, set priors as vague:
``` R
alpha ~ dnorm(0, sd = 10) # prior for alpha
beta_temperature ~ dnorm(0, sd = 10) # prior for beta_temperature
sigma_rw ~ T(dnorm(0, 1), 0, Inf) # half-normal prior for variance of weekly effects
```
## Priors focus in `NIMBLE` model
```{r}
#| echo: false
code_weekly_random_walk <- nimbleCode({
# priors
alpha ~ dnorm(0, sd = 10) # prior for alpha
sigma_rw ~ T(dnorm(0, 1), 0, Inf) # half-normal prior for variance of weekly effects
# likelihood
for (t in 1:Nw) {
deaths[t] ~ dpois(mu[t])
log(mu[t]) <- log(population[t]) + lograte[t]
lograte[t] <- alpha + rw[t]
}
# random walk over time
rw[1] <- 0
for (t in 2:Nw) {
rw[t] ~ dnorm(rw[t - 1], sigma_rw)
}
})
```
## Priors focus in `NIMBLE` model
::: nonincremental
- How does $\alpha$ prior look?
:::
$$Normal(0,10)$$
```{r}
#| echo: false
p <- seq(-10, 10, length = 10000)
# create plot of Beta distribution with shape parameters 2 and 10
plot(p, dnorm(p, 0, 10), type = "l")
```
## Priors focus in `NIMBLE` model
::: nonincremental
- Alternative $\alpha$ priors:
:::
$$Normal(0,1)$$
```{r}
#| echo: false
p <- seq(-10, 10, length = 10000)
# create plot of Beta distribution with shape parameters 2 and 10
plot(p, dnorm(p, 0, 1), type = "l")
```
## Priors focus in `NIMBLE` model
::: nonincremental
- Alternative $\alpha$ priors:
:::
$$Normal(1,1)$$
```{r}
#| echo: false
p <- seq(-10, 10, length = 10000)
# create plot of Beta distribution with shape parameters 2 and 10
plot(p, dnorm(p, 1, 1), type = "l")
```
## Priors focus in `NIMBLE` model
::: nonincremental
- Alternative $\alpha$ priors:
:::
$$Normal(1,5)$$
```{r}
#| echo: false
p <- seq(-10, 10, length = 10000)
# create plot of Beta distribution with shape parameters 2 and 10
plot(p, dnorm(p, 1, 5), type = "l")
```
## Reminder of regression model steps
::: nonincremental
- Basic steps for regression models:
1. Establish suitable model for observations.
2. Identify type of relationship between predictor(s) and outcome.
3. For Bayesian approach, specify prior distributions etc.
4. Run the model somehow (e.g., `R` with `NIMBLE`).
5. See how well the model has fit.
:::
# Running models
- Use `NIMBLE` with `R`!
## Linear regression: Real world example
::: nonincremental
- Let's load in the data we'll use for lab (Spain mortality).
:::
``` R
data <- read_csv(here("data", "Spain", "data_spain.csv"))
```
```{r}
data <- read_csv(here("data", "Spain", "data_spain.csv"))
```
## Linear regression: Real world example
::: nonincremental
- What does it look like?
:::
``` R
data <- read_csv(here("data", "Spain", "data_spain.csv"))
head(data)
data_national <- data |>
group_by(week, week_of_year) |>
summarise(
deaths = sum(deaths),
population = sum(population),
t2m = mean(t2m),
weekly_t2m_anomaly = mean(weekly_t2m_anomaly)
) |>
mutate(week = dmy(week)) |>
arrange(week) |>
filter(year(week) < 2020) # avoiding COVID for now
ggplot(data = data_national) +
geom_point(aes(x = week, y = deaths))
```
## Linear regression: Real world example
::: nonincremental
- Find some kind of association.
:::
``` R
code_linear <- nimbleCode({
# priors
alpha ~ dnorm(0, sd = 10) # prior for alpha
beta_week ~ dnorm(0, sd = 10) # prior for beta_week
# likelihood
for (t in 1:Nw) {
deaths[t] ~ dpois(mu[t])
log(mu[t]) <- log(population[t]) + alpha + beta_week * t
}
# what's the estimated annual rate of change?
beta_year <- exp(52 * beta_week)
})
```
## Linear regression: Real world example
::: nonincremental
- Find some kind of association.
:::
```{r}
#| echo: false
data_national <- data_national |>
mutate(rate = 100000 * deaths / population)
code_linear <- nimbleCode({
# priors
alpha ~ dnorm(0, sd = 10) # prior for alpha
beta_week ~ dnorm(0, sd = 10) # prior for beta_week
# likelihood
for (t in 1:Nw) {
deaths[t] ~ dpois(mu[t])
log(mu[t]) <- log(population[t]) + alpha + beta_week * t
}
# what's the estimated annual rate of change?
beta_year <- exp(52 * beta_week)
})
constants <- list(Nw = nrow(data_national))
data <- list(deaths = data_national$deaths, population = data_national$population)
inits <- list(alpha = 0, beta_week = 0)
parameters_to_monitor <- c("alpha", "beta_week", "beta_year")
nimbleMCMC_samples_linear <- nimbleMCMC(
code = code_linear,
data = data,
constants = constants,
inits = inits,
monitors = parameters_to_monitor,
niter = 10000,
nburnin = 5000,
setSeed = 1,
progressBar = FALSE,
samplesAsCodaMCMC = TRUE
)
pred_death_rate <- 100000 * exp(
# add alpha and beta_week * week_number by sample
sweep(
nimbleMCMC_samples_linear[, "beta_week"] %*% t(1:nrow(data_national)),
1,
nimbleMCMC_samples_linear[, "alpha"],
FUN = "+"
)
) |>
# take the median and 2.5, 97.5 quantiles
apply(
FUN = quantile,
MARGIN = 2,
p = c(0.025, 0.5, 0.975)
)
linear_fit <- data_national |>
ungroup() |>
mutate(
.death_rate_median = pred_death_rate[2, ],
.death_rate_lower = pred_death_rate[1, ],
.death_rate_upper = pred_death_rate[3, ],
) |>
mutate(residuals = rate - .death_rate_median)
linear_fit |>
ggplot(aes(x = week)) +
geom_point(aes(y = rate), size = 0.6) +
geom_ribbon(aes(ymin = .death_rate_lower, ymax = .death_rate_upper), fill = "red", alpha = 0.1) +
geom_line(aes(y = .death_rate_median), size = 0.4, colour = "red")
```
## Reminder of regression model steps
::: nonincremental
- Basic steps for regression models:
1. Establish suitable model for observations.
2. Identify type of relationship between predictor(s) and outcome.
3. For Bayesian approach, specify prior distributions etc.
4. Run the model somehow (e.g., `R` with `NIMBLE`).
5. See how well the model has fit.
:::
# Evaluating model fit
## Evaluating model fit
- Sometimes a seemingly reasonable approach can result in unexpected results.
- When this happens (and in general), good idea to have some way to evaluate model fit of candidate models.
- Ideally residuals after model fit are essentially normally distributed centered around zero.
## Linear model residuals
```{r}
linear_fit |>
ggplot(aes(x = residuals)) +
geom_histogram()
```
## Random walk residuals
```{r}
rw_fit |>
ggplot(aes(x = residuals)) +
geom_histogram()
```
## Other ways of evaluating model fit
- Sometimes not just how well model fits data.
- Also model parameterisation important.
- Predictability is very important and overfitted models are not going to predict well.
- Others will go into Bayesian versions of this.
## Reminder of regression model steps
::: nonincremental
- Basic steps for regression models:
1. Establish suitable model for observations.
2. Identify type of relationship between predictor(s) and outcome.
3. For Bayesian approach, specify prior distributions etc.
4. Run the model somehow (e.g., `R` with `NIMBLE`).
5. See how well the model has fit.
:::
# Getting ready for the lab
## The lab for this session {.smaller}
- This goal of this lab is to explore some key temporal modelling concepts, including linear slopes, random walks and inclusion of linear exposure terms.
- During this lab session, we will:
1. Explore some real time series mortality data;
2. Apply a basic linear model;
3. Apply a non-linear model;
4. Incorporate basic temperature term into model;
5. Modify temperature term to be month-specific; and
6. Explore how well model convergence and fit performs.
# Questions?