# LRT vs Wald in DESeq2: which question is tested? # https://pseudocount.com/posts/lrt-vs-wald-deseq2/ # # The R code of the post, in the order the page runs it. Run it top to bottom in a fresh # R session. Commented-out blocks are shown on the page but not run there. The package # versions behind the numbers on the page are listed at the end of the post. ## ---- setup ---- suppressPackageStartupMessages({ library(DESeq2) library(edgeR) library(ggplot2) }) stopifnot(packageVersion("DESeq2") >= "1.40", packageVersion("edgeR") >= "4.0.0", packageVersion("ggplot2") >= "3.4") pct <- function(x, d = 0) sprintf("%.*f%%", d, 100 * x) num <- function(x, d = 2) sprintf("%.*f", d, x) int <- function(x) format(x, big.mark = ",", scientific = FALSE, trim = TRUE) violet <- "#5a3fc0"; magenta <- "#d14fa6"; grey <- "#8a8797" theme_pc <- theme_minimal(base_size = 12) + theme(plot.background = element_rect(fill = "white", colour = "white"), panel.grid.minor = element_blank(), legend.position = "bottom") pat_lab <- c(null = "no effect", shift = "constant shift", single = "t2 only", updown = "up then down") ## ---- precompute: the page runs design, simulate, fit, wald, numbers here ---- ## ---- design ---- cd <- data.frame(group = factor(rep(c("ctrl", "trt"), each = 12)), time = factor(rep(rep(paste0("t", 0:3), each = 3), 2))) table(cd$group, cd$time) X <- model.matrix(~ group + time + group:time, cd) colnames(X) ## ---- simulate ---- G <- 3000 n_pat <- c(null = 1800, shift = 400, single = 400, updown = 400) pat <- factor(rep(names(n_pat), n_pat), levels = names(n_pat)) # true group effect (trt minus ctrl, log2 scale) at t0, t1, t2, t3 for an effect of size 1 prof <- rbind(null = c(0, 0, 0, 0), shift = c(1, 1, 1, 1), single = c(0, 0, 1, 0), updown = c(0, 1, 0, -1)) lfc_range <- c(0.7, 1.5) set.seed(2026) mu0 <- exp(rnorm(G, log(150), 1.3)) # baseline mean count phi <- 0.03 + 0.3 / sqrt(mu0) # NB dispersion, higher for low counts size <- runif(G, lfc_range[1], lfc_range[2]) * sample(c(-1, 1), G, TRUE) tcourse <- cbind(0, matrix(rnorm(G * 3, 0, 0.5), G, 3)) # time course shared by both groups eff <- prof[as.character(pat), ] * size # G x 4 group effects ti <- as.integer(cd$time); is_trt <- cd$group == "trt" lf <- tcourse[, ti] + eff[, ti] * rep(is_trt, each = G) mu <- mu0 * 2^lf * rep(runif(24, 0.8, 1.25), each = G) cts <- matrix(rnbinom(G * 24, mu = mu, size = 1 / phi), G, 24) storage.mode(cts) <- "integer" ## ---- fit ---- alpha <- 0.05 dds <- DESeqDataSetFromMatrix(cts, cd, design = ~ group + time + group:time) # LRT 1: drop the interaction. Does the group difference change over time? dds_lrt <- DESeq(dds, test = "LRT", reduced = ~ group + time, quiet = TRUE) res_int <- results(dds_lrt, alpha = alpha) # LRT 2: drop group and the interaction. Is there a group difference at any time? dds_any <- nbinomLRT(dds_lrt, reduced = ~ time, quiet = TRUE) res_any <- results(dds_any, alpha = alpha) ## ---- wald ---- dds_wald <- DESeq(dds, quiet = TRUE) # standard workflow, Wald test res_t0 <- results(dds_wald, name = "group_trt_vs_ctrl", alpha = alpha) # trt vs ctrl at t1, t2, t3: the t0 difference plus that time's interaction res_tk <- lapply(c(t1 = 1, t2 = 2, t3 = 3), function(k) results(dds_wald, alpha = alpha, contrast = list(c("group_trt_vs_ctrl", paste0("grouptrt.timet", k))))) ## ---- numbers ---- sig <- function(p) !is.na(p) & p < alpha union_w <- sig(res_t0$padj) | Reduce(`|`, lapply(res_tk, function(r) sig(r$padj))) calls <- cbind(LRT_int = sig(res_int$padj), LRT_any = sig(res_any$padj), Wald_t0 = sig(res_t0$padj), Wald_t2 = sig(res_tk$t2$padj), Wald_union = union_w) rt <- t(apply(calls, 2, function(s) tapply(s, pat, mean))) # share called, per gene type n_calls <- colSums(calls) fdp <- function(s, neg) mean(pat[s] %in% neg) # share of calls that are false fdp_any <- fdp(calls[, "LRT_any"], "null") fdp_union <- fdp(calls[, "Wald_union"], "null") df_int <- ncol(X) - ncol(model.matrix(~ group + time, cd)) # coefficients dropped df_any <- ncol(X) - ncol(model.matrix(~ time, cd)) int_null <- rt["LRT_int", "null"]; int_shift <- rt["LRT_int", "shift"] int_single <- rt["LRT_int", "single"]; int_updown <- rt["LRT_int", "updown"] any_shift <- rt["LRT_any", "shift"]; any_single <- rt["LRT_any", "single"] t0_shift <- rt["Wald_t0", "shift"]; t0_single <- rt["Wald_t0", "single"] t0_updown <- rt["Wald_t0", "updown"]; t2_single <- rt["Wald_t2", "single"] n_int <- n_calls[["LRT_int"]]; n_any <- n_calls[["LRT_any"]] n_t0 <- n_calls[["Wald_t0"]]; n_union <- n_calls[["Wald_union"]] null_p05_any <- mean(res_any$pvalue[pat == "null"] < 0.05, na.rm = TRUE) # calibration check null_p05_t0 <- mean(res_t0$pvalue[pat == "null"] < 0.05, na.rm = TRUE) list(rates = round(rt, 3), calls = n_calls, other = c(fdp_any = fdp_any, fdp_union = fdp_union, df_int = df_int, df_any = df_any, null_p05_any = null_p05_any, null_p05_t0 = null_p05_t0)) ## ---- design ---- cd <- data.frame(group = factor(rep(c("ctrl", "trt"), each = 12)), time = factor(rep(rep(paste0("t", 0:3), each = 3), 2))) table(cd$group, cd$time) X <- model.matrix(~ group + time + group:time, cd) colnames(X) ## ---- simulate: shown here on the page, run above ---- ## ---- fig-patterns ---- ex <- expand.grid(time = levels(cd$time), group = levels(cd$group), pattern = c("null", "shift", "single", "updown"), stringsAsFactors = FALSE) tc_ex <- c(t0 = 0, t1 = 0.8, t2 = 0.3, t3 = -0.4) # illustrative shared time course prof_ex <- rbind(null = c(0, 0, 0, 0), shift = c(1, 1, 1, 1), single = c(0, 0, 1, 0), updown = c(0, 1, 0, -1)) colnames(prof_ex) <- names(tc_ex) ex$log2mu <- 7 + tc_ex[ex$time] + (ex$group == "trt") * prof_ex[cbind(ex$pattern, ex$time)] ex$pattern <- factor(pat_lab[ex$pattern], levels = pat_lab) ggplot(ex, aes(time, log2mu, colour = group, group = group)) + geom_line(linewidth = 1) + geom_point(size = 2.2) + facet_wrap(~ pattern, nrow = 1) + scale_colour_manual(values = c(ctrl = grey, trt = violet), name = NULL) + labs(x = NULL, y = "true log2 mean") + theme_pc ## ---- fit: shown here on the page, run above ---- ## ---- wald: shown here on the page, run above ---- ## ---- numbers ---- sig <- function(p) !is.na(p) & p < alpha union_w <- sig(res_t0$padj) | Reduce(`|`, lapply(res_tk, function(r) sig(r$padj))) calls <- cbind(LRT_int = sig(res_int$padj), LRT_any = sig(res_any$padj), Wald_t0 = sig(res_t0$padj), Wald_t2 = sig(res_tk$t2$padj), Wald_union = union_w) rt <- t(apply(calls, 2, function(s) tapply(s, pat, mean))) # share called, per gene type n_calls <- colSums(calls) fdp <- function(s, neg) mean(pat[s] %in% neg) # share of calls that are false fdp_any <- fdp(calls[, "LRT_any"], "null") fdp_union <- fdp(calls[, "Wald_union"], "null") df_int <- ncol(X) - ncol(model.matrix(~ group + time, cd)) # coefficients dropped df_any <- ncol(X) - ncol(model.matrix(~ time, cd)) int_null <- rt["LRT_int", "null"]; int_shift <- rt["LRT_int", "shift"] int_single <- rt["LRT_int", "single"]; int_updown <- rt["LRT_int", "updown"] any_shift <- rt["LRT_any", "shift"]; any_single <- rt["LRT_any", "single"] t0_shift <- rt["Wald_t0", "shift"]; t0_single <- rt["Wald_t0", "single"] t0_updown <- rt["Wald_t0", "updown"]; t2_single <- rt["Wald_t2", "single"] n_int <- n_calls[["LRT_int"]]; n_any <- n_calls[["LRT_any"]] n_t0 <- n_calls[["Wald_t0"]]; n_union <- n_calls[["Wald_union"]] null_p05_any <- mean(res_any$pvalue[pat == "null"] < 0.05, na.rm = TRUE) # calibration check null_p05_t0 <- mean(res_t0$pvalue[pat == "null"] < 0.05, na.rm = TRUE) list(rates = round(rt, 3), calls = n_calls, other = c(fdp_any = fdp_any, fdp_union = fdp_union, df_int = df_int, df_any = df_any, null_p05_any = null_p05_any, null_p05_t0 = null_p05_t0)) ## ---- fig-rates ---- test_lab <- c(LRT_int = "LRT, reduced ~ group + time", LRT_any = "LRT, reduced ~ time", Wald_t0 = "Wald, group_trt_vs_ctrl", Wald_t2 = "Wald, trt vs ctrl at t2", Wald_union = "Wald, any of the 4 times") hm <- expand.grid(test = names(test_lab), pattern = colnames(rt), stringsAsFactors = FALSE) hm$rate <- rt[cbind(hm$test, hm$pattern)] hm$test <- factor(test_lab[hm$test], levels = rev(test_lab)) hm$pattern <- factor(pat_lab[hm$pattern], levels = pat_lab) ggplot(hm, aes(pattern, test, fill = rate)) + geom_tile(colour = "white", linewidth = 1) + geom_text(aes(label = pct(rate), colour = rate > 0.5), size = 3.8, show.legend = FALSE) + scale_fill_gradient(low = "#f1eefa", high = violet, limits = c(0, 1), guide = "none") + scale_colour_manual(values = c("TRUE" = "white", "FALSE" = "grey20")) + scale_x_discrete(position = "top") + labs(x = NULL, y = NULL) + theme_pc + theme(panel.grid = element_blank()) ## ---- same-p ---- nm <- resultsNames(dds_lrt) nm by_name <- lapply(setNames(nm[-1], nm[-1]), function(n) results(dds_lrt, name = n, alpha = alpha)) p_same <- all(vapply(by_name, function(r) identical(r$pvalue, res_int$pvalue), TRUE)) lfc_same <- identical(by_name$group_trt_vs_ctrl$log2FoldChange, by_name$grouptrt.timet2$log2FoldChange) lfc_default_last <- identical(res_int$log2FoldChange, by_name[[nm[length(nm)]]]$log2FoldChange) c(p_same = p_same, lfc_same = lfc_same, lfc_default_last = lfc_default_last) mcols(by_name$group_trt_vs_ctrl)$description[c(2, 5)] ## ---- fig-confusion ---- tab <- as.data.frame(by_name$group_trt_vs_ctrl)[, c("log2FoldChange", "pvalue", "padj")] tab$pattern <- factor(pat_lab[as.character(pat)], levels = pat_lab) tab <- tab[!is.na(tab$pvalue), ] tab$called <- ifelse(sig(tab$padj), "padj below cut-off", "not called") ggplot(tab, aes(log2FoldChange, -log10(pmax(pvalue, 1e-300)), colour = called)) + geom_point(size = 0.9, alpha = 0.6) + facet_wrap(~ pattern, nrow = 1) + scale_colour_manual(values = c("padj below cut-off" = violet, "not called" = "grey70"), name = NULL) + coord_cartesian(xlim = c(-3, 3), ylim = c(0, 20)) + labs(x = "log2FoldChange column (t0 effect)", y = "-log10 LRT p-value") + theme_pc ## ---- confusion-numbers ---- shift_big <- pat == "shift" & abs(by_name$group_trt_vs_ctrl$log2FoldChange) > 1 n_shift_big <- sum(shift_big) n_shift_big_called <- sum(sig(by_name$group_trt_vs_ctrl$padj)[shift_big]) called_dyn <- sig(res_int$padj) & pat %in% c("single", "updown") med_lfc_dyn <- median(abs(by_name$group_trt_vs_ctrl$log2FoldChange[called_dyn])) c(n_shift_big = n_shift_big, n_shift_big_called = n_shift_big_called, med_lfc_dyn = med_lfc_dyn) ## ---- wald-from-lrt ---- w_t0 <- results(dds_lrt, name = "group_trt_vs_ctrl", test = "Wald", alpha = alpha) w_t2 <- results(dds_lrt, test = "Wald", alpha = alpha, contrast = list(c("group_trt_vs_ctrl", "grouptrt.timet2"))) max_diff_wald <- max(abs(c(w_t0$pvalue - res_t0$pvalue, w_t2$pvalue - res_tk$t2$pvalue, w_t0$log2FoldChange - res_t0$log2FoldChange)), na.rm = TRUE) mcols(w_t0)$description[5] max_diff_wald ## ---- edger ---- y <- DGEList(cts, group = interaction(cd$group, cd$time)) keep <- filterByExpr(y, design = X) y <- normLibSizes(y[keep, , keep.lib.sizes = FALSE]) fit <- glmQLFit(y, X, legacy = FALSE) inter <- grep(":", colnames(X), value = TRUE) # the three interaction columns ql_int <- glmQLFTest(fit, coef = inter) # like the LRT against ~ group + time ql_any <- glmQLFTest(fit, coef = c("grouptrt", inter)) # like the LRT against ~ time ## ---- edger-numbers ---- ql_call <- function(q) { # genes removed by filterByExpr count as not called s <- rep(FALSE, G); s[keep] <- p.adjust(q$table$PValue, "BH") < alpha; s } rt_ql <- rbind(QL_int = tapply(ql_call(ql_int), pat, mean), QL_any = tapply(ql_call(ql_any), pat, mean)) ql_int_shift <- rt_ql["QL_int", "shift"]; ql_int_single <- rt_ql["QL_int", "single"] ql_any_shift <- rt_ql["QL_any", "shift"] round(rt_ql, 3)