get_model_matrix: Get a named model matrix

View source: R/get_model_matrix.R

get_model_matrixR Documentation

Get a named model matrix

Description

Get a named model matrix

Usage

get_model_matrix(model, newdata, mfx = NULL)

## Default S3 method:
get_model_matrix(model, newdata, mfx = NULL)

## S3 method for class 'negbin'
get_model_matrix(model, newdata, mfx = NULL)

## S3 method for class 'rlm'
get_model_matrix(model, newdata, mfx = NULL)

## S3 method for class 'brglmFit'
get_model_matrix(model, newdata, mfx = NULL)

## S3 method for class 'geeglm'
get_model_matrix(model, newdata, mfx = NULL)

## S3 method for class 'ivreg'
get_model_matrix(model, newdata, mfx = NULL)

## S3 method for class 'rq'
get_model_matrix(model, newdata, mfx = NULL)

## S3 method for class 'ols'
get_model_matrix(model, newdata, mfx = NULL)

## S3 method for class 'lrm'
get_model_matrix(model, newdata, mfx = NULL)

## S3 method for class 'lm'
get_model_matrix(model, newdata, mfx = NULL)

## S3 method for class 'glm'
get_model_matrix(model, newdata, mfx = NULL)

## S3 method for class 'svyglm'
get_model_matrix(model, newdata, mfx = NULL)

Arguments

model

Model object

newdata

Grid of predictor values at which we evaluate the slopes.

  • Warning: When newdata is NULL, the data is retrieved using get_modeldata(), which may have to extract it from the modeling environment. This can produce unexpected results if the data has been modified, or when called inside lapply(), Shiny apps, or nested functions. To avoid potential issues, consider using set_modeldata() to attach the training data to the model object explicitly.

  • NULL (default): Unit-level slopes for each observed value in the dataset (empirical distribution). The dataset is retrieved using get_modeldata().

  • datagrid() call to specify a custom grid of regressors. For example:

    • newdata = datagrid(cyl = c(4, 6)): cyl variable equal to 4 and 6 and other regressors fixed at their means or modes.

    • See the Examples section and the datagrid() documentation.

  • subset() call with a single argument to select a subset of the dataset used to fit the model, ex: newdata = subset(treatment == 1)

  • dplyr::filter() call with a single argument to select a subset of the dataset used to fit the model, ex: newdata = filter(treatment == 1)

  • string:

    • "mean": Slopes evaluated when each predictor is held at its mean or mode.

    • "median": Slopes evaluated when each predictor is held at its median or mode.

    • "balanced": Slopes evaluated on a balanced grid with every combination of categories and numeric variables held at their means.

    • "tukey": Slopes evaluated at Tukey's 5 numbers.

    • "grid": Slopes evaluated on a grid of representative numbers (Tukey's 5 numbers and unique values of categorical predictors).


marginaleffects documentation built on Sept. 3, 2026, 9:08 a.m.