Skip to content

Instantly share code, notes, and snippets.

View fdabl's full-sized avatar

Fabian Dablander fdabl

View GitHub Profile
@fdabl
fdabl / power.R
Created June 21, 2025 06:51
Power analysis two proportions
library(pwr)
library(dplyr)
library(ggplot2)
compute_sample_size <- function(
p1, p2, power = 0.8, sig.level = 0.05, alternative = 'two.sided'
) {
h <- ES.h(p1, p2) # Cohen's h
result <- pwr.2p.test(
h = h, power = power,
@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