Skip to content

Commit

Permalink
generating different types of models.
Browse files Browse the repository at this point in the history
  • Loading branch information
wolski committed May 16, 2024
1 parent c51885e commit adf666a
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 53 deletions.
81 changes: 59 additions & 22 deletions R/simulate_LFQ_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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]])
}
Expand All @@ -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]])
Expand Down
31 changes: 19 additions & 12 deletions R/tidyMS_R6_Modelling.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)) )
Expand All @@ -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)),]

Expand Down
19 changes: 19 additions & 0 deletions man/dot-lmer4_coeff_matrix.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 11 additions & 10 deletions man/linfct_from_model.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions man/sim_build_models_lm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions man/sim_build_models_lmer.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions man/sim_make_model_lm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/sim_make_model_lmer.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit adf666a

Please sign in to comment.