Special model terms extend a gamlss2 formula with a model component that is fitted inside the backfitting algorithm. Ordinary linear terms and mgcv smooths cover many models; specials are useful when the fitting method needs its own estimator, prediction method, state, or tuning parameters.
The standard fitting workflow is described in First Steps. This vignette focuses on extending the predictor; Family Objects instead extends the response distribution.
1 Built-in special terms
The package already provides several specials. For example, lo() fits a weighted local-polynomial smoother, tree(), ct(), and cf() fit tree-based terms, n() fits a neural-network term, and lin(), re(), random(), and gnet() provide structured linear or grouped effects. The mgcv terms s(), te(), ti(), and t2() are also supported.
Specials can occur in any distributional parameter formula. They are refitted from the current working response and weights at each backfitting iteration, then centered before being added to the additive predictor. Centering keeps the intercept and the special term identifiable.
2 Defining a custom special
A custom special consists of three pieces:
a constructor used in the formula;
a special_fit() method that fits the term to the current working response and weights; and
a special_predict() method for fitted values on new data.
The constructor should return a list of class c("special", "user") containing the formula, variables, data, controls, and anything else needed by fitting and prediction. The reserved user name is recognized by fake_formula(), so it can be used without changing package internals. For a package-level special, register its name in fake_formula() and use a dedicated class instead.
A fitting method receives at least x, z, and w. The framework can also supply y, eta, j, family, and control; accepting ... keeps a method compatible with these additional arguments. The returned list must contain numeric fitted.values. It should usually also contain:
edf, the effective degrees of freedom;
model, the fitted underlying model;
shift, the centering constant; and
optionally transfer, for state passed to the next backfitting iteration.
A prediction method receives the fitted object and data, and returns a numeric vector. If it supports se.fit = TRUE, it should return a data frame with a fit column and, optionally, interval columns.
3 A custom weighted local smoother
The following example implements a small user() term around stats::loess(). It is deliberately compact: a production term should validate its controls and handle missing values and extrapolation explicitly.
The user constructor is available while the formula is parsed because it is one of the reserved special names. The corresponding methods are found by S3 dispatch during fitting and prediction.
Use an existing special when it already provides the required estimator and prediction behavior. Write a custom special when the term has a distinct fitting algorithm, needs custom state, or wraps an external modelling method. Keep the constructor lightweight, store all information needed for prediction, return centered fitted contributions, and test both fitting and newdata predictions. Prediction support alone does not imply support for joint coefficient uncertainty: the Gaussian intervals in Prediction and Uncertainty require a coefficient-linear design and a verifiable quadratic penalty. A custom fitting method may need its own uncertainty procedure. The help topic ?special_terms documents the method arguments and return-value contract; ?fake_formula documents special-name registration.