multi_resids: Get the residuals from multiple linear models.

Description Usage Arguments Value Examples

View source: R/multi-lm.R

Description

There is the option to add the mean (or median for robust models) to the residuals.

Usage

1
multi_resids(lms, lms_data = NULL, reset_mean_med = FALSE, new_data = NULL)

Arguments

lms

A list of fitted linear models (the results of calls to stats::lm() or MASS::rlm()). Most likely, the output of a call to multi_lm().

lms_data

The data frame that was passed as the df argument to multi_lm(), if multi_lm() was used to create lms.

reset_mean_med

A flag. If TRUE, for each variable for which the residuals are calculated the mean (or in the case of robust linear models, the median) of the original values of these variables is added to the residuals, such that the output is again centred on this value.

new_data

Rather than calculating the residuals on the data where the model was fit, you can pass a new dataset and calculate the residuals there.

Value

A data frame of the residuals. The variables in this data frame will have their original names. If lms_data is given, other columns (ones for which residuals were not calculated) of that data frame will be returned as is.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
if (rlang::is_installed("mirmisc")) {
  gars_data <- get_combined_cohort_data(c("ga", "rs"), log2 = TRUE) %>%
    dplyr::mutate(
      cohort = dplyr::if_else(startsWith(mirvie_id, "RS"), "RS", "GA"),
      cohort = factor(cohort),
      meta_major_race = forcats::fct_drop(meta_major_race)
    ) %>%
    dplyr::filter(!is.na(meta_major_race), !is.na(meta_collectionga))
  xs <- c("cohort", "meta_major_race", "meta_collectionga")
  ys <- gars_data %>%
    dplyr::select(dplyr::any_of(mirmisc::get_gene_names())) %>%
    purrr::map_dbl(sum) %>%
    sort() %>%
    tail(100) %>%
    names() # ys are the highest expressed 100 genes
  lms <- multi_lm(gars_data, xs, ys)
  resids <- multi_resids(lms, gars_data, reset_mean_med = TRUE)
}

mirvie/mirmodels documentation built on Jan. 14, 2022, 11:12 a.m.