sccomp_calculate_residuals computes the residuals between observed cell group proportions and the predicted proportions from a fitted sccomp model. This function is useful for assessing model fit and identifying cell groups or samples where the model may not adequately capture the observed data. The residuals are calculated as the difference between the observed proportions and the predicted mean proportions from the model.

sccomp_calculate_residuals(.data)

Arguments

.data

A tibble of class sccomp_tbl, which is the result of sccomp_estimate(). This tibble contains the fitted model and associated data necessary for calculating residuals.

Value

A tibble (tbl) with the following columns:

  • sample - A character column representing the sample identifiers.

  • cell_group - A character column representing the cell group identifiers.

  • residuals - A numeric column representing the residuals, calculated as the difference between observed and predicted proportions.

  • exposure - A numeric column representing the total counts (sum of counts across cell groups) for each sample.

Details

The function performs the following steps:

  1. Extracts the predicted mean proportions for each cell group and sample using sccomp_predict().

  2. Calculates the observed proportions from the original count data.

  3. Computes residuals by subtracting the predicted proportions from the observed proportions.

  4. Returns a tibble containing the sample, cell group, residuals, and exposure (total counts per sample).

Examples

# \donttest{
  if (instantiate::stan_cmdstan_exists() && .Platform$OS.type == "unix") {
# Load example data
data("counts_obj")

# Fit the sccomp model
estimates <- sccomp_estimate(
  counts_obj,
  formula_composition = ~ type,
  formula_variability = ~1,
  .sample = sample,
  .cell_group = cell_group,
  .count = count,
  approximate_posterior_inference = "all",
  cores = 1
)

# Calculate residuals
residuals <- sccomp_calculate_residuals(estimates)

# View the residuals
print(residuals)
}# }