de_mean: Compute group-meaned and de-meaned variables

View source: R/de_mean.R

de_meanR Documentation

Compute group-meaned and de-meaned variables

Description

de_mean() computes group- and de-meaned versions of a variable that can be used in regression analysis to model the between- and within-subject effect.

Usage

de_mean(x, ..., grp, append = TRUE, suffix.dm = "_dm", suffix.gm = "_gm")

Arguments

x

A data frame.

...

Names of variables that should be group- and de-meaned.

grp

Quoted or unquoted name of the variable that indicates the group- or cluster-ID.

append

Logical, if TRUE (the default) and x is a data frame, x including the new variables as additional columns is returned; if FALSE, only the new variables are returned.

suffix.dm, suffix.gm

String value, will be appended to the names of the group-meaned and de-meaned variables of x. By default, de-meaned variables will be suffixed with "_dm" and grouped-meaned variables with "_gm".

Details

de_mean() is intended to create group- and de-meaned variables for complex random-effect-within-between models (see Bell et al. 2018), where group-effects (random effects) and fixed effects correlate (see Bafumi and Gelman 2006)). This violation of one of the Gauss-Markov-assumptions can happen, for instance, when analysing panel data. To control for correlating predictors and group effects, it is recommended to include the group-meaned and de-meaned version of time-varying covariates in the model. By this, one can fit complex multilevel models for panel data, including time-varying, time-invariant predictors and random effects. This approach is superior to simple fixed-effects models, which lack information of variation in the group-effects or between-subject effects.

A description of how to translate the formulas described in Bell et al. 2018 into R using lmer() from lme4 or glmmTMB() from glmmTMB can be found here: for lmer() and for glmmTMB().

Value

For append = TRUE, x including the group-/de-meaned variables as new columns is returned; if append = FALSE, only the group-/de-meaned variables will be returned.

References

Bafumi J, Gelman A. 2006. Fitting Multilevel Models When Predictors and Group Effects Correlate. In. Philadelphia, PA: Annual meeting of the American Political Science Association.

Bell A, Fairbrother M, Jones K. 2018. Fixed and Random Effects Models: Making an Informed Choice. Quality & Quantity. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1007/s11135-018-0802-x")}

Examples

data(efc)
efc$ID <- sample(1:4, nrow(efc), replace = TRUE) # fake-ID
de_mean(efc, c12hour, barthtot, grp = ID, append = FALSE)

sjPlot/sjmisc documentation built on June 28, 2023, 2:39 p.m.