if (FALSE) {
# Simulate Bayesian operating characteristics for an unadjusted
# proportional odds comparison (Wilcoxon test)
# For 100 simulations, 5 looks, 2 true parameter values, and
# 2 assertion/prior combinations, compute the posterior probability
# Use a low-level logistic regression call to speed up simuluations
# Use data.table to compute various summary measures
# Total simulation time: 2s
lfit <- function(x, y) {
f <- rms::lrm.fit(x, y)
k <- length(coef(f))
c(coef(f)[k], vcov(f)[k, k])
}
gdat <- function(beta, n1, n2) {
# Cell probabilities for a 7-category ordinal outcome for the control group
p <- c(2, 1, 2, 7, 8, 38, 42) / 100
# Compute cell probabilities for the treated group
p2 <- pomodm(p=p, odds.ratio=exp(beta))
y1 <- sample(1 : 7, n1, p, replace=TRUE)
y2 <- sample(1 : 7, n2, p2, replace=TRUE)
list(y1=y1, y2=y2)
}
# Assertion 1: log(OR) < 0 under prior with prior mean 0.1 and sigma 1 on log OR scale
# Assertion 2: OR between 0.9 and 1/0.9 with prior mean 0 and sigma computed so that
# P(OR > 2) = 0.05
asserts <- list(list('Efficacy', '<', 0, mu=0.1, sigma=1),
list('Similarity', 'in', log(c(0.9, 1/0.9)),
cutprior=log(2), tailprob=0.05))
set.seed(1)
est <- estSeqSim(c(0, log(0.7)), looks=c(50, 75, 95, 100, 200),
gendat=gdat,
fitter=lfit, nsim=100)
z <- gbayesSeqSim(est, asserts)
head(z)
attr(z, 'asserts')
# Compute the proportion of simulations that hit targets (different target posterior
# probabilities for efficacy vs. similarity)
# For the efficacy assessment compute the first look at which the target
# was hit (set to infinity if never hit)
require(data.table)
z <- data.table(z)
u <- z[, .(first=min(p1 > 0.95)), by=.(parameter, sim)]
# Compute the proportion of simulations that ever hit the target and
# that hit it by the 100th subject
u[, .(ever=mean(first < Inf)), by=.(parameter)]
u[, .(by75=mean(first <= 100)), by=.(parameter)]
}
Run the code above in your browser using DataLab