-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added a Gaussian noise around a sinusoidal mean
- Loading branch information
Showing
5 changed files
with
108 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
^docs$ | ||
^pkgdown$ | ||
^\.github$ | ||
^vignettes/articles$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
vignettes/articles/fitting_synthetic_data_including_covariates.Rmd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
--- | ||
title: "Fitting synthetic data including covariates" | ||
--- | ||
|
||
```{r, include = FALSE} | ||
knitr::opts_chunk$set( | ||
collapse = TRUE, | ||
comment = "#>" | ||
) | ||
``` | ||
|
||
```{r setup} | ||
library(epidp) | ||
``` | ||
|
||
In this document, we explore how the incorporation of covariate information affects | ||
estimation of $R_t$. | ||
|
||
## Added Gaussian noise around a sinusoidal covariate-driven mean $R_t$ | ||
```{r} | ||
# define sinusoidal Rt with noise | ||
rt_fun <- function(t, x) { | ||
x[1] * exp(x[2]) | ||
} | ||
nt <- 200 | ||
t <- 1:nt | ||
f <- 1.3 + 1.2 * sin(4 * (pi / 180) * t) | ||
g <- vector(length = nt) | ||
g[1] <- rnorm(1, 0, 1) | ||
rho <- 0.8 | ||
for(i in 2:length(g)) { | ||
g[i] <- rho * g[i - 1] + rnorm(1, 0, 0.1) | ||
} | ||
X <- matrix(c(f, g), nrow = length(x), ncol = 2) | ||
# simulation parameters | ||
mean_si <- 6.5 | ||
sd_si <- 4.03 | ||
i_0 <- 10 | ||
# data frame of outputs | ||
epidemic_df <- simulate_renewal_epidemic(rt_fun, nt, mean_si, sd_si, i_0, X) | ||
# plot | ||
epidemic_df %>% | ||
mutate(f=f) %>% | ||
select(-c(w_dist, lambda_t)) %>% | ||
pivot_longer(c(R_t, f)) %>% | ||
ggplot(aes(x = t, y = value, colour = name)) + | ||
geom_line() + | ||
scale_y_continuous( | ||
name = "Incidence (i_t)", | ||
sec.axis = sec_axis(~ . / 1000, name = "Reproduction Number (R_t)") | ||
) + | ||
labs(x = "Time", colour = "Variable") + | ||
theme_minimal() | ||
``` | ||
We first try to estimate $R_t$ without covariate information. | ||
We now use a Stan version of EpiFilter to estimate the $R_t$ profile. | ||
```{r} | ||
# fit model | ||
fit <- fit_epifilter( | ||
N=length(epidemic_df$i_t), | ||
C=epidemic_df$i_t, | ||
w=epidemic_df$w_dist, | ||
iter=200 | ||
) | ||
# look at MCMC summaries | ||
print(fit, c("sigma", "R")) | ||
``` | ||
|
||
We now overlay the estimated $R_t$ versus the actual values. The estimated $R_t$ values | ||
coincide reasonably with the true values. | ||
```{r} | ||
# extract posterior quantiles | ||
R_draws <- rstan::extract(fit, "R")[[1]] | ||
lower <- apply(R_draws, 2, function(x) quantile(x, 0.025)) | ||
middle <- apply(R_draws, 2, function(x) quantile(x, 0.5)) | ||
upper <- apply(R_draws, 2, function(x) quantile(x, 0.975)) | ||
# plot | ||
epidemic_df %>% | ||
mutate( | ||
lower=lower, | ||
middle=middle, | ||
upper=upper | ||
) %>% | ||
select(t, R_t, lower, middle, upper) %>% | ||
ggplot(aes(x=t, y=R_t)) + | ||
geom_line(colour="red") + | ||
geom_ribbon(aes(ymin=lower, ymax=upper), fill="blue", alpha=0.4) + | ||
geom_line(aes(y=middle)) | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters