knitr::opts_chunk$set( echo = TRUE, warning = FALSE, message = FALSE, error = FALSE, tidy = FALSE, dev = c("png"), cache = TRUE )
Typical analysis using regression models assumes a linear affect of the covariate on the response. Here we consider testing non-linear effects in the case of 1) continuous and 2) ordered categorical variables.
We demonstrate this feature on a lightly modified analysis of PBMCs from 8 individuals stimulated with interferon-β (Kang, et al, 2018, Nature Biotech).
Here is the code from the main vignette:
library(dreamlet) library(muscat) library(ExperimentHub) library(scater) # Download data, specifying EH2259 for the Kang, et al study eh <- ExperimentHub() sce <- eh[["EH2259"]] # only keep singlet cells with sufficient reads sce <- sce[rowSums(counts(sce) > 0) > 0, ] sce <- sce[, colData(sce)$multiplets == "singlet"] # compute QC metrics qc <- perCellQCMetrics(sce) # remove cells with few or many detected genes ol <- isOutlier(metric = qc$detected, nmads = 2, log = TRUE) sce <- sce[, !ol] # set variable indicating stimulated (stim) or control (ctrl) sce$StimStatus <- sce$stim sce$id <- paste0(sce$StimStatus, sce$ind) # Create pseudobulk pb <- aggregateToPseudoBulk(sce, assay = "counts", cluster_id = "cell", sample_id = "id", verbose = FALSE )
Consider the continuous variable Age
. Typical analysis only considers linear effects using a single regression coefficient, but we also want to consider the non-linear effects of age. We can peform a basis expansion using splines instead use 3 coefficients to model the age effect.
# Simulate age between 18 and 65 pb$Age <- runif(ncol(pb), 18, 65) # formula included non-linear effects of Age # by using a natural spline of degree 3 # This corresponds to using 3 coefficients instead of 1 form <- ~ splines::ns(Age, 3) # Normalize and apply voom/voomWithDreamWeights res.proc <- processAssays(pb, form, min.count = 5) # Differential expression analysis within each assay res.dl <- dreamlet(res.proc, form) # The spline has degree 3, so there are 3 coefficients # estimated for Age effects coefNames(res.dl) # Jointly test effects of the 3 spline components # The test of the 3 coefficients is performed with an F-statistic topTable(res.dl, coef = coefNames(res.dl)[2:4], number = 3)
We can also test non-linear effects in the case of categorical variables with a natural ordering to the categories. Consider time course data with 4 time points. Each time point is a category and has a natural ordering from first to last.
We have multiple options to model the time course.
Continuous: Modeling time point as a continuous variable uses a single regression coefficient to model the linear effects of the time course. This is simple, models the order of the time points, but ignores non-linear effects
Model using as.numeric(TimePoint)
Categorical: Including time point as a typical categorical variable uses estimated the mean response value for each category. So it estimates 4 coefficients. While this can be useful for comparing two categories, it ignores the order of the time points.
Model using factor(TimePoint)
Ordered categorical: Here, the trend across ordered time points is modled using orthogonal polynomials. The trend is decomposed into independent linear, quadratic, etc., effects that can be tested either jointly or by themselves.
Model using:
r
ord <- c("time_1", "time_2", "time_3", "time_4")
ordered(factor(TimePoint), ord)
Here we simulated 4 time points, and perform differential expression analysis.
# Consider data generated across 4 time points # While there are no time points in the real data # we can add some for demonstration purposes pb$TimePoint <- ordered(paste0("time_", rep(1:4, 4))) # examine the ordering pb$TimePoint # Use formula including time point form <- ~TimePoint # Normalize and apply voom/voomWithDreamWeights res.proc <- processAssays(pb, form, min.count = 5) # Differential expression analysis within each assay res.dl <- dreamlet(res.proc, form) # Examine the coefficient estimated # for TimePoint it estimates # linear (i.e. L) # quadratic (i.e. Q) # and cubic (i.e. C) effects coefNames(res.dl) # Test only linear effect topTable(res.dl, coef = "TimePoint.L", number = 3) # Test linear, quadratic and cubic effcts coefs <- c("TimePoint.L", "TimePoint.Q", "TimePoint.C") topTable(res.dl, coef = coefs, number = 3)
Due to variation in cell and read count for each sample, processAssays()
filters out some sample. This filtering is summarized here:
details(res.dl)
Whle all 16 samples are detained in B cells, only 9 are retained for megakaryocytes. This can result in a time point being dropped, and so the polynomial expansion for some cell types can have a lower degree. The combined results will then have NA
values for these coefficients. For example, for TIMP1
in Megakaryocytes
above there is not enought data to fit the cubic term, so TimePoint.C
is NA
.
sessionInfo()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.