Skip to content

Instantly share code, notes, and snippets.

View fdabl's full-sized avatar

Fabian Dablander fdabl

View GitHub Profile
@fdabl
fdabl / covid_party.R
Created October 18, 2024 13:13
Estimating the risk of getting infected with Covid at an ADE party
library(ggplot2)
library(RColorBrewer)
# Roughly estimates the risk of getting Covid by attending an ADE party(N = 900)
# Main uncertainties:
# (1) Number of people infectious
# (2) Number of people a single infectious person infects (R_eff)
#
# For (1), we rely on the number of positive tests from last week. This was 1.5%. Generally, this is an underestimate
# of the true number. Similarly, many people are coming to ADE abroad. We use a distribution over different values below.
@fdabl
fdabl / covid_party.R
Created October 18, 2024 13:13
Estimating the risk of getting infected with Covid at an ADE party
library(ggplot2)
library(RColorBrewer)
# Roughly estimates the risk of getting Covid by attending an ADE party(N = 900)
# Main uncertainties:
# (1) Number of people infectious
# (2) Number of people a single infectious person infects (R_eff)
#
# For (1), we rely on the number of positive tests from last week. This was 1.5%. Generally, this is an underestimate
# of the true number. Similarly, many people are coming to ADE abroad. We use a distribution over different values below.
@fdabl
fdabl / covid_party.R
Created October 18, 2024 13:10
Estimating the risk of getting infected with Covid at an ADE party
library(ggplot2)
library(RColorBrewer)
# Roughly estimates the number of Covid infectious people at an ADE party(N = 900)
# Main uncertainties:
# (1) Number of people infectious
# (2) Number of people a single infectious person infects (R_eff)
#
# For (1), we rely on the number of positive tests from last week. This was 1.5%. Generally, this is an underestimate
# of the true number. Similarly, many people are coming to ADE abroad. We use a distribution over different values below.
@fdabl
fdabl / gist:4eeece12dda8ab81b3b91f39d61758e3
Created October 18, 2024 13:10
Estimating the risk of getting infected with Covid at an ADE party
library(ggplot2)
library(RColorBrewer)
# Roughly estimates the number of Covid infectious people at an ADE party(N = 900)
# Main uncertainties:
# (1) Number of people infectious
# (2) Number of people a single infectious person infects (R_eff)
#
# For (1), we rely on the number of positive tests from last week. This was 1.5%. Generally, this is an underestimate
# of the true number. Similarly, many people are coming to ADE abroad. We use a distribution over different values below.
@fdabl
fdabl / gist:11382ed858b0d3ca505809af1a98c88c
Created October 18, 2024 13:08
Estimating the risk of getting infected with Covid at an ADE party
library(ggplot2)
library(RColorBrewer)
# Roughly estimates the number of Covid infectious people at an ADE party(N = 900)
# Main uncertainties:
# (1) Number of people infectious
# (2) Number of people a single infectious person infects (R_eff)
#
# For (1), we rely on the number of positive tests from last week. This was 1.5%. Generally, this is an underestimate
# of the true number. Similarly, many people are coming to ADE abroad. We use a distribution over different values below.
@fdabl
fdabl / gist:dc7bba8dfb331587efed0a9ad2851c20
Created October 18, 2024 13:07
Estimating the risk of getting infected with Covid at an ADE party
library(ggplot2)
library(RColorBrewer)
# Roughly estimates the number of Covid infectious people at an ADE party(N = 900)
# Main uncertainties:
# (1) Number of people infectious
# (2) Number of people a single infectious person infects (R_eff)
#
# For (1), we rely on the number of positive tests from last week. This was 1.5%. Generally, this is an underestimate
# of the true number. Similarly, many people are coming to ADE abroad. We use a distribution over different values below.
@fdabl
fdabl / variable-selection-comparison.R
Last active January 19, 2023 10:32
Implements three ways to do Bayesian variable selection. For context, see https://fdabl.github.io/r/Spike-and-Slab.html
library('doParallel')
registerDoParallel(cores = 4)
#' Spike-and-Slab Regression using Gibbs Sampling for p > 1 predictors
#'
#' @param y: vector of responses
#' @param X: matrix of predictor values
#' @param nr_samples: indicates number of samples drawn
#' @param a1: parameter a1 of Gamma prior on variance sigma2e
@fdabl
fdabl / dirichlet-triangle.R
Last active December 5, 2018 09:57
Visualizes three dimensional Dirichlet distributions
# devtools::install_github("dkahle/dirichlet")
library('TeX')
library('ggplot2')
library('gridExtra')
library('dirichlet')
plot_dirichlet <- function(alphas = c(.5, .5, .5), add_points = FALSE) {
f <- function(v) ddirichlet(v, alphas)
mesh <- simplex_mesh(.0025) %>% as.data.frame %>% tbl_df
@fdabl
fdabl / ci.R
Last active July 30, 2018 14:24
bfx <- function(x, tstat, n, mu, gamma, k, v = n - 2) {
num <- integrate(function(delta) {
dt(tstat, df = v, ncp = sqrt(n)*delta)/gamma * dnorm((delta - mu)/gamma)
}, -Inf, Inf)$value
dt(tstat, df = v, ncp = sqrt(n)*x) / num
}
lo <- uniroot(function(x) bfx(x, .5, 10, 0, .3) - 1, interval = c(-1, 0))$root
# for https://www.facebook.com/efpsa.jeps/posts/1278843852167100
library('ggplot2')
library('gridExtra')
set.seed(1774)
# generate data
A = c(rnorm(20, 20, 4), rnorm(20, 40, 4)) # mixture distribution
B = rt(30, 1) + 30 # from a heavy-tailed distribution (Cauchy)
C = c(25, 35, 33) # just three data points