model_terms: Find the names of model terms

model_termsR Documentation

Find the names of model terms

Description

Returns the names of any terms in a model, without needing to call summary(). The list of model terms is especially useful when predicting from a mgcv::gam() model using the exclude or terms argument of mgcv::predict.gam() or mgcv::predict.bam().

Usage

model_terms(object, ...)

## S3 method for class 'gam'
model_terms(object, ...)

## S3 method for class 'gamm'
model_terms(object, ...)

## S3 method for class 'gamm4'
model_terms(object, ...)

## S3 method for class 'lm'
model_terms(object, ...)

Arguments

object

a fitted model.

...

arguments to be passed to other methods; not currently used.

Details

From the point of view of gratia, models contain two types of term:

  1. parametric terms, and

  2. smooth terms.

If we consider the formula y ~ fac + s(x2, by = fac) + s(x0), for a factor fac with three levels, there are seven terms in the model:

  1. the model constant term, with name "(Intercept)",

  2. the parametric factor term, with names

    • fac2,

    • fac3,

  3. the univariate smooth of x0, named "s(x0)", and

  4. the three factor-by smooths with names

    • "s(x2):fac1",

    • "s(x2):fac2", and

    • "s(x2):fac3".'

model_terms() will return a vector of those names.

Value

A character vector of model terms.

Examples

load_mgcv()

# simulate data
df <- data_sim("eg4", n = 400, dist = "normal", scale = 2, seed = 1)

# fit model
m <- gam(y ~ fac + s(x2, by = fac) + s(x0),
  data = df, method = "REML")

# return the names of terms in this model
model_terms(m)

gratia documentation built on Feb. 7, 2026, 9:06 a.m.