mid.conditional: Calculate MID Conditional Expectations

View source: R/mid_conditional.R

mid.conditionalR Documentation

Calculate MID Conditional Expectations

Description

mid.conditional() calculates the data required to draw Individual Conditional Expectation (ICE) curves from a fitted MID model. ICE curves visualize how a single observation's prediction changes as a specified variable's value varies, while all other variable are held constant.

Usage

mid.conditional(
  object,
  variable,
  data = NULL,
  n.samples = 100L,
  max.nrow = 100000L,
  type = c("response", "link"),
  keep.effects = TRUE
)

Arguments

object

a "mid" object.

variable

a character string or expression specifying the single predictor variable for which to calculate ICE curves.

data

a data frame containing the observations to be used for the ICE calculations. If not provided, data is automatically extracted based on the function call.

n.samples

the number of sample points for the variable's range.

max.nrow

the maximum number of rows for the output data frames. If the number of evaluation points exceeds this limit, the original data is randomly subsampled.

type

the type of prediction to return. "response" (default) for the original scale or "link" for the scale of the linear predictor.

keep.effects

logical. If TRUE, the effects of individual component functions are stored in the output object.

Details

The function generates a set of hypothetical observations by creating copies of the original data and varying the specified variable across a range of sample points. It then obtains a prediction for each of these hypothetical observations from the MID model. The returned object can be plotted to visualize the ICE curves.

Value

mid.conditional() returns an object of class "mid.conditional". This is a list with the following components:

terms

a character vector of relevant terms for the variable.

observed

a data frame of the original observations used, along with their predictions.

conditional

a data frame of the hypothetical observations and their corresponding predictions.

values

a vector of the sample points for the variable used in the ICE calculation

See Also

interpret, plot.mid.conditional, ggmid.mid.conditional

Examples

data(airquality, package = "datasets")
mid <- interpret(Ozone ~ .^2, data = airquality, lambda = 1)

# Calculate the ICE values for a fitted MID model
ice <- mid.conditional(mid, variable = "Wind", data = airquality)
print(ice)

midr documentation built on Sept. 11, 2025, 1:07 a.m.