Skip to contents

Extract random parameters of each individual group in the context of mixed models. Can be reshaped to be of the same dimensions as the original data, which can be useful to add the random effects to the original data.

Usage

estimate_grouplevel(model, type = "random", ...)

reshape_grouplevel(x, indices = "all", group = "all", ...)

Arguments

model

A mixed model with random effects.

type

If "random" (default), the coefficients are the ones estimated natively by the model (as they are returned by, for instance, lme4::ranef()). They correspond to the deviation of each individual group from their fixed effect. As such, a coefficient close to 0 means that the participants' effect is the same as the population-level effect (in other words, it is "in the norm"). If "total", it will return the sum of the random effect and its corresponding fixed effects. These are known as BLUPs (Best Linear Unbiased Predictions). This argument can be used to reproduce the results given by lme4::ranef() and coef() (see ?coef.merMod). Note that BLUPs currently don't have uncertainty indices (such as SE and CI), as these are not computable.

...

Other arguments passed to or from other methods.

x

The output of estimate_grouplevel().

indices

A list containing the indices to extract (e.g., "Coefficient").

group

A list containing the random factors to select.

Examples

# lme4 model
data(mtcars)
model <- lme4::lmer(mpg ~ hp + (1 | carb), data = mtcars)
random <- estimate_grouplevel(model)
random
#> Group | Level |   Parameter | Coefficient |   SE |        95% CI
#> ----------------------------------------------------------------
#> carb  |     1 | (Intercept) |        0.41 | 0.84 | [-1.24, 2.05]
#> carb  |     2 | (Intercept) |        0.11 | 0.78 | [-1.42, 1.65]
#> carb  |     3 | (Intercept) |       -0.32 | 0.94 | [-2.16, 1.51]
#> carb  |     4 | (Intercept) |       -0.78 | 0.78 | [-2.31, 0.75]
#> carb  |     6 | (Intercept) |        0.09 | 1.00 | [-1.87, 2.05]
#> carb  |     8 | (Intercept) |        0.50 | 1.00 | [-1.47, 2.46]

# Visualize random effects
plot(random)


# Show group-specific effects
estimate_grouplevel(model, deviation = FALSE)
#> Group | Level |   Parameter | Coefficient |   SE |        95% CI
#> ----------------------------------------------------------------
#> carb  |     1 | (Intercept) |        0.41 | 0.84 | [-1.24, 2.05]
#> carb  |     2 | (Intercept) |        0.11 | 0.78 | [-1.42, 1.65]
#> carb  |     3 | (Intercept) |       -0.32 | 0.94 | [-2.16, 1.51]
#> carb  |     4 | (Intercept) |       -0.78 | 0.78 | [-2.31, 0.75]
#> carb  |     6 | (Intercept) |        0.09 | 1.00 | [-1.87, 2.05]
#> carb  |     8 | (Intercept) |        0.50 | 1.00 | [-1.47, 2.46]

# Reshape to wide data so that it matches the original dataframe...
reshaped <- reshape_grouplevel(random, indices = c("Coefficient", "SE"))

# ... and can be easily combined
alldata <- cbind(mtcars, reshaped)

# Use summary() to remove duplicated rows
summary(reshaped)
#>   carb carb_Coefficient_Intercept carb_SE_Intercept
#> 1    4                -0.77955416         0.7829275
#> 2    1                 0.40506064         0.8390523
#> 3    2                 0.11088289         0.7829275
#> 4    3                -0.32486359         0.9369268
#> 5    6                 0.09296535         1.0007322
#> 6    8                 0.49550886         1.0007322

# Compute BLUPs
estimate_grouplevel(model, type = "total")
#> Group | Level |   Parameter | Coefficient
#> -----------------------------------------
#> carb  |     1 | (Intercept) |       30.18
#> carb  |     2 | (Intercept) |       29.88
#> carb  |     3 | (Intercept) |       29.45
#> carb  |     4 | (Intercept) |       28.99
#> carb  |     6 | (Intercept) |       29.87
#> carb  |     8 | (Intercept) |       30.27