Created
June 9, 2024 00:45
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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