Since read counts are summed across cells in a pseudobulk approach, modeling continuous cell-level covariates also requires a collapsing step. Here we summarize the values of a variable from a set of cells using the mean, and store the value for each cell type. Including these variables in a regression formula uses the summarized values from the corresponding cell type.
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
In many datasets, continuous cell-level variables could be mapped reads, gene count, mitochondrial rate, etc. There are no continuous cell-level variables in this dataset, so we can simulate two from a normal distribution:
sce$value1 <- rnorm(ncol(sce))
sce$value2 <- rnorm(ncol(sce))
Now compute the pseudobulk using standard code:
sce$id <- paste0(sce$StimStatus, sce$ind)
# Create pseudobulk
pb <- aggregateToPseudoBulk(sce,
assay = "counts",
cluster_id = "cell",
sample_id = "id",
verbose = FALSE
)
The means per variable, cell type, and sample are stored in the pseudobulk SingleCellExperiment object:
metadata(pb)$aggr_means
## # A tibble: 128 × 5
## # Groups: cell [8]
## cell id cluster value1 value2
## <fct> <fct> <dbl> <dbl> <dbl>
## 1 B cells ctrl101 3.96 0.191 -0.0834
## 2 B cells ctrl1015 4.00 0.0249 0.0182
## 3 B cells ctrl1016 4 -0.0997 0.127
## 4 B cells ctrl1039 4.04 -0.135 -0.416
## 5 B cells ctrl107 4 0.0517 -0.181
## 6 B cells ctrl1244 4 0.138 0.0233
## 7 B cells ctrl1256 4.01 -0.00264 0.141
## 8 B cells ctrl1488 4.02 0.0570 0.0275
## 9 B cells stim101 4.09 0.127 0.0870
## 10 B cells stim1015 4.06 -0.0846 -0.0996
## # ℹ 118 more rows
Including these variables in a regression formula uses the summarized values from the corresponding cell type. This happens behind the scenes, so the user doesn’t need to distinguish bewteen sample-level variables stored in colData(pb) and cell-level variables stored in metadata(pb)$aggr_means.
Variance partition and hypothesis testing proceeds as ususal:
form <- ~ StimStatus + value1 + value2
# Normalize and apply voom/voomWithDreamWeights
res.proc <- processAssays(pb, form, min.count = 5)
# run variance partitioning analysis
vp.lst <- fitVarPart(res.proc, form)
# Summarize variance fractions genome-wide for each cell type
plotVarPart(vp.lst, label.angle = 60)
# Differential expression analysis within each assay
res.dl <- dreamlet(res.proc, form)
# dreamlet results include coefficients for value1 and value2
res.dl
## class: dreamletResult
## assays(8): B cells CD14+ Monocytes ... Megakaryocytes NK cells
## Genes:
## min: 164
## max: 5262
## details(7): assay n_retain ... n_errors error_initial
## coefNames(4): (Intercept) StimStatusstim value1 value2
A variable in colData(sce) is handled according to if the variable is
metadata(pb)$aggr_meanscolData(pb)## R version 4.6.1 (2026-06-24)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.5 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.24-bioc/R/lib/libRblas.so
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0 LAPACK version 3.12.0
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_GB LC_COLLATE=C
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## time zone: America/New_York
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] muscData_1.27.1 scater_1.42.0
## [3] scuttle_1.24.0 ExperimentHub_3.4.0
## [5] AnnotationHub_4.4.0 BiocFileCache_3.4.0
## [7] dbplyr_2.6.0 muscat_1.28.0
## [9] dreamlet_1.12.0 SingleCellExperiment_1.36.0
## [11] SummarizedExperiment_1.44.0 Biobase_2.74.0
## [13] GenomicRanges_1.66.0 Seqinfo_1.4.0
## [15] IRanges_2.48.0 S4Vectors_0.52.0
## [17] BiocGenerics_0.60.0 generics_0.1.4
## [19] MatrixGenerics_1.26.0 matrixStats_1.5.0
## [21] variancePartition_1.44.0 BiocParallel_1.48.0
## [23] limma_4.0.0 ggplot2_4.0.4
## [25] BiocStyle_2.42.0
##
## loaded via a namespace (and not attached):
## [1] bitops_1.1-0 httr_1.4.9
## [3] RColorBrewer_1.1-3 doParallel_1.0.17
## [5] Rgraphviz_2.58.0 numDeriv_2016.8-1.1
## [7] tools_4.6.1 backports_1.5.1
## [9] utf8_1.2.6 R6_2.6.1
## [11] metafor_5.2-1 mgcv_1.9-5
## [13] GetoptLong_1.1.1 withr_3.0.3
## [15] prettyunits_1.2.0 gridExtra_2.3.1
## [17] cli_3.6.6 sandwich_3.1-3
## [19] labeling_0.4.3 sass_0.5.0
## [21] KEGGgraph_1.74.0 SQUAREM_2026.1
## [23] mvtnorm_1.4-2 S7_1.0.0
## [25] blme_1.0-8 mixsqp_0.3-54
## [27] zenith_1.16.0 dichromat_2.0-1
## [29] invgamma_1.2 RSQLite_3.53.3
## [31] shape_1.4.6.1 gtools_3.9.5
## [33] dplyr_1.2.1 Matrix_1.7-6
## [35] scrapper_1.8.0 metadat_1.6-0
## [37] ggbeeswarm_0.7.3 abind_1.4-8
## [39] lifecycle_1.0.5 multcomp_1.4-32
## [41] yaml_2.3.12 edgeR_5.0.0
## [43] mathjaxr_2.0-0 gplots_3.3.0
## [45] SparseArray_1.14.0 grid_4.6.1
## [47] blob_1.3.0 crayon_1.5.3
## [49] lattice_0.23-1 beachmat_2.30.0
## [51] msigdbr_26.1.1 annotate_1.92.0
## [53] KEGGREST_1.54.0 magick_2.9.1
## [55] pillar_1.11.1 knitr_1.52
## [57] ComplexHeatmap_2.30.0 rjson_0.2.23
## [59] boot_1.3-32 estimability_2.0.0
## [61] corpcor_1.6.10 codetools_0.2-20
## [63] glue_1.8.1 data.table_1.18.6.1
## [65] vctrs_0.7.3 png_0.1-9
## [67] Rdpack_2.6.6 gtable_0.3.6
## [69] assertthat_0.2.1 cachem_1.1.0
## [71] zigg_0.0.2 xfun_0.62
## [73] rbibutils_2.4.1 S4Arrays_1.14.0
## [75] Rfast_2.1.5.2 coda_0.19-4.1
## [77] reformulas_0.4.4 survival_3.8-13
## [79] iterators_1.0.14 tinytex_0.61
## [81] statmod_1.5.2 TH.data_1.1-5
## [83] nlme_3.1-172 pbkrtest_0.5.5
## [85] bit64_4.8.6 filelock_1.0.3
## [87] progress_1.2.3 EnvStats_3.1.0
## [89] bslib_0.12.0 TMB_1.9.25
## [91] irlba_2.4.1 vipor_0.4.7
## [93] KernSmooth_2.23-27 otel_0.3.0
## [95] colorspace_2.1-4 rmeta_3.0
## [97] DBI_1.3.0 tidyselect_1.2.1
## [99] emmeans_2.0.5 curl_8.2.0
## [101] bit_4.6.0 compiler_4.6.1
## [103] httr2_1.3.0 graph_1.92.0
## [105] BiocNeighbors_2.8.0 DelayedArray_0.40.0
## [107] bookdown_0.48 scales_1.4.0
## [109] caTools_1.18.4 remaCor_0.0.20
## [111] rappdirs_0.3.4 stringr_1.6.0
## [113] digest_0.6.39 minqa_1.2.8
## [115] rmarkdown_2.32 aod_1.3.3
## [117] XVector_0.54.0 RhpcBLASctl_0.23-42
## [119] htmltools_0.5.9 pkgconfig_2.0.3
## [121] lme4_2.1-0 sparseMatrixStats_1.26.0
## [123] mashr_0.2.79 fastmap_1.2.0
## [125] rlang_1.3.0 GlobalOptions_0.1.4
## [127] DelayedMatrixStats_1.36.0 farver_2.1.2
## [129] jquerylib_0.1.4 zoo_1.9-1
## [131] jsonlite_2.0.0 BiocSingular_1.30.0
## [133] RCurl_1.98-1.20 magrittr_2.0.5
## [135] Rcpp_1.1.2 viridis_0.6.5
## [137] EnrichmentBrowser_2.44.0 stringi_1.8.9
## [139] MASS_7.3-66 plyr_1.8.9
## [141] parallel_4.6.1 ggrepel_0.9.8
## [143] Biostrings_2.82.0 splines_4.6.1
## [145] hms_1.1.4 circlize_0.4.18
## [147] RTMB_2.0 reshape2_1.4.5
## [149] ScaledMatrix_1.22.0 BiocVersion_3.24.0
## [151] XML_3.99-0.25 evaluate_1.0.5
## [153] RcppParallel_6.2.1 BiocManager_1.30.27
## [155] nloptr_2.2.1 foreach_1.5.2
## [157] tidyr_1.3.2 purrr_1.2.2
## [159] clue_0.3-68 scattermore_1.2
## [161] ashr_2.2-63 BiocBaseUtils_1.16.0
## [163] rsvd_2.0.0 broom_1.0.13
## [165] xtable_1.8-8 fANCOVA_0.6-1
## [167] viridisLite_0.4.3 truncnorm_1.0-9
## [169] tibble_3.3.1 lmerTest_3.2-1
## [171] glmmTMB_1.1.15.2 memoise_2.0.1
## [173] beeswarm_0.4.0 AnnotationDbi_1.76.0
## [175] cluster_2.1.8.3 GSEABase_1.76.0