diff --git a/R/simulate_LFQ_data.R b/R/simulate_LFQ_data.R index 3a55f65c..3038c75c 100644 --- a/R/simulate_LFQ_data.R +++ b/R/simulate_LFQ_data.R @@ -266,24 +266,37 @@ sim_lfq_data_2Factor_config <- function(Nprot = 10, #' @export #' @keywords internal #' @examples -#' mod <- sim_build_models_lm(model = "interaction", weight_missing = 1) +#' modi <- sim_build_models_lm(model = "interaction", weight_missing = 1) #' stopifnot(dim(mod$modelDF) == c(10,9)) +#' mod2 <- sim_build_models_lm(model = "parallel2", weight_missing = 1) +#' mod$modelDF$linear_model[[1]] +#' mod3 <- sim_build_models_lm(model = "parallel3", weight_missing = 1) +#' modf <- sim_build_models_lm(model = "factors", weight_missing = 1) #' -sim_build_models_lm <- function(model = c("factors", "interaction"), +sim_build_models_lm <- function(model = c("parallel2","parallel3","factors", "interaction"), Nprot = 10, with_missing = TRUE, weight_missing = 1) { model <- match.arg(model) - model <- if (model == "factors") { - "~ Treatment + Background" - } else { - "~ Treatment * Background" - } + if (model != "parallel3") { istar <- prolfqua::sim_lfq_data_2Factor_config( Nprot = Nprot, with_missing = with_missing, weight_missing = weight_missing) + } else { + istar <- prolfqua::sim_lfq_data_protein_config() + } istar <- prolfqua::LFQData$new(istar$data,istar$config) + + model <- if (model == "factors") { + "~ Treatment + Background" + } else if (model == "interaction") { + "~ Treatment * Background" + } else if (model == "parallel2") { + "~ Treatment" + } else if (model == "parallel3") { + "~ group_" + } else {NULL} modelFunction <- strategy_lm(paste0(istar$response(), model)) mod <- build_model( istar, @@ -297,25 +310,40 @@ sim_build_models_lm <- function(model = c("factors", "interaction"), #' @keywords internal #' @examples #' undebug(sim_build_models_lmer) -#' mod <- sim_build_models_lmer(model = "interaction", weight_missing = 1) -#' stopifnot(dim(mod$modelDF) == c(10,9)) +#' modi <- sim_build_models_lmer(model = "interaction", weight_missing = 1) +#' stopifnot(sum(modi$modelDF$exists_lmer) == 6) +#' mod2 <- sim_build_models_lmer(model = "parallel2", weight_missing = 1) +#' stopifnot(sum(mod2$modelDF$exists_lmer) == 6) +#' mod4 <- sim_build_models_lmer(model = "parallel3", weight_missing = 1) +#' stopifnot(sum(mod4$modelDF$exists_lmer) == 6) +#' modf <- sim_build_models_lmer(model = "factors", weight_missing = 1) +#' stopifnot(sum(modf$modelDF$exists_lmer) == 6) #' -sim_build_models_lmer <- function(model = c("factors", "interaction"), +sim_build_models_lmer <- function(model = c("parallel2", "parallel3","factors", "interaction"), Nprot = 10, with_missing = TRUE, weight_missing = 1) { model <- match.arg(model) + if (model != "parallel3") { + istar <- prolfqua::sim_lfq_data_2Factor_config( + Nprot = Nprot, + with_missing = with_missing, + PEPTIDE = TRUE, + weight_missing = weight_missing) + } else { + istar <- prolfqua::sim_lfq_data_peptide_config() + } + istar <- prolfqua::LFQData$new(istar$data,istar$config) + model <- if (model == "factors") { "~ Treatment + Background + (1|peptide_Id) + (1|sampleName)" - } else { + } else if (model == "interaction") { "~ Treatment * Background + (1|peptide_Id) + (1|sampleName)" - } - istar <- prolfqua::sim_lfq_data_2Factor_config( - Nprot = Nprot, - with_missing = with_missing, - PEPTIDE = TRUE, - weight_missing = weight_missing) - istar <- prolfqua::LFQData$new(istar$data,istar$config) + } else if (model == "parallel2") { + "~ Treatment + (1|peptide_Id) + (1|sampleName)" + } else if (model == "parallel3") { + "~ group_ + (1|peptide_Id) + (1|sampleName)" + } else {NULL} modelFunction <- strategy_lmer(paste0(istar$response(), model)) mod <- build_model( istar, @@ -330,9 +358,16 @@ sim_build_models_lmer <- function(model = c("factors", "interaction"), #' @keywords internal #' @examples #' m <- sim_make_model_lm() -#' m <- sim_make_model_lm("interaction") -#' -sim_make_model_lm <- function(model = c("factors", "interaction")){ +#' mi <- sim_make_model_lm("interaction") +#' length(coef(mi)) == 4 +#' mf <- sim_make_model_lm("factors") +#' length(coef(mf)) = 3 +#' m2 <- sim_make_model_lm("parallel2") +#' length(coef(m2)) == 2 +#' m3 <- sim_make_model_lm("parallel3") +#' length(coef(m3)) == 3 +sim_make_model_lm <- function(model = c("parallel2", "parallel3","factors", "interaction")){ + model <- match.arg(model) mod <- sim_build_models_lm(model = model, Nprot = 1, with_missing = FALSE) return(mod$modelDF$linear_model[[1]]) } @@ -347,7 +382,9 @@ sim_make_model_lm <- function(model = c("factors", "interaction")){ #' mf <- sim_make_model_lmer("factors") #' mf <- sim_make_model_lmer("interaction") #' -sim_make_model_lmer <- function(model = c("factors", "interaction"), singular = FALSE){ +sim_make_model_lmer <- function(model = c("parallel2", "parallel3","factors", "interaction"), + singular = FALSE){ + model <- match.arg(model) mod <- sim_build_models_lmer(model = model, Nprot = 10, with_missing = FALSE) m <- mod$modelDF |> dplyr::filter(isSingular == isSingular) |> dplyr::pull(linear_model) return(m[[1]]) diff --git a/R/tidyMS_R6_Modelling.R b/R/tidyMS_R6_Modelling.R index 16486b5f..06166d92 100644 --- a/R/tidyMS_R6_Modelling.R +++ b/R/tidyMS_R6_Modelling.R @@ -384,7 +384,14 @@ plot_lmer_peptide_predictions <- function(m, intensity = "abundance"){ # Generate linear functions ----- -# get matrix of indicator coefficients for each interaction +#' get matrix of indicator coefficients for each interaction +#' @examples +#' m <- sim_make_model_lm("interaction") +#' coef(m) +#' prolfqua:::.lmer4_coeff_matrix(m) +#' m <- sim_make_model_lm("factors") +#' m +#' prolfqua:::.lmer4_coeff_matrix(m) .lmer4_coeff_matrix <- function(m){ data <- NULL if ("lm" %in% class(m)) { @@ -443,21 +450,22 @@ plot_lmer_peptide_predictions <- function(m, intensity = "abundance"){ #' @keywords internal #' @examples #' -#' m <- prolfqua_data('data_basicModel_p1807') -#' # debug(linfct_from_model) -#' linfct <- linfct_from_model(m) +#' m <- sim_make_model_lm() +#' linfct <- linfct_from_model(m, as_list = TRUE) #' #' linfct$linfct_factors #' linfct$linfct_interactions -#' -#' m <- prolfqua_data('data_interactionModel_p1807') -#' # debug(.coeff_weights_factor_levels) +#' lf <- matrix( +#' c(1, 1, 1, 1, 0.5, 0.5, 0, 1, 0, 1, 0.5, 0.5), +#' nrow = 4, +#' byrow = FALSE, +#' dimnames = list(c("BackgroundX", "BackgroundZ", "TreatmentA", "TreatmentB"), +#' c("(Intercept)", "TreatmentB", "BackgroundZ")) +#' ) +#' stopifnot(lf == linfct$linfct_factors) +#' m <- sim_make_model_lm("interaction") #' linfct <- linfct_from_model(m) #' -#' all.equal(linfct$linfct_factors["CelltypeCMP/MEP",] , -#' apply(linfct$linfct_interactions[grep("CelltypeCMP/MEP", rownames(linfct$linfct_interactions)),],2, mean)) -#' linfct$linfct_interactions -#' #' m <- lm(Petal.Width ~ Species, data = iris) #' linfct_from_model(m) #' xx <- data.frame( Y = 1:10 , Condition = c(rep("a",5), rep("b",5)) ) @@ -471,7 +479,6 @@ plot_lmer_peptide_predictions <- function(m, intensity = "abundance"){ #' linfct_from_model(m) #' linfct_from_model <- function(m, as_list = TRUE){ - cm <- .lmer4_coeff_matrix(m) cm_mm <- cm$mm[order(rownames(cm$mm)),] diff --git a/man/dot-lmer4_coeff_matrix.Rd b/man/dot-lmer4_coeff_matrix.Rd new file mode 100644 index 00000000..7d64be38 --- /dev/null +++ b/man/dot-lmer4_coeff_matrix.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/tidyMS_R6_Modelling.R +\name{.lmer4_coeff_matrix} +\alias{.lmer4_coeff_matrix} +\title{get matrix of indicator coefficients for each interaction} +\usage{ +.lmer4_coeff_matrix(m) +} +\description{ +get matrix of indicator coefficients for each interaction +} +\examples{ +m <- sim_make_model_lm("interaction") +coef(m) +prolfqua:::.lmer4_coeff_matrix(m) +m <- sim_make_model_lm("factors") +m +prolfqua:::.lmer4_coeff_matrix(m) +} diff --git a/man/linfct_from_model.Rd b/man/linfct_from_model.Rd index 375a98a3..e58bd31d 100644 --- a/man/linfct_from_model.Rd +++ b/man/linfct_from_model.Rd @@ -14,21 +14,22 @@ get linfct from model } \examples{ -m <- prolfqua_data('data_basicModel_p1807') -# debug(linfct_from_model) -linfct <- linfct_from_model(m) +m <- sim_make_model_lm() +linfct <- linfct_from_model(m, as_list = TRUE) linfct$linfct_factors linfct$linfct_interactions - -m <- prolfqua_data('data_interactionModel_p1807') -# debug(.coeff_weights_factor_levels) +lf <- matrix( +c(1, 1, 1, 1, 0.5, 0.5, 0, 1, 0, 1, 0.5, 0.5), +nrow = 4, +byrow = FALSE, +dimnames = list(c("BackgroundX", "BackgroundZ", "TreatmentA", "TreatmentB"), + c("(Intercept)", "TreatmentB", "BackgroundZ")) +) +stopifnot(lf == linfct$linfct_factors) +m <- sim_make_model_lm("interaction") linfct <- linfct_from_model(m) -all.equal(linfct$linfct_factors["CelltypeCMP/MEP",] , - apply(linfct$linfct_interactions[grep("CelltypeCMP/MEP", rownames(linfct$linfct_interactions)),],2, mean)) -linfct$linfct_interactions - m <- lm(Petal.Width ~ Species, data = iris) linfct_from_model(m) xx <- data.frame( Y = 1:10 , Condition = c(rep("a",5), rep("b",5)) ) diff --git a/man/sim_build_models_lm.Rd b/man/sim_build_models_lm.Rd index 9b4b1ab5..530c3cf9 100644 --- a/man/sim_build_models_lm.Rd +++ b/man/sim_build_models_lm.Rd @@ -5,7 +5,7 @@ \title{build dataframe with models for testing} \usage{ sim_build_models_lm( - model = c("factors", "interaction"), + model = c("parallel2", "parallel3", "factors", "interaction"), Nprot = 10, with_missing = TRUE, weight_missing = 1 @@ -15,8 +15,12 @@ sim_build_models_lm( build dataframe with models for testing } \examples{ -mod <- sim_build_models_lm(model = "interaction", weight_missing = 1) +modi <- sim_build_models_lm(model = "interaction", weight_missing = 1) stopifnot(dim(mod$modelDF) == c(10,9)) +mod2 <- sim_build_models_lm(model = "parallel2", weight_missing = 1) +mod$modelDF$linear_model[[1]] +mod3 <- sim_build_models_lm(model = "parallel3", weight_missing = 1) +modf <- sim_build_models_lm(model = "factors", weight_missing = 1) } \seealso{ diff --git a/man/sim_build_models_lmer.Rd b/man/sim_build_models_lmer.Rd index e5e62d5d..bb474e21 100644 --- a/man/sim_build_models_lmer.Rd +++ b/man/sim_build_models_lmer.Rd @@ -5,7 +5,7 @@ \title{build lmer model from simulated data} \usage{ sim_build_models_lmer( - model = c("factors", "interaction"), + model = c("parallel2", "parallel3", "factors", "interaction"), Nprot = 10, with_missing = TRUE, weight_missing = 1 @@ -16,8 +16,14 @@ build lmer model from simulated data } \examples{ undebug(sim_build_models_lmer) -mod <- sim_build_models_lmer(model = "interaction", weight_missing = 1) -stopifnot(dim(mod$modelDF) == c(10,9)) +modi <- sim_build_models_lmer(model = "interaction", weight_missing = 1) +stopifnot(sum(modi$modelDF$exists_lmer) == 6) +mod2 <- sim_build_models_lmer(model = "parallel2", weight_missing = 1) +stopifnot(sum(mod2$modelDF$exists_lmer) == 6) +mod4 <- sim_build_models_lmer(model = "parallel3", weight_missing = 1) +stopifnot(sum(mod4$modelDF$exists_lmer) == 6) +modf <- sim_build_models_lmer(model = "factors", weight_missing = 1) +stopifnot(sum(modf$modelDF$exists_lmer) == 6) } \seealso{ diff --git a/man/sim_make_model_lm.Rd b/man/sim_make_model_lm.Rd index deeedebf..6cdea7fa 100644 --- a/man/sim_make_model_lm.Rd +++ b/man/sim_make_model_lm.Rd @@ -4,15 +4,23 @@ \alias{sim_make_model_lm} \title{make interaction model for examples} \usage{ -sim_make_model_lm(model = c("factors", "interaction")) +sim_make_model_lm( + model = c("parallel2", "parallel3", "factors", "interaction") +) } \description{ make interaction model for examples } \examples{ m <- sim_make_model_lm() -m <- sim_make_model_lm("interaction") - +mi <- sim_make_model_lm("interaction") +length(coef(mi)) == 4 +mf <- sim_make_model_lm("factors") +length(coef(mf)) = 3 +m2 <- sim_make_model_lm("parallel2") +length(coef(m2)) == 2 +m3 <- sim_make_model_lm("parallel3") +length(coef(m3)) == 3 } \seealso{ Other modelling: diff --git a/man/sim_make_model_lmer.Rd b/man/sim_make_model_lmer.Rd index 688c3611..18de2967 100644 --- a/man/sim_make_model_lmer.Rd +++ b/man/sim_make_model_lmer.Rd @@ -4,7 +4,10 @@ \alias{sim_make_model_lmer} \title{make interaction model for examples} \usage{ -sim_make_model_lmer(model = c("factors", "interaction"), singular = FALSE) +sim_make_model_lmer( + model = c("parallel2", "parallel3", "factors", "interaction"), + singular = FALSE +) } \description{ make interaction model for examples