emmeans R Package

repository·main·Indexed 19 days ago

https://github.com/rvlenth/emmeans

An R package for calculating Estimated Marginal Means (EMMs), also known as least-squares means, and performing post hoc comparisons and contrasts from various statistical models. It supports standard models (lm, glm), complex models (mixed models, GEEs, survival, zero-inflated), and Bayesian models (carBayes, MCMCglmm, MCMCpack). Key features include estimability checks for rank-deficient models, back-transformations via the delta method, and trend calculations using emtrends.

Tokens
3.7K
Snippets
6
Records
20
Agent score
65%

What's inside emmeans

  1. Understand the `emmGrid` object structure via `ref_grid`

    main

    The ref_grid() function is the foundation of the emmeans package. It extracts information from a model to create an emmGrid object. An emmGrid contains the following key slots:

    • bhat ($b$): The fixed-effects coefficients.
    • V ($V$): The variance-covariance matrix of $b$.
    • linfct ($L$): The linear functions of $b$ (the linear predictor for the points in the reference grid is $Lb$).
    • grid: A data frame containing factor/covariate combinations for each row of $L$. It may include .wgt. (weights) and .offset. columns.
    • levels: A named list of factor levels.
    • dffun and dfargs: A function and arguments used to compute degrees of freedom for a linear prediction $x'b$.
    • nbasis ($N$): A matrix used to assess the estimability of $x'b$. If $N'x \neq 0$, the prediction is not uniquely estimable.
    • post.beta: For Bayesian models, a posterior sample of $b$ instances.
    • model.info, roles, matlevs, and misc: Metadata and extra information used by package functions.
  2. Handle Bayesian models and `summary.hpd`

    main

    For Bayesian models where the post.beta slot is populated with posterior samples, functions like emmeans, contrast, emtrends, and regrid apply their transformations to every row in the posterior sample.

    When summary() is called on such an object, it automatically uses summary.hpd (Highest Posterior Density) instead of frequentist methods, provided frequentist = FALSE is set (or the default behavior is used). This allows for direct summarization of back-transformed posterior samples without needing the delta method.

  3. Core concepts of emmeans

    main

    The emmeans package calculates Estimated Marginal Means (EMMs), also known as least-squares means.

    How it works:

    1. Reference Grid: The package uses a fitted model to make predictions over a regular grid of predictor combinations.
    2. Averaging: These predictions are typically averaged (often with equal weights) over one or more predictors to describe the effects of factors.
    3. Comparisons: The package provides tools for estimating and testing pairwise comparisons of EMMs and other types of contrasts.
    4. Trends: For models with continuous predictors interacting with factors, the emtrends function calculates predicted slopes of trend lines for each factor combination.

    Key Features:

    • Estimability Checks: In rank-deficient models, the package verifies if predictions are uniquely defined to avoid invalid results.
    • Transformations: Many functions support a type argument (e.g., type = "response") to back-transform results to the original response scale. The regrid() function can also be used to reconstruct objects on a specific transformed scale.
    • Graphics: Provides support for interaction-style plots and side-by-side intervals for emmGrid objects.
  4. How the Satterthwaite method estimates degrees of freedom

    main

    The Satterthwaite method is used to estimate degrees of freedom ($\nu$) by assuming a variance estimator $W$ is proportional to a $\chi^2$ random variable. The degrees of freedom are estimated using the formula $\hat\nu = 2W^2 / \hat{\text{var}}(W)$.

    In emmeans, the implementation depends on the model type:

    • For nlme::gls objects: The package calculates the Jacobian of the variance matrix of the random effects using the gls_grad function.
    • For lme4::mermod and nlme::lme objects: Since the variance matrix is not directly available, an approximate method ("appx-satterthwaite") is used. This method employs the gradV.kludge function, which perturbs response values slightly and refits the model to estimate the required variances.
  5. Understanding estimability in emmeans predictions

    main

    The emmeans package assesses the estimability of predictions. This is critical when working with rank-deficient model matrices, where multiple solutions for fixed effects exist. A prediction $x'\beta$ is considered estimable if it remains the same regardless of which solution for $\beta$ is used. Mathematically, $x'\beta$ is estimable if and only if $x$ is in the row space of the model matrix $X$.

    To perform this assessment, emmeans uses the estimability package to create a basis $N$ for the null space of $X$ (where $XN=0$). A prediction is estimable if $x'N=0$. The basis matrix $N$ is stored in the @nbasis slot of an emmGrid object.

  6. Submit the emmeans package to CRAN

    main

    Once checks are passed, follow these steps to submit:

    1. Visit the CRAN submissions site.
    2. Enter your name and email, select your tar.gz file, and upload.
    3. Verification: You will receive a confirmation email. You must click the link in that email to verify your identity and confirm you have reviewed the CRAN policies and check results.
    4. After verification, the submission process proceeds. You may receive follow-up emails regarding preliminary checks or potential failures in reverse dependency checks.
    # To release without running checks (if already checked)
    devtools::release(check = FALSE)
  7. Best practices for post hoc analysis

    main

    When performing post hoc analyses with emmeans, avoid blindly piping results into subsequent code blocks (e.g., using %>% or |>).

    Why? Most emmeans functions produce results with critical annotations, such as:

    • Transformations applied
    • P-value adjustment methods
    • Families used for adjustments

    If you pipe results directly into further processing without inspecting them, you lose visibility into these statistical annotations, which can lead to incorrect interpretations.

  8. Install the emmeans package

    main

    You can install the stable version from CRAN or the latest development version from GitHub.

    Install from CRAN

    Visit the CRAN page for the latest stable release.

    Install from GitHub (Development Version)

    To install the latest development version, use the remotes package. Windows users should ensure Rtools is installed first.

    remotes::install_github("rvlenth/emmeans", dependencies = TRUE, build_vignettes = TRUE)

    Note: You can omit build_vignettes = TRUE to speed up installation if you do not need the vignettes immediately.

    remotes::install_github("rvlenth/emmeans", dependencies = TRUE, build_vignettes = TRUE)
  9. Build and check the emmeans tarball

    main

    To prepare the package for CRAN, you need to build a source package (tarball) and run R CMD check to ensure it meets requirements.

    Option 1: Command Line (Manual)

    1. Build the tarball:
    devtools::build(manual = TRUE)
    1. Check the tarball (run in a shell/DOS window):
    R CMD check --as-cran emmeans-xxx.tar.gz

    Note: If you encounter errors, try manually deleting the emmeans.Rcheck/ directory before re-running the check.

    Option 2: devtools (Integrated)

    You can combine building and checking into a single step in an R session:

    devtools::check(manual = TRUE, cran = TRUE)
    # Build and check in one step
    devtools::check(manual = TRUE, cran = TRUE)
  10. Update the vignette index using vigindex

    main

    The emmeans vignettes use HTML comments to compile a simple index. If you add or change vignettes, you must manually update the index by running vigindex::vigindex(). This generates a new vignette named vigindex.Rmd.

    Typical index tags follow this format:

    <!-- @index Transformations; 
                Transformations!Bias correction;
                Examples!`pigs`   -->

    Note: The closing comment tag --> must be on the same line as the last entry. Entries starting with a prefix followed by ! (e.g., Transformations!Bias correction) create sub-entries.

    vigindex::vigindex()
  11. Configure rounding behavior in summary output

    main

    By default, emmeans uses an optimal-digits algorithm that rounds results to a useful number of digits relative to the confidence limits. This prevents cluttered output.

    If you prefer standard R rounding behavior, you can disable this routine using the opt.digits option.

    Note: The values stored in emm_summary objects are the actual unrounded values; only the displayed output is affected by this setting.

    # Example of disabling optimal rounding (conceptually)
    # Use opt.digits = FALSE in relevant functions if available