From 11607be2b77ae3f7711026379b77d755dcaf6ef1 Mon Sep 17 00:00:00 2001 From: Stefano Mangiola Date: Fri, 8 Dec 2023 13:31:11 +1100 Subject: [PATCH] use lme4 to drop random effects --- R/functions_SE.R | 16 ++++++++-------- R/utilities.R | 29 ----------------------------- 2 files changed, 8 insertions(+), 37 deletions(-) diff --git a/R/functions_SE.R b/R/functions_SE.R index 23374aee..a9c087fa 100755 --- a/R/functions_SE.R +++ b/R/functions_SE.R @@ -1100,13 +1100,13 @@ get_differential_transcript_abundance_glmmSeq_SE <- function(.data, omit_contrast_in_colnames = FALSE } - # Check if package is installed, otherwise install - if (find.package("edgeR", quiet = TRUE) %>% length %>% equals(0)) { - message("tidybulk says: Installing edgeR needed for differential transcript abundance analyses") - if (!requireNamespace("BiocManager", quietly = TRUE)) - install.packages("BiocManager", repos = "https://cloud.r-project.org") - BiocManager::install("edgeR", ask = FALSE) - } + # # Check if package is installed, otherwise install + # if (find.package("edgeR", quiet = TRUE) %>% length %>% equals(0)) { + # message("tidybulk says: Installing edgeR needed for differential transcript abundance analyses") + # if (!requireNamespace("BiocManager", quietly = TRUE)) + # install.packages("BiocManager", repos = "https://cloud.r-project.org") + # BiocManager::install("edgeR", ask = FALSE) + # } # Check if package is installed, otherwise install if (find.package("glmmSeq", quiet = TRUE) %>% length %>% equals(0)) { @@ -1136,7 +1136,7 @@ get_differential_transcript_abundance_glmmSeq_SE <- function(.data, # Create design matrix for dispersion, removing random effects design = model.matrix( - object = .formula |> eliminate_random_effects(), + object = .formula |> lme4::nobars(), data = metadata ) diff --git a/R/utilities.R b/R/utilities.R index b59a58e0..1bf9cbe7 100755 --- a/R/utilities.R +++ b/R/utilities.R @@ -1524,32 +1524,3 @@ feature__ = get_special_column_name_symbol(".feature") sample__ = get_special_column_name_symbol(".sample") -#' Produced using ChatGPT - Eliminate Random Effects from a Formula -#' -#' This function takes a mixed-effects model formula and returns a modified -#' formula with all random effects removed, leaving only the fixed effects. -#' -#' @param formula An object of class \code{formula}, representing a mixed-effects model formula. -#' @return A formula object with random effects parts removed. -#' @examples -#' eliminate_random_effects(~ age_days * sex + (1 | file_id) + ethnicity_simplified + assay_simplified + .aggregated_cells + (1 + age_days * sex | tissue)) -#' @noRd -#' @importFrom stats as.formula -eliminate_random_effects <- function(formula) { - # Convert the formula to a string - formula_str <- deparse(formula) - - # Split the string by '+' while keeping the brackets content together - split_str <- unlist(strsplit(formula_str, "(?<=\\))\\s*\\+\\s*|\\+\\s*(?=\\()", perl = TRUE)) - - # Filter out the random effects parts - fixed_effects <- grep("\\|", split_str, value = TRUE, invert = TRUE) - - # Combine the fixed effects parts back into a single string - modified_str <- paste(fixed_effects, collapse = " + ") - - # Convert the modified string back to a formula - modified_formula <- as.formula(modified_str) - - return(modified_formula) -} \ No newline at end of file