-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest1.Rmd
175 lines (127 loc) · 4.24 KB
/
test1.Rmd
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
---
title: "Quantitative Methods in Infectious Disease"
subtitle: "Time series examples"
author: "Dan Weinberger"
date: 'November 28, 2021'
runtime: shiny
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
require(shiny)
source('./R/WaveletPkg.R')
source('./R/plot.wave2.R')
source('./R/biennial.func.R')
library(dplyr)
library(dplyr)
library(ggplot2)
library(MASS)
```
## Data
For these examples, we will use data from the WHO surveillance database for RSV (from the Flu datamark)
Downloads the latest data from WHO
```{r}
#code Courtesy of DeusThindwa; data from WHO
#library(curl)
#library(dplyr)
#library(ggplot2)
#rsv <- read.csv(curl("https://frontdoor-l4uikgap6gz3m.azurefd.net/FLUMART/VIW_FNT?$format=csv"))
#saveRDS(rsv,'./Data/rsv_who.rds')
```
Import and clean the data. Fill in 0s to create a time series. data are not collected year round; assume 0 cases during weeks when there is no data
```{r}
rsv <- readRDS('./Data/rsv_who.rds')
#Clean the data, fill in 0s if not observed
rsvds <-
rsv %>%
dplyr::filter(!is.na(RSV)) %>%
dplyr::select(WHOREGION, FLUSEASON, ORIGIN_SOURCE,HEMISPHERE, COUNTRY_AREA_TERRITORY, MMWR_WEEKSTARTDATE, MMWR_YEAR, MMWR_WEEK, RSV) %>%
arrange(WHOREGION, COUNTRY_AREA_TERRITORY, MMWR_WEEKSTARTDATE,ORIGIN_SOURCE) %>%
filter(COUNTRY_AREA_TERRITORY %in% c('France', 'Sweden')) %>%
mutate(date=as.Date(MMWR_WEEKSTARTDATE)) %>%
filter(date>='2015-01-01' & date <= '2023-03-01') %>%
rename(country=COUNTRY_AREA_TERRITORY) %>%
tidyr::complete(country,ORIGIN_SOURCE,date = seq.Date(from=as.Date('2015-01-04') ,to=as.Date('2022-11-06'), by='week'), fill=list(RSV=0)) %>%
arrange(country,ORIGIN_SOURCE, date ) %>%
group_by(country, ORIGIN_SOURCE) %>%
mutate(t= row_number()) %>%
ungroup()
```
Plot the time series
```{r}
rsvds %>% filter(ORIGIN_SOURCE=='NONSENTINEL') %>%
ggplot( aes(x=date, y=RSV, group=country ,color=country)) +
geom_line() +
theme_classic()
```
## Harmonic Regression
### Generate harmonicd with specified periods (Fourier series), fit to data
## Create the needed harmonic variables with 12, 24, and 48 month periods
focus on sweden in pre pandemic period
```{r harm1, echo=TRUE}
mod_ds <- rsvds %>%
mutate( sin52 = sin(2*pi*t/52.143),
cos52 = cos(2*pi*t/52.143),
sin104 = sin(2*pi*t*0.5/52.143),
cos104 = cos(2*pi*t*0.5/52.143),
) %>%
filter(country=='Sweden' & ORIGIN_SOURCE=='NONSENTINEL' & date<='2020-03-01')
```
## Fit a simple poisson regression with just 52 week period
```{r harm_reg1}
fit1a <- glm.nb(RSV ~ sin52 + cos52, data=mod_ds )
mod_ds$pred1a<- fitted(fit1a)
```
```{r}
ggplot(mod_ds, aes(x=date, y=RSV)) +
geom_line( color='gray') +
geom_line(aes(x=date, y=pred1a), color='red') +
theme_classic()
```
## Residuals
```{r}
mod_ds <- mod_ds %>%
mutate( log_resid1 = log(RSV+0.5) - log(pred1a+0.5)) # for a log-linked model, residual is observed/expected
```
```{r}
ggplot(mod_ds, aes(x=date, y=log_resid1)) +
geom_line( color='red') +
theme_classic()+
geom_hline(yintercept=0, lty=2, col='gray')
```
## Why does this work?
This is effectively a simple regression, relating X (sin and cos) to log(Y)(cases)-- but we are plotting both as a function of time. If we plot compared to each other, the relationship is more obvious:
```{r}
ggplot(mod_ds, aes(x=sin52, y=log(RSV+0.5))) +
geom_point( color='gray') +
theme_classic()
ggplot(mod_ds, aes(x=cos52, y=log(RSV+0.5))) +
geom_point( color='gray') +
theme_classic()
ggplot(mod_ds, aes(x=(2*cos52 + 2*sin52), y=log(RSV+0.5))) +
geom_point( color='gray') +
theme_classic()
```
## Add in 2 year periodicity
```{r harm_reg2}
fit2a <- glm.nb(RSV ~ sin52+cos52 + sin104 + cos104, data=mod_ds )
mod_ds$pred2a<- fitted(fit2a)
```
```{r}
ggplot(mod_ds, aes(x=date, y=RSV)) +
geom_line( color='gray') +
geom_line(aes(x=date, y=pred2a), color='red') +
theme_classic()
```
### Residuals
```{r}
mod_ds <- mod_ds %>%
mutate( log_resid2 = log(RSV+0.5) - log(pred2a+0.5)) # for a log-linked model, residual is observed/expected
```
```{r}
ggplot(mod_ds, aes(x=date, y=log_resid2)) +
geom_line( color='red') +
geom_line(aes(x=date, y=log_resid1), color='gray') +
theme_classic()+
geom_hline(yintercept=0, lty=2, col='gray')
```