Skip to content

Instantly share code, notes, and snippets.

View sbfnk's full-sized avatar

Sebastian Funk sbfnk

View GitHub Profile
@sbfnk
sbfnk / contacts.r
Created November 1, 2023 14:13
Visualise some aspects of the contact data set
library("tidyr")
library("dplyr")
library("cowplot")
library("stringi")
library("scales")
library("ggplot2")
library("here")
dt_contacts <- readRDS("contacts.rds")
@sbfnk
sbfnk / weekly_forecasts.r
Created October 12, 2023 09:48
Weekly forecast plotting with EpiNow2
#' Aggregate daily forecasts to weekly forecasts
#'
#' Aggregate daily case forecasts from regional_epinow2 to weekly forecasts.
#'
#' @param inf List output from `EpiNow2::regional_epinow()`. No default.
#' @param week_start Week start day. Default is 1 (Monday).
#' @param summary_quantiles Quantiles for forecast summary. Default is median, plus upper and lower bound for 20\%, 50\% and 90\% forecasts.
#' @importFrom dplyr group_by summarise filter mutate select case_when contains
#' @importFrom tidyr pivot_wider
#' @importFrom purrr map_df
# Updated from https://gist.github.com/pkazmier to support yabai
#
# The following configuration heavily leverages modal keymaps to minimize the
# pollution of global keybindings. In addition, the modal keymaps facilitate
# the consistent use of the same keybindings across different modes. For
# example, this configuration uses 'h', 'l', 'j', and 'k' to represent west,
# east, south, and north when: changing focus, warping windows, resizing
# windows, swapping windows, and moving floating windows. Those four keys are
# mapped differently depending on the current mode to provide a consistent user
# experience.
library("readr")
library("dplyr")
df <- readr::read_csv("https://raw.githubusercontent.com/epiforecasts/inc2prev/master/outputs/estimates_age_ab.csv") ## nolint
td <- df |>
dplyr::filter(name == "infections") |>
dplyr::select(t_index, date) |>
dplyr::distinct() |>
dplyr::arrange(t_index)
library("devtools")
library("here")
library("socialmixr")
redo <- FALSE
files_file <- here::here("files.rds")
dir.create(here::here("surveys"), showWarnings = FALSE)
if (!file.exists(files_file) || redo) {
ls <- list_surveys()
@sbfnk
sbfnk / interveral-censored-right-truncated-distribution-estimation-with-brms.R
Last active October 26, 2022 15:41 — forked from seabbs/interveral-censored-right-truncated-distribution-estimation-with-brms.R
This gist shows how to estimate a doubly censored (i.e daily data) and right truncated (i.e due to epidemic phase) distribution using the brms package.
# Load packages
library(brms)
library(cmdstanr)
library(data.table) # here we use the development version of data.table install it with data.table::update_dev_pkg
library(purrr)
library(bpmodels) # devtools::install_github("sbfnk/bpmodels")
# Set up parallel cores
options(mc.cores = 4)
@sbfnk
sbfnk / spain_hosp.r
Created October 24, 2022 10:11
download Spanish hospitalisation data
library("readr")
library("dplyr")
library("janitor")
library("lubridate")
library("ggplot2")
## SOURCE 1: download data
df <- read_delim("https://www.sanidad.gob.es/profesionales/saludPublica/ccayes/alertasActual/nCov/documentos/Datos_Capacidad_Asistencial_Historico_19102022.csv", delim = ";", show_col_types = FALSE)
df_national <- df %>%
library("readr")
library("dplyr")
library("ggplot2")
highlight_models <- c(
"EuroCOVIDhub-ensemble", "KITmetricslab-bivar_branching",
"EuroCOVIDhub-baseline"
)
model_scores <- read_csv(paste0(
library("readr")
library("dplyr")
library("lubridate")
library("here")
library("tidyr")
regional_estimates <-
read_csv(paste0("https://raw.githubusercontent.com/epiforecasts/inc2prev/",
"master/outputs/estimates_regional.csv"))
national_estimates <-
##' Get estimates shown at \url{https://epiforecasts.io/covid} for all times
##'
##' Nowcasts at \url{https://epiforecasts.covid} and the related github repository at
##' \url{https://github.com/epiforecasts/covid-rt-estimates} only cover the last 3
##' months. This function downloads all available estimates and applies a median
##' average to the provided quantiles to provide an estimate of a time series
##' covering all times available
##'
##' @param dataset character; data set corresponding to directories at
##' \url{https://github.com/epiforecasts/covid-rt-estimates}. Default is