View source: R/estimate_grouplevel.R
estimate_grouplevel | R Documentation |
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.
estimate_grouplevel(model, type = "random", ...)
reshape_grouplevel(x, indices = "all", group = "all", ...)
model |
A mixed model with random effects. |
type |
If |
... |
Other arguments passed to or from other methods. |
x |
The output of |
indices |
A list containing the indices to extract (e.g., "Coefficient"). |
group |
A list containing the random factors to select. |
# lme4 model
if (require("lme4") && require("see")) {
model <- lmer(mpg ~ hp + (1 | carb), data = mtcars)
random <- estimate_grouplevel(model)
random
# Visualize random effects
plot(random)
# Show group-specific effects
estimate_grouplevel(model, deviation = FALSE)
# 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)
# Compute BLUPs
estimate_grouplevel(model, type = "total")
}
# Bayesian models
if (require("rstanarm")) {
model <- rstanarm::stan_lmer(mpg ~ hp + (1 | carb) + (1 | gear), data = mtcars, refresh = 0)
# Broken estimate_grouplevel(model)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.