R/H-Test.R

Defines functions perform_h_test

Documented in perform_h_test

#' Half's Modified t-Test for Differential Gene Expression
#'
#' Computes a modified t-statistic (sometimes called "Half's t-test") that uses
#' only the control standard deviation in its denominator, applies BH correction,
#' and returns a results table together with the list of significant DEGs.
#'
#' @inheritParams perform_t_test
#'
#' @return A named list:
#'   \describe{
#'     \item{Table}{Data.frame with columns \code{G_Symbol}, \code{log2FC},
#'       \code{statistic}, \code{p.value}, \code{BH}, \code{fdr}.}
#'     \item{DEGs}{Data.frame of significant gene identifiers.}
#'   }
#'
#' @export
#' @importFrom stats sd pt p.adjust na.omit quantile
#' @examples
#' library(DGEAR)
#' data("gene_exp_data")
#' result <- perform_h_test(dataframe = gene_exp_data,
#'                          con1 = 1, con2 = 10,
#'                          exp1 = 11, exp2 = 20)
#' head(result$Table)
#' result$DEGs
perform_h_test <- function(dataframe, con1, con2, exp1, exp2,
                           alpha = 0.05, annot_df = NULL) {
  prep     <- .prepare_data(dataframe, con1, con2, exp1, exp2)
  G_Symbol <- .resolve_symbols(prep$dataframe, annot_df)
  con      <- prep$con
  exp      <- prep$exp
  log2FC   <- prep$log2FC
  
  s0          <- apply(con, 1, sd, na.rm = TRUE)
  sample_size <- sqrt((1 / ncol(exp)) + (1 / ncol(con)))
  statistic   <- log2FC / (s0 * sample_size)
  
  half_t_stat        <- data.frame(G_Symbol, log2FC, statistic,
                                   stringsAsFactors = FALSE)
  half_t_stat$p.value <- 2 * pt(abs(statistic), df = nrow(con) - 1L,
                                lower.tail = FALSE)
  half_t_stat$BH     <- p.adjust(half_t_stat$p.value, method = "BH")
  half_t_stat$fdr    <- as.integer(half_t_stat$BH <= alpha)
  
  DEGs <- data.frame(
    DEGs = na.omit(half_t_stat$G_Symbol[half_t_stat$fdr == 1L]),
    stringsAsFactors = FALSE)
  list(Table = half_t_stat, DEGs = DEGs)
}

Try the DGEAR package in your browser

Any scripts or data that you put into this service are public.

DGEAR documentation built on July 3, 2026, 9:07 a.m.