`demean()`

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

is more generic in terms of the centering-operation. While
`demean()`

always uses mean-centering, `degroup()`

can also use the mode or
median for centering.

```
demean(
x,
select,
group,
suffix_demean = "_within",
suffix_groupmean = "_between",
add_attributes = TRUE,
verbose = TRUE
)
degroup(
x,
select,
group,
center = "mean",
suffix_demean = "_within",
suffix_groupmean = "_between",
add_attributes = TRUE,
verbose = TRUE
)
detrend(
x,
select,
group,
center = "mean",
suffix_demean = "_within",
suffix_groupmean = "_between",
add_attributes = TRUE,
verbose = TRUE
)
```

x | A data frame. |
---|---|

select | Character vector (or formula) with names of variables to select that should be group- and de-meaned. |

group | Character vector (or formula) with the name of the variable that indicates the group- or cluster-ID. |

suffix_demean, suffix_groupmean | String value, will be appended to the
names of the group-meaned and de-meaned variables of |

add_attributes | Logical, if |

verbose | Toggle warnings and messages. |

center | Method for centering. |

A data frame with the group-/de-meaned variables, which get the suffix
`"_between"`

(for the group-meaned variable) and `"_within"`

(for
the de-meaned variable) by default.

Mixed models include different levels of sources of variability, i.e.
error terms at each level. When macro-indicators (or level-2 predictors,
or higher-level units, or more general: *group-level predictors that
vary within and across groups*) are included as fixed effects (i.e.
treated as covariate at level-1), the variance that is left unaccounted for
this covariate will be absorbed into the error terms of level-1 and level-2
(Bafumi and Gelman 2006; Gelman and Hill 2007, Chapter 12.6.):
“Such covariates contain two parts: one that is specific to the
higher-level entity that does not vary between occasions, and one that
represents the difference between occasions, within higher-level entities”
(Bell et al. 2015). Hence, the error terms will be correlated with
the covariate, which violates one of the assumptions of mixed models
(iid, independent and identically distributed error terms). This bias is
also called the

`demean()`

is intended to create group- and de-meaned variables
for panel regression models (fixed effects models), or for complex
random-effect-within-between models (see Bell et al. 2015, 2018),
where group-effects (random effects) and fixed effects correlate (see
Bafumi and Gelman 2006). This can happen, for instance, when
analyzing panel data, which can lead to *Heterogeneity Bias*. To
control for correlating predictors and group effects, it is recommended
to include the group-meaned and de-meaned version of *time-varying covariates*
(and group-meaned version of *time-invariant covariates* that are on
a higher level, e.g. level-2 predictors) in the model. By this, one can
fit complex multilevel models for panel data, including time-varying
predictors, time-invariant predictors and random effects.

A mixed models approach can model the causes of endogeneity explicitly by including the (separated) within- and between-effects of time-varying fixed effects and including time-constant fixed effects. Furthermore, mixed models also include random effects, thus a mixed models approach is superior to classic fixed-effects models, which lack information of variation in the group-effects or between-subject effects. Furthermore, fixed effects regression cannot include random slopes, which means that fixed effects regressions are neglecting “cross-cluster differences in the effects of lower-level controls (which) reduces the precision of estimated context effects, resulting in unnecessarily wide confidence intervals and low statistical power” (Heisig et al. 2017).

The group-meaned variable is simply the mean of an independent variable
within each group (or id-level or cluster) represented by `group`

.
It represents the cluster-mean of an independent variable. The regression
coefficient of a group-meaned variable is the *between-subject-effect*.
The de-meaned variable is then the centered version of the group-meaned
variable. De-meaning is sometimes also called person-mean centering or
centering within clusters. The regression coefficient of a de-meaned
variable represents the *within-subject-effect*.

For continuous time-varying predictors, the recommendation is to include both their de-meaned and group-meaned versions as fixed effects, but not the raw (untransformed) time-varying predictors themselves. The de-meaned predictor should also be included as random effect (random slope). In regression models, the coefficient of the de-meaned predictors indicates the within-subject effect, while the coefficient of the group-meaned predictor indicates the between-subject effect.

For binary time-varying predictors, there are two recommendations. First
is to include the raw (untransformed) binary predictor as fixed effect
only and the *de-meaned* variable as random effect (random slope).
The alternative would be to add the de-meaned version(s) of binary
time-varying covariates as additional fixed effect as well (instead of
adding it as random slope). Centering time-varying binary variables to
obtain within-effects (level 1) isn't necessary. They have a sensible
interpretation when left in the typical 0/1 format (Hoffmann 2015,
chapter 8-2.I). `demean()`

will thus coerce categorical time-varying
predictors to numeric to compute the de- and group-meaned versions for
these variables, where the raw (untransformed) binary predictor and the
de-meaned version should be added to the model.

Factors with more than two levels are demeaned in two ways: first, these are also converted to numeric and de-meaned; second, dummy variables are created (binary, with 0/1 coding for each level) and these binary dummy-variables are de-meaned in the same way (as described above). Packages like panelr internally convert factors to dummies before demeaning, so this behaviour can be mimicked here.

There are multiple ways to deal
with interaction terms of within- and between-effects. A classical approach
is to simply use the product term of the de-meaned variables (i.e.
introducing the de-meaned variables as interaction term in the model
formula, e.g. `y ~ x_within * time_within`

). This approach, however,
might be subject to bias (see Giesselmann & Schmidt-Catran 2020).

Another option is to first calculate the product term and then apply the
de-meaning to it. This approach produces an estimator “that reflects
unit-level differences of interacted variables whose moderators vary
within units”, which is desirable if *no* within interaction of
two time-dependent variables is required.

A third option, when the interaction should result in a genuine within
estimator, is to "double de-mean" the interaction terms
(Giesselmann & Schmidt-Catran 2018), however, this is currently
not supported by `demean()`

. If this is required, the `wmb()`

function from the panelr package should be used.

To de-mean interaction terms for within-between models, simply specify
the term as interaction for the `select`

-argument, e.g.
`select = "a*b"`

(see 'Examples').

A description of how to translate the
formulas described in *Bell et al. 2018* into R using `lmer()`

from lme4 can be found in
this vignette.

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. 2019. Fixed and Random Effects Models: Making an Informed Choice. Quality & Quantity (53); 1051-1074

Bell A, Jones K. 2015. Explaining Fixed Effects: Random Effects Modeling of Time-Series Cross-Sectional and Panel Data. Political Science Research and Methods, 3(1), 133–153.

Gelman A, Hill J. 2007. Data Analysis Using Regression and Multilevel/Hierarchical Models. Analytical Methods for Social Research. Cambridge, New York: Cambridge University Press

Giesselmann M, Schmidt-Catran, AW. 2020. Interactions in fixed effects regression models. Sociological Methods & Research, 1–28. https://doi.org/10.1177/0049124120914934

Heisig JP, Schaeffer M, Giesecke J. 2017. The Costs of Simplicity: Why Multilevel Models May Benefit from Accounting for Cross-Cluster Differences in the Effects of Controls. American Sociological Review 82 (4): 796–827.

Hoffman L. 2015. Longitudinal analysis: modeling within-person fluctuation and change. New York: Routledge

If grand-mean centering (instead of centering within-clusters)
is required, see `center()`

.

```
data(iris)
iris$ID <- sample(1:4, nrow(iris), replace = TRUE) # fake-ID
iris$binary <- as.factor(rbinom(150, 1, .35)) # binary variable
x <- demean(iris, select = c("Sepal.Length", "Petal.Length"), group = "ID")
head(x)
#> Sepal.Length_between Petal.Length_between Sepal.Length_within
#> 1 5.746154 3.492308 -0.6461538
#> 2 6.115625 4.387500 -1.2156250
#> 3 6.115625 4.387500 -1.4156250
#> 4 5.685000 3.322500 -1.0850000
#> 5 5.685000 3.322500 -0.6850000
#> 6 5.879487 3.953846 -0.4794872
#> Petal.Length_within
#> 1 -2.092308
#> 2 -2.987500
#> 3 -3.087500
#> 4 -1.822500
#> 5 -1.922500
#> 6 -2.253846
x <- demean(iris, select = c("Sepal.Length", "binary", "Species"), group = "ID")
#> Categorical predictors (Species, binary) have been coerced to numeric values to compute de- and group-meaned variables.
#>
head(x)
#> Sepal.Length_between Species_between binary_between Species_setosa_between
#> 1 5.746154 0.8974359 0.2051282 0.3846154
#> 2 6.115625 1.2500000 0.3437500 0.1875000
#> 3 6.115625 1.2500000 0.3437500 0.1875000
#> 4 5.685000 0.8250000 0.4000000 0.5000000
#> 5 5.685000 0.8250000 0.4000000 0.5000000
#> 6 5.879487 1.0769231 0.3846154 0.2307692
#> Species_versicolor_between Species_virginica_between Sepal.Length_within
#> 1 0.3333333 0.2820513 -0.6461538
#> 2 0.3750000 0.4375000 -1.2156250
#> 3 0.3750000 0.4375000 -1.4156250
#> 4 0.1750000 0.3250000 -1.0850000
#> 5 0.1750000 0.3250000 -0.6850000
#> 6 0.4615385 0.3076923 -0.4794872
#> Species_within binary_within Species_setosa_within Species_versicolor_within
#> 1 -0.8974359 -0.2051282 0.6153846 -0.3333333
#> 2 -1.2500000 -0.3437500 0.8125000 -0.3750000
#> 3 -1.2500000 0.6562500 0.8125000 -0.3750000
#> 4 -0.8250000 0.6000000 0.5000000 -0.1750000
#> 5 -0.8250000 -0.4000000 0.5000000 -0.1750000
#> 6 -1.0769231 0.6153846 0.7692308 -0.4615385
#> Species_virginica_within
#> 1 -0.2820513
#> 2 -0.4375000
#> 3 -0.4375000
#> 4 -0.3250000
#> 5 -0.3250000
#> 6 -0.3076923
# demean interaction term x*y
dat <- data.frame(
a = c(1, 2, 3, 4, 1, 2, 3, 4),
x = c(4, 3, 3, 4, 1, 2, 1, 2),
y = c(1, 2, 1, 2, 4, 3, 2, 1),
ID = c(1, 2, 3, 1, 2, 3, 1, 2)
)
demean(dat, select = c("a", "x*y"), group = "ID")
#> a_between x_y_between a_within x_y_within
#> 1 2.666667 4.666667 -1.6666667 -0.6666667
#> 2 2.333333 4.000000 -0.3333333 2.0000000
#> 3 2.500000 4.500000 0.5000000 -1.5000000
#> 4 2.666667 4.666667 1.3333333 3.3333333
#> 5 2.333333 4.000000 -1.3333333 0.0000000
#> 6 2.500000 4.500000 -0.5000000 1.5000000
#> 7 2.666667 4.666667 0.3333333 -2.6666667
#> 8 2.333333 4.000000 1.6666667 -2.0000000
# or in formula-notation
demean(dat, select = ~ a + x * y, group = ~ID)
#> a_between x_y_between a_within x_y_within
#> 1 2.666667 4.666667 -1.6666667 -0.6666667
#> 2 2.333333 4.000000 -0.3333333 2.0000000
#> 3 2.500000 4.500000 0.5000000 -1.5000000
#> 4 2.666667 4.666667 1.3333333 3.3333333
#> 5 2.333333 4.000000 -1.3333333 0.0000000
#> 6 2.500000 4.500000 -0.5000000 1.5000000
#> 7 2.666667 4.666667 0.3333333 -2.6666667
#> 8 2.333333 4.000000 1.6666667 -2.0000000
```