Skip to content

Commit

Permalink
added to vignette the grocery example with dp financial data
Browse files Browse the repository at this point in the history
  • Loading branch information
ben18785 committed Nov 14, 2024
1 parent 1e1c465 commit 0bdf9f1
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 2 deletions.
14 changes: 14 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,17 @@
#' @source <https://github.com/TRACE-LAC/pet-epi-notebooks/tree/main>
#' @source <https://www.google.com/covid19/mobility/>
"covid_colombia_cases_deaths_mobility"


#' Daily COVID-19 case data for Bogota and Medellin and corresponding Google mobility data
#'
#' @format ## `bogota_financial_time_series`
#' A data frame with 1348 rows and 10 columns:
#' \describe{
#' \item{date}{Daily}
#' \item{city}{Bogota or Medellin}
#' \item{cases}{Daily counts of COVID-19 cases}
#' \item{deaths}{Daily counts of COVID-19 deaths}
#' ...
#' }
"bogota_financial_time_series"
Binary file added data/bogota_financial_time_series.rda
Binary file not shown.
2 changes: 1 addition & 1 deletion inst/stan/epifilter.stan
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ model {

generated quantities {
vector[N] log_likelihood;
for(t in 1:N)
for(t in 2:N)
log_likelihood[t] = poisson_lpmf(C[t]|E_cases[t]);
}
2 changes: 1 addition & 1 deletion inst/stan/epifilter_covariates.stan
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ model {

generated quantities {
vector[N] log_likelihood;
for(t in 1:N)
for(t in 2:N)
log_likelihood[t] = poisson_lpmf(C[t]|E_cases[t]);
}
26 changes: 26 additions & 0 deletions man/bogota_financial_time_series.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions vignettes/articles/fitting_real_covid19_data.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,95 @@ fit <- fit_epifilter_covariates(
print(fit, "beta[2]")
```
This negative association probably is a result of individuals responding to the COVID-19 pandemic conditions or governmental policy.

# Determining drivers of $R_t$ for Bogota using financial time series
```{r}
data("bogota_financial_time_series")
```

Smooth the weekly time series
```{r}
daily_dates <- seq(
min(bogota_financial_time_series$date),
max(bogota_financial_time_series$date), by = "day")
merchants <- unique(bogota_financial_time_series$merch_category)
for(i in seq_along(merchants)) {
df_short <- bogota_financial_time_series %>%
filter(merch_category==merchants[i])
spendamountusd_interpolated <- approx(
df_short$date,
df_short$spendamt, xout = daily_dates)$y
countamountusd_interpolated <- approx(df_short$date, df_short$nb_transactions, xout = daily_dates)$y
df_daily <- tibble(
date=daily_dates,
spendamountusd=spendamountusd_interpolated,
countamountusd=countamountusd_interpolated
) %>%
mutate(merch_type=merchants[i])
if(i == 1)
big_df <- df_daily
else
big_df <- big_df %>% bind_rows(df_daily)
}
df_both <- big_df %>%
left_join(covid_colombia_cases_deaths_mobility) %>%
filter(city=="Bogota")
```

Here, we demonstrate how we can investigate whether the (smoothed) daily number of transactions in Bogota is associated with $R_$. We pick the "Grocery Stores/Supermarkets" category here because this is likely a particularly high contact shop.

```{r}
df_supermarkets <- df_both %>%
filter(merch_type=="Grocery Stores/Supermarkets")
X <- tibble(
cons = rep(1, nrow(df_bogota)),
m = df_supermarkets$countamountusd
) %>%
mutate(
m = scale(m)[, 1]
) %>%
as.matrix()
options(mc.cores=4)
fit <- fit_epifilter_covariates(
N = nrow(df_supermarkets),
C = df_supermarkets$cases,
w = w,
X = X,
is_sampling = TRUE,
iter = 200,
chains = 4
)
print(fit, "beta[2]")
```
Plotting $R_t$ using these estimates.
```{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
df_supermarkets %>%
mutate(
lower = lower,
middle = middle,
upper = upper
) %>%
select(date, lower, middle, upper) %>%
filter(date >= as.Date("2020-04-01")) %>%
ggplot(aes(x = date)) +
geom_ribbon(aes(ymin = lower, ymax = upper), fill = "blue", alpha = 0.6) +
geom_line(aes(y = middle), colour = "blue") +
geom_hline(yintercept = 1, linetype = 2, colour = "orange") +
xlab("Date") +
ylab("R_t")
```


0 comments on commit 0bdf9f1

Please sign in to comment.