get_modeldata: Get or set the dataset used to fit a model

View source: R/get_modeldata.R

get_modeldataR Documentation

Get or set the dataset used to fit a model

Description

get_modeldata() retrieves the data used to fit a model. If the data was previously attached via set_modeldata(), it is returned directly. Otherwise, the data is retrieved from the model object or the environment using insight::get_data().

Usage

get_modeldata(model, ...)

set_modeldata(model, newdata)

Arguments

model

Model object

...

Additional arguments are passed to the predict() method supplied by the modeling package.These arguments are particularly useful for mixed-effects or bayesian models (see the online vignettes on the marginaleffects website). Available arguments can vary from model to model, depending on the range of supported arguments by each modeling package. See the "Model-Specific Arguments" section of the ?slopes documentation for a non-exhaustive list of available arguments.

newdata

A data frame to attach to the model object.

Details

set_modeldata() attaches a dataset to a model object as an attribute. This is useful when the training data may not be available in the environment where marginaleffects functions are called, such as inside lapply(), in Shiny apps, or in nested function calls. By attaching the data explicitly, you ensure that marginaleffects always uses the correct dataset.

Value

get_modeldata() returns a data frame of the original data used to fit the model, or NULL if not available. set_modeldata() returns the model with the data attached as an attribute.


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