Skip to content

Commit

Permalink
use lme4 to drop random effects
Browse files Browse the repository at this point in the history
  • Loading branch information
stemangiola committed Dec 8, 2023
1 parent 8c17496 commit 11607be
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 37 deletions.
16 changes: 8 additions & 8 deletions R/functions_SE.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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
)

Expand Down
29 changes: 0 additions & 29 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 11607be

Please sign in to comment.