library(knitr)
options(knitr.kable.NA = "")
options(digits = 2)

knitr::opts_chunk$set(
  echo = TRUE,
  collapse = TRUE,
  warning = FALSE,
  message = FALSE,
  comment = "#>",
  out.width = "100%",
  tidy.opts = list(width.cutoff = 120)
)

pkgs <- c(
  "effectsize", "BayesFactor", "lme4", "metafor", "lavaan", "nFactors", "BH",
  "EGAnet", "brms", "psych", "rstanarm", "glmmTMB", "GLMMadaptive", "FactoMineR"
)

successfully_loaded <- vapply(pkgs, requireNamespace, TRUE, quietly = TRUE)

if (all(successfully_loaded)) {
  library(parameters)
  library(effectsize)
  library(psych)
  library(brms)
  library(GLMMadaptive)
}

set.seed(333)

The model_parameters() function (also accessible via the shortcut parameters()) allows you to extract the parameters and their characteristics from various models in a consistent way. It can be considered as a lightweight alternative to broom::tidy(), with some notable differences:

Correlations and t-tests

Frequentist

library(parameters)
cor.test(iris$Sepal.Length, iris$Sepal.Width) |>
  parameters()
t.test(mpg ~ vs, data = mtcars) |>
  parameters()

Bayesian

BayesFactor::correlationBF(iris$Sepal.Length, iris$Sepal.Width) |>
  parameters()
BayesFactor::ttestBF(formula = mpg ~ vs, data = mtcars) |>
  parameters()

ANOVAs

Indices of effect size for ANOVAs, such as partial and non-partial versions of eta_squared(), epsilon_sqared() or omega_squared() are powered by the effectsize-package. However, parameters uses these function to compute such indices for parameters summaries, including confidence intervals

Simple

aov(Sepal.Length ~ Species, data = iris) |>
  parameters(effectsize_type = c("omega", "eta", "epsilon"))

Let's complicate things further with an interaction term:

aov(Sepal.Length ~ Species * Sepal.Width, data = iris) |>
  parameters(
    effectsize_type = c("omega", "eta"),
    ci = 0.8
  )

Repeated measures

parameters() (resp. its alias model_parameters()) also works on repeated measures ANOVAs, whether computed from aov() or from a mixed model.

aov(mpg ~ am + Error(gear), data = mtcars) |>
  parameters()

Regressions (GLMs, Mixed Models, GAMs, ...)

parameters() (resp. its alias model_parameters()) was mainly built with regression models in mind. It works for many types of models and packages, including mixed models and Bayesian models.

GLMs

glm(vs ~ poly(mpg, 2) + cyl, data = mtcars, family = binomial()) |>
  parameters()
# show Odds Ratios and Wald-method for degrees of freedom
glm(vs ~ poly(mpg, 2) + cyl, data = mtcars, family = binomial()) |>
  parameters(exponentiate = TRUE, ci_method = "wald")
# show Odds Ratios and include model summary
glm(vs ~ poly(mpg, 2) + cyl, data = mtcars, family = binomial()) |>
  parameters(exponentiate = TRUE, summary = TRUE)

Mixed Models

library(lme4)

lmer(Sepal.Width ~ Petal.Length + (1 | Species), data = iris) |>
  parameters()

Mixed Models, without Random Effects Variances

lmer(Sepal.Width ~ Petal.Length + (1 | Species), data = iris) |>
  parameters(effects = "fixed")

Mixed Model with Zero-Inflation Model

library(GLMMadaptive)
library(glmmTMB)
data("Salamanders")
model <- mixed_model(
  count ~ spp + mined,
  random = ~ 1 | site,
  zi_fixed = ~ spp + mined,
  family = zi.negative.binomial(),
  data = Salamanders
)
parameters(model)

Mixed Models with Dispersion Model

library(glmmTMB)

sim1 <- function(nfac = 40, nt = 100, facsd = 0.1, tsd = 0.15, mu = 0, residsd = 1) {
  dat <- expand.grid(fac = factor(letters[1:nfac]), t = 1:nt)
  n <- nrow(dat)
  dat$REfac <- rnorm(nfac, sd = facsd)[dat$fac]
  dat$REt <- rnorm(nt, sd = tsd)[dat$t]
  dat$x <- rnorm(n, mean = mu, sd = residsd) + dat$REfac + dat$REt
  dat
}

set.seed(101)
d1 <- sim1(mu = 100, residsd = 10)
d2 <- sim1(mu = 200, residsd = 5)
d1$sd <- "ten"
d2$sd <- "five"
dat <- rbind(d1, d2)
model <- glmmTMB(x ~ sd + (1 | t), dispformula = ~sd, data = dat)

parameters(model)

Bayesian Models

model_parameters() also works with Bayesian models from the rstanarm package:

library(rstanarm)

# if you are unfamiliar with the `refresh` argument here, it just avoids
# printing few messages to the console
stan_glm(mpg ~ wt * cyl, data = mtcars, refresh = 0) |>
  parameters()

Additionally, it also works for models from the brms package.

For more complex models, specific model components can be printed using the arguments effects and component arguments.

library(brms)
data(fish)
set.seed(123)

# fitting a model using `brms`
model <- suppressWarnings(brm(
  bf(
    count ~ persons + child + camper + (1 | persons),
    zi ~ child + camper + (1 | persons)
  ),
  data = fish,
  family = zero_inflated_poisson(),
  refresh = 0
))

parameters(model, component = "conditional", verbose = FALSE)

parameters(model, effects = "all", component = "all", verbose = FALSE)

To include information about the random effect parameters (group levels), set group_level = TRUE:

parameters(
  model,
  effects = "all",
  component = "conditional",
  group_level = TRUE,
  verbose = FALSE
)

Structural Models (PCA, EFA, CFA, SEM...)

The parameters package extends the support to structural models.

Principal Component Analysis (PCA) and Exploratory Factor Analysis (EFA)

psych::pca(mtcars, nfactors = 3) |>
  parameters()

We will avoid displaying a graph while carrying out factor analysis:

FactoMineR::FAMD(iris, ncp = 3, graph = FALSE) |>
  parameters()

Confirmatory Factor Analysis (CFA) and Structural Equation Models (SEM)

Frequentist

library(lavaan)

model <- lavaan::cfa(" visual  =~ x1 + x2 + x3
                       textual =~ x4 + x5 + x6
                       speed   =~ x7 + x8 + x9 ",
  data = HolzingerSwineford1939
)

model_parameters(model)

Bayesian

blavaan to be done.

Meta-Analysis

parameters() also works for rma-objects from the metafor package.

library(metafor)

mydat <<- data.frame(
  effectsize = c(-0.393, 0.675, 0.282, -1.398),
  standarderror = c(0.317, 0.317, 0.13, 0.36)
)

rma(yi = effectsize, sei = standarderror, method = "REML", data = mydat) |>
  model_parameters()

Plotting Model Parameters

There is a plot()-method implemented in the see-package. Several examples are shown in this vignette.



easystats/parameters documentation built on April 12, 2024, 9:33 a.m.