Skip to content

Instantly share code, notes, and snippets.

View sbfnk's full-sized avatar

Sebastian Funk sbfnk

View GitHub Profile
diff --git a/t/010_cpu.t b/t/010_cpu.t
index d71bfa4..497bdfb 100644
--- a/t/010_cpu.t
+++ b/t/010_cpu.t
@@ -1,3 +1,4 @@
use Test::More tests => 1;
-is(system('script/libbi sample @test.conf') >> 8, 0, 'CPU');
+system('env 1>&2');
+is(system('script/libbi sample @test.conf --verbose') >> 8, 0, 'CPU');
@sbfnk
sbfnk / bp_trees.r
Created February 11, 2019 09:07
simulate branching process trees with unobserved nodes/ancestors
library('tidyverse')
library('bpmodels')
library('epicontacts')
obs_tree <- function(obs_prob=0.5, stop_threshold=100, ...) {
## generate chains
chains <- chain_sim(infinite=stop_threshold, tree=T, ...)
observed <- chains %>%
mutate(observed=factor(rbinom(n=n(), size=1, prob=obs_prob),
@sbfnk
sbfnk / gist:3b27eca3bfcc2244f5bbb13e13e57525
Last active January 24, 2020 13:33
Simulate outbreak sizes
## devtools::install_github("sbfnk/bpmodels")
library('bpmodels')
## 41 initial exposure events
n <- 41
## exposure events distributed over `nexp` days
nexp <- 26 ## 8 Dec - 2 Jan according to http://virological.org/t/epidemiological-data-from-the-ncov-2019-outbreak-early-descriptions-from-publicly-available-data/337/1
## randomly sample exposure events
t0 <- sample(seq(1, 26), n, replace = TRUE)
## mean of serial interval distribution
mean_si <- 8.4 ## from Lispsitch et al. (2003)
@sbfnk
sbfnk / time_R.r
Last active January 29, 2020 09:40
code to estimate time-varying R0 for nCoV
library('tidyverse')
library('ggplot2')
##' Samples size (the number of trials) of a binomial distribution
##' copied from https://github.com/sbfnk/bpmodels/blob/master/R/utils.r
rbinom_size <- function(n, x, prob) {
x + stats::rnbinom(n, x + 1, prob)
}
##
@sbfnk
sbfnk / china_contacts.r
Created February 4, 2020 22:00
China contacts
library('socialmixr')
library('tidyverse')
## get China survey
s <- get_survey("https://doi.org/10.5281/zenodo.3366396")
## contact matrix
contact_matrix(s, age.limits = seq(0, 15, 5))
## $matrix
@sbfnk
sbfnk / google_mob.r
Last active April 27, 2020 15:50
google vs. rt
library("tidyverse")
library("ggplot2")
library("cowplot")
## get mobility data
dm <- read_csv("https://www.gstatic.com/covid19/mobility/Global_Mobility_Report.csv") %>%
mutate(date = as.Date(date, format = "%d/%m/%Y")) %>%
filter(is.na(sub_region_1)) %>%
rename(country = country_region) %>%
gather(type, value, 6:ncol(.)) %>%
data <- list(
est1 = c(`0.025` = 0.84, `0.5` = 0.9, `0.975` = 0.96),
est2 = c(`0.025` = 0.32, `0.5` = 0.53, `0.975` = 0.66)
)
fn_gamma <- function(par, x) {
quantiles <- as.numeric(names(x))
return(sum((qgamma(quantiles, shape = par[1], rate = par[2]) - x)**2))
}
@sbfnk
sbfnk / get_utla_estimates.r
Created June 17, 2021 11:32
get all UTLA-level Rt estimates
library("gh")
library("vroom")
owner <- "epiforecasts"
repo <- "covid-rt-estimates"
path <- "subnational/united-kingdom-local/cases/summary/rt.csv"
rt_commits <-
gh("/repos/{owner}/{repo}/commits?path={path}",
owner = owner,
@sbfnk
sbfnk / get_covid19_nowcasts_all_times.r
Created November 26, 2021 17:02
get R estimates for all times
##' 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
##' 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