Skip to contents

FitLmer - This is a simpler version of FitLmerContrast that makes no assumptions about structure of underlying data except that it can accomodate a mixed effects model formula and that there are multiple cell types to be separately fitted. fit a single cell mixed effects linear model. Designed to fit an aggregated gene module score. Fits a model to each cell cluster to test a grouping, treatment or combination factor; returns model fits for maximum flexibility

Usage

FitLmer(
  module_data_frame,
  celltype_column = "celltype",
  metadata,
  lme4.formula = NULL
)

Arguments

module_data_frame

data for each cell to model -- designed to be scores for modules (columns) returned by scglmmr::WeightedModuleScore

celltype_column

the column in metadata with the cluster / celltype designation for each cell

lme4metadata

metadata for model fit

f1

model furmula

Value

Examples

if (FALSE) {
s = SubsetData(s, ident.use = tc)
s = NormalizeData(s,normalization.method = 'LogNormalize',assay = 'RNA')


# format metadata as factors group_id is order leveled for:
# contrast_fit = contrast(emm1, method = list( (c21 - c20) - (c11 - c10) ))
md = s@meta.data %>%
  filter(timepoint %in% c(0,1)) %>%
  mutate(group_id = paste(group, timepoint, sep = '_')) %>%
  mutate(group_id = factor(group_id,  levels = c('0_0', '0_1', '1_0', '1_1'))) %>%
  mutate(subjectid = factor(sampleid)) %>%
  select(celltype, subjectid, age, group_id) %>%
  mutate(age = as.numeric(age)) %>%
  droplevels()

# qc data to remove celltypes with no cells for some subhects at both timepoints
# keeps MLE more stable for the estimates of random intercept
ct.si = apply(table(md$celltype, md$subjectid) , 1, min)
c.keep = names(ct.si[ct.si > 7])
md = md[md$celltype %in% c.keep, ]


# add single cell weighted module scores
# split to standardize within cell type
ct.md = split(md, f = md$celltype)
mod_scores = lapply(ct.md, function(x){
  scglmmr::WeightedCellModuleScore(gene_matrix = s@assays$RNA@data[ ,rownames(x)],
                                   module_list = mtor.sigs,
                                   threshold = 0,
                                   # standardize within protein celltype
                                   cellwise_scaling = TRUE,
                                   return_weighted = FALSE )
})
ms = bind_rows(mod_scores)

# correctly order rows after the split.
ms = ms[match(x = rownames(md), table = rownames(ms)), ]
stopifnot(all.equal(rownames(ms), rownames(md)))


# specify model
f1 = 'modulescore ~ group_id + age + (1|subjectid)'

# fit sc mod mixed model on ewighted module scores.
mm_res.m1 = scglmmr::FitLmerContrast(module_data_frame = ms,
                                     celltype_column = 'celltype',
                                     metadata = md,
                                     lmer_formula = f1,
                                     plotdatqc = TRUE,
                                     fixed_effects = NULL,
                                     figpath = plot_savepath)
}