# sums over data sets, by model x method x stratum
band_of <- function(st) ifelse(st %in% c("0-0.1", "0.9-1"), "extreme",
ifelse(st == "0.3-0.7", "middle", "between"))
runs$band <- band_of(runs$stratum)
cols <- "cbind(null_n, null_p05, dm_n, dm_call, big_n, big_call, call_n, call_null, call_tiny)"
S <- aggregate(as.formula(paste(cols, "~ mix + model + method + stratum")), data = runs, FUN = sum)
S$band <- band_of(S$stratum)
B <- aggregate(as.formula(paste(cols, "~ mix + model + method + band")), data = S, FUN = sum)
Tot <- aggregate(as.formula(paste(cols, "~ mix + model + method")), data = S, FUN = sum)
rate <- function(tab, mod, meth, col, den, band = NULL, mx = "array") {
keep <- tab$mix == mx & tab$model == mod & tab$method == meth
if (!is.null(band)) keep <- keep & tab$band %in% band
r <- tab[keep, ]
sum(r[[col]]) / sum(r[[den]])
}
per_set <- function(mod, meth, mx = "array")
sum(Tot$call_n[Tot$mix == mx & Tot$model == mod & Tot$method == meth]) / n_sets
# residual SD and prior df, from the first logit-normal data set
fb1 <- lmFit(example_ln$beta, design); fm1 <- lmFit(example_ln$M, design)
eb1 <- eBayes(fb1); em1 <- eBayes(fm1)
sd_beta_ext <- median(fb1$sigma[example_ln$stratum %in% c("0-0.1", "0.9-1")])
sd_beta_mid <- median(fb1$sigma[example_ln$stratum == "0.3-0.7"])
sd_m_ext <- median(fm1$sigma[example_ln$stratum %in% c("0-0.1", "0.9-1")])
sd_m_mid <- median(fm1$sigma[example_ln$stratum == "0.3-0.7"])
d0_beta <- eb1$df.prior; d0_m <- em1$df.prior
s0_beta <- sqrt(eb1$s2.prior)
ext <- "extreme"; mid <- "middle"; other <- c("middle", "between")
t1 <- function(mod, meth, band, mx = "array") rate(B, mod, meth, "null_p05", "null_n", band, mx)
pw <- function(mod, meth, band, mx = "array") rate(B, mod, meth, "dm_call", "dm_n", band, mx)
tiny_share <- function(mod, meth, band = NULL) {
tab <- if (is.null(band)) Tot else B
rate(tab, mod, meth, "call_tiny", "call_n", band)
}
big_power <- function(mod, meth) rate(Tot, mod, meth, "big_call", "big_n")
# range of the type I error over the five strata
t1_range <- function(mod, meth, mx = "array") {
r <- S[S$mix == mx & S$model == mod & S$method == meth, ]
range(r$null_p05 / r$null_n)
}
# Monte Carlo error of a pooled rate, from the spread between data sets (CpGs within a data set
# share one estimated prior, so they are not independent)
mc_se <- function(mod, meth, bnd, mx = "array") {
r <- runs[runs$mix == mx & runs$model == mod & runs$method == meth & runs$band == bnd, ]
per <- tapply(r$null_p05, r$set, sum) / tapply(r$null_n, r$set, sum)
sd(per) / sqrt(length(per))
}
null_mid_n <- sum(B$null_n[B$mix == "array" & B$model == "logit-normal" & B$method == "limma, M" &
B$band == mid])
mc_se_max <- max(sapply(c("array", "even"), function(mx) sapply(c("logit-normal", "beta"),
function(mod) sapply(c("limma, beta", "limma, M", "limma-trend, beta", "limma-trend, M"),
function(meth) sapply(c(ext, mid), function(bnd) mc_se(mod, meth, bnd, mx))))))
t_rng <- range(sapply(c("array", "even"), function(mx) sapply(c("logit-normal", "beta"), function(mod)
sapply(c("t-test, beta", "t-test, M"), function(meth) t1_range(mod, meth, mx)))))
trend_m_rng <- range(sapply(c("array", "even"), function(mx)
sapply(c("logit-normal", "beta"), function(mod) t1_range(mod, "limma-trend, M", mx))))
# how many M units a delta-beta of 0.05 is, near the edge and in the middle
dM_at <- function(m) logit2(m + db_min / 2) - logit2(m - db_min / 2)
edge_centre <- 0.05
dM_edge <- dM_at(edge_centre); dM_mid <- dM_at(lfc_centre)
# the delta-beta that the fixed M threshold lfc_M allows at a mean of m_edge
dM_at_db <- function(m, db) logit2(m + db / 2) - logit2(m - db / 2)
db_for_lfc <- function(m) uniroot(function(db) dM_at_db(m, db) - lfc_M, c(1e-6, 2 * m - 1e-6))$root
m_edge <- 0.03
db_edge_lfc <- db_for_lfc(m_edge)
# named scalars quoted in the text; ln = logit-normal model, bt = beta model
ln <- "logit-normal"; bt <- "beta"
t1_b_ext <- t1(ln, "limma, beta", ext); t1_b_mid <- t1(ln, "limma, beta", mid)
t1_m_ext <- t1(ln, "limma, M", ext); t1_m_mid <- t1(ln, "limma, M", mid)
pw_b_ext <- pw(ln, "limma, beta", ext); pw_m_ext <- pw(ln, "limma, M", ext)
pw_b_mid <- pw(ln, "limma, beta", mid); pw_m_mid <- pw(ln, "limma, M", mid)
bt_t1_m_ext <- t1(bt, "limma, M", ext); bt_t1_m_mid <- t1(bt, "limma, M", mid)
bt_t1_b_ext <- t1(bt, "limma, beta", ext); bt_t1_b_mid <- t1(bt, "limma, beta", mid)
bt_pw_m_ext <- pw(bt, "limma, M", ext); bt_pw_tm_ext <- pw(bt, "limma-trend, M", ext)
bt_pw_m_mid <- pw(bt, "limma, M", mid); bt_pw_tm_mid <- pw(bt, "limma-trend, M", mid)
trend_b_rng <- range(sapply(c(ln, bt), function(mod) t1_range(mod, "limma-trend, beta")))
tb_t1_ext <- t1(ln, "limma-trend, beta", ext); tb_t1_mid <- t1(ln, "limma-trend, beta", mid)
tb_pw_mid <- pw(ln, "limma-trend, beta", mid); tm_pw_mid <- pw(ln, "limma-trend, M", mid)
calls_tb_bt <- per_set(bt, "limma-trend, beta")
# the check with evenly spread means (ev_)
ev_b_ext <- t1(ln, "limma, beta", ext, "even"); ev_b_mid <- t1(ln, "limma, beta", mid, "even")
ev_pw_b_ext <- pw(ln, "limma, beta", ext, "even"); ev_pw_m_ext <- pw(ln, "limma, M", ext, "even")
ev_bt_m_ext <- t1(bt, "limma, M", ext, "even"); ev_bt_m_mid <- t1(bt, "limma, M", mid, "even")
d0_beta_even <- eBayes(lmFit(example_even$beta, design))$df.prior
calls_m_ln <- per_set(ln, "limma, M"); calls_t_m_ln <- per_set(ln, "t-test, M")
calls_tm_bt <- per_set(bt, "limma-trend, M"); calls_m_bt <- per_set(bt, "limma, M")
fdp_rng <- range(sapply(c(ln, bt), function(mod) sapply(c("limma, beta", "limma, M"),
function(meth) rate(Tot, mod, meth, "call_null", "call_n"))))
calls_t_b_ln <- per_set(ln, "t-test, beta"); calls_b_ln <- per_set(ln, "limma, beta")
tiny_m_ext <- tiny_share(ln, "limma, M", ext); tiny_m_oth <- tiny_share(ln, "limma, M", other)
tiny_trM_ext <- tiny_share(ln, "treat on M", ext); tiny_trM_oth <- tiny_share(ln, "treat on M", other)
filt <- "limma, M, then |delta-beta| filter"
tiny_filt <- tiny_share(ln, filt); tiny_trB <- tiny_share(ln, "treat on beta")
bt_tiny_filt <- tiny_share(bt, filt); bt_tiny_trB <- tiny_share(bt, "treat on beta")
big_filt <- big_power(ln, filt); big_trB <- big_power(ln, "treat on beta")
calls_filt <- per_set(ln, filt); calls_trB <- per_set(ln, "treat on beta")
calls_trM <- per_set(ln, "treat on M")
tiny_b_ext <- tiny_share(ln, "limma, beta", ext)
bx <- B[B$mix == "array" & B$model == ln & B$method == "limma, M" & B$band == ext, ]
real_tiny_ext <- (bx$call_tiny - bx$call_null) / bx$call_tiny # true change, but below db_min