Skip to content

Instantly share code, notes, and snippets.

@statwonk
Created June 9, 2024 00:45
Show Gist options
  • Save statwonk/cd226ce12dd44c6e6a541ede5f85031c to your computer and use it in GitHub Desktop.
Save statwonk/cd226ce12dd44c6e6a541ede5f85031c to your computer and use it in GitHub Desktop.
A simulation to study the efficiency gain of random effects vs. semiparametric Poisson.
library(tidyverse)
library(sandwich)
library(lmtest)
library(lme4)
patients <- 3e3
seq_len(patients) %>%
map_df(function(id) {
rnorm(1, 0, 1) -> patient_effect
N <- 2
rbinom(N, 1, 0.5) -> trt
tibble(id,
trt,
y = rpois(N, exp(1 + 0.5 * trt +
patient_effect)))
}) -> d
glm(y ~ trt,
data = d,
family = "poisson") -> fit
coeftest(fit, vcovHAC(fit))
glmer(y ~ trt + (1 | id),
data = d,
family = "poisson") %>%
summary()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment