Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

collapse_by_group

Collapse raw data by random effect groups


Description

This function extracts the raw data points (i.e. the data that was used to fit the model) and "averages" (i.e. "collapses") the response variable over the levels of the grouping factor given in collapse.by. Only works with mixed models.

Usage

collapse_by_group(grid, model, collapse.by = NULL, residuals = FALSE)

Arguments

grid

A data frame representing the data grid, or an object of class ggeffects, as returned by ggpredict() and others.

model

The model for which to compute partial residuals. The data grid grid should match to predictors in the model.

collapse.by

Name of the (random effects) grouping factor. Data is collapsed by the levels of this factor.

residuals

Logical, if TRUE, collapsed partial residuals instead of raw data by the levels of the grouping factor.

Value

A data frame with raw data points, averaged over the levels of the given grouping factor from the random effects. The group level of the random effect is saved in the column "random".

Examples

library(ggeffects)
if (require("lme4", quietly = TRUE)) {
  data(efc)
  efc$e15relat <- as.factor(efc$e15relat)
  efc$c161sex <- as.factor(efc$c161sex)
  levels(efc$c161sex) <- c("male", "female")
  model <- lmer(neg_c_7 ~ c161sex + (1 | e15relat), data = efc)
  me <- ggpredict(model, terms = "c161sex")
  head(attributes(me)$rawdata)
  collapse_by_group(me, model, "e15relat")
}

ggeffects

Create Tidy Data Frames of Marginal Effects for 'ggplot' from Model Outputs

v1.1.0
GPL-3
Authors
Daniel Lüdecke [aut, cre] (<https://orcid.org/0000-0002-8895-3206>), Frederik Aust [ctb] (<https://orcid.org/0000-0003-4900-788X>), Sam Crawley [ctb] (<https://orcid.org/0000-0002-7847-0411>), Mattan S. Ben-Shachar [ctb] (<https://orcid.org/0000-0002-4287-4801>)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.