Skip to content

Instantly share code, notes, and snippets.

View martinmodrak's full-sized avatar

Martin Modrák martinmodrak

View GitHub Profile
@martinmodrak
martinmodrak / scaled_dirichlet.R
Last active July 10, 2024 18:52
Expectation of scaled dirichlet w.r.t. the aitchinson base
p <- MCMCpack::rdirichlet(1, rep(1, 4))[1,]
N <- 10000
g2 <- matrix(rnorm(length(p) * N), nrow = length(p))^2
pg2 = sweep(g2, MARGIN = 1, STATS = p, FUN = "*")
pg2_norm <- sweep(pg2, MARGIN = 2, STATS = colSums(pg2), FUN = "/")
@martinmodrak
martinmodrak / sbc_rejection_per_datapoint.R
Created February 27, 2024 12:45
SBC shows failures when rejecting individual data points
library(SBC)
library(cmdstanr)
library(bayesplot)
library(posterior)
library(future)
plan(multisession)
@martinmodrak
martinmodrak / variance_reparametrization.Rmd
Created April 25, 2023 08:26
Variance reparametrization
```{r}
library(cmdstanr)
```
```{r}
intercept_uncentered <- cmdstan_model(write_stan_file("data {
int<lower=1> N;
vector[N] signal;
int<lower = 1> N_subj;
vector[N] c_cloze;
@martinmodrak
martinmodrak / error_in_q975.R
Created February 3, 2023 10:03
Simulation to show distribution of estimated tail quantiles from a specific number of draws
library(ggplot2)
N_sims <- 1000
N_draws <- 4000
sims <- matrix(rnorm(N_sims * N_draws), nrow = N_sims, ncol = N_draws)
q975 <- apply(sims, MARGIN = 1, FUN = quantile, prob = 0.975)
true_q975 <- qnorm(0.975)
ggplot(data.frame(x = q975), aes(x)) +
geom_histogram() +
#Profile
ggplot(data.frame(x = 1:3, y = c(5, 7, 1)), aes(x, y)) +
geom_bar(stat = "identity", fill = "#6364ff", color = FALSE) +
theme_void() +
theme(plot.margin = margin(15,15,15,15))
# Header
set.seed(55465)
x <- 0:100
m <- splines::bs(x = x, df = 5, intercept = TRUE) %*% matrix(rnorm(5 * 50), ncol = 50, nrow = 5)
@martinmodrak
martinmodrak / sim_cohen.R
Created December 15, 2022 14:41
Simulation showing how the same difference in means can lead to arbitrary Cohen's d
library(ggplot2)
library(effsize)
set.seed(156226)
N <- 50
groups <- rep(c("A","B"), each = N)
means <- rep(c(20,18), each = N)
noise <- rnorm(2*N, 0, 1)
noise <- noise - mean(noise)
df1 <- data.frame(group = groups, y = means + noise * 0.001)
library(tidyverse)
library(lubridate)
quakes_raw <- read_csv("https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.csv")
quakes <- quakes_raw %>% mutate(hour = floor_date(time, "hour"),
day = floor_date(time, "day"))
quake_fanos <- quakes %>% group_by(hour, day, net) %>%
summarise(count = n()) %>%
group_by(day, net) %>%
@martinmodrak
martinmodrak / sim_chisq_halving.R
Created December 6, 2021 10:31
Simulation check to show halving p-values for chi-squared test _could_ be a valid statistical procedure
library(ggplot2)
N_treatment <- 709
N_control <- 699
N_sims <- 1e4
prob_event <- 0.1
# Assuming prob(event|treatment) = prob(event|control)
# The case prob(event|treatment) > prob(event|control) produces fewer false positives (type-I errors)
pos_control <- rbinom(N_sims, size = N_control, prob = prob_event)
@martinmodrak
martinmodrak / divergence_example.Rmd
Created June 23, 2021 12:34
Code to generate example animations with leapfrog integrator
```{r}
library(tidyverse)
library(gganimate)
theme_set(cowplot::theme_cowplot())
```
```{r}
quad <- function(x) {
-x^2
@martinmodrak
martinmodrak / run.R
Created May 27, 2021 12:53
Soft sum to zero constraint breaks ADVI
library(cmdstanr)
model_code <- "
data {
int N;
vector[N] y;
int K;
int<lower=1, upper=K> groups[N];
}