library(purrr)
library(ggplot2)
# Data
df <- data.frame(
xi = simplify(
map(
c(.1,.08,.05,.1,.13,.14,.14,.09,.25),
~ rbinom(50,1,.x))),
p0 = simplify(
map(
c(.1,.1,.1,.1,.1,.1,.1,.15,.2),
~ rnorm(50,.x,.03))),
by = rep(
factor(paste('Subject', c('A','B','C'))),
times = c(150,150,150))
)
# Overall event rate
p0 <- sum(df$xi) / nrow(df)
# Create CUSUM plot
cusum_loglike(
xi = df$xi,
p0 = p0,
p1 = p0 * 1.5,
by = df$by
) |>
ggplot(aes(y = cusum, x = i)) +
geom_step() +
geom_hline(aes(yintercept = h0), linetype = 2) +
geom_hline(aes(yintercept = h1), linetype = 2) +
ylab("Cumulative Log-likelihood Ratio") +
xlab("Case Number") +
facet_wrap(~ by) +
theme_bw()
Run the code above in your browser using DataLab