pm_coef: Supporting a New Model Class

View source: R/adapter.R

pm_coefR Documentation

Supporting a New Model Class

Description

Kmatrix, predictmeans, contrastmeans and covariatemeans get everything they need from a model through these seven generics. Their default methods work for any model class with the usual methods (coef, vcov, terms, df.residual, family), so many classes need no extra code. For a class that lacks one of these, or needs something special, write a method for the corresponding generic only; nothing inside predictmeans needs to change.

Usage

pm_coef(model, ...)
pm_vcov(model, ...)
pm_basis(model, newdata, ...)
pm_df(model, L, ...)
pm_linkinv(model, ...)
pm_contrasts(model, ...)

Arguments

model

A fitted model.

newdata

A data frame of predictor values (the reference grid).

L

A matrix of linear functions of the coefficients, one row per contrast or mean.

...

Further arguments. Kmatrix() passes terms, contrasts and xlev to pm_basis(), and exclude for gam models.

Details

pm_coef(model)

Named fixed-effect estimates, with NA for aliased coefficients. Default: coef(model); methods for lme, merMod and glmmTMB return the fixed effects.

pm_vcov(model)

Covariance matrix of the non-aliased estimates. Default: vcov(model).

pm_basis(model, newdata, ...)

Design matrix of the fixed effects for newdata, with columns named as in pm_coef(model): the columns are matched to the coefficients by name, and a coefficient without a column stops with a message (a column for an aliased coefficient may be there or not). Default: model.matrix() of the model's terms (with their stored predvars, so poly(), ns(), scale() etc. use the fitted basis), contrasts and factor levels. The gam method uses the linear predictor matrix of package mgcv.

pm_data(model)

The data used to fit the model. The default method (see pm_data) finds them from the data argument of the call, getData() or the model frame; Kmatrix() uses a pm_data() method only when the class has its own.

pm_df(model, L)

Denominator degrees of freedom for each row of L, or NULL (the default) to use the model's single residual df: df.residual(model), or Inf (normal-based) when there is none. Kenward-Roger df for lmer and the rules of nlme for lme are built in.

pm_linkinv(model)

Inverse link function used to back-transform the means, or NULL for none. Default: the inverse link of family(model) unless it is the identity.

pm_contrasts(model)

The contrasts of the factors used in the fit, as for the contrasts.arg argument of model.matrix(). Default: those of model.matrix(model), else model$contrasts; a method is only needed for a class that has neither.

The model also needs a terms() method for its fixed effects (used to find the variables in the model), and its call (getCall()) or a pm_data() method so that the data can be found.

Value

As described in Details.

Author(s)

Dongwen Luo, Siva Ganesh and John Koolaard

See Also

Kmatrix, pm_data

Examples

  library(predictmeans)
  # A made-up model class that wraps lm() and has only four methods. (Methods are
  # found where predictmeans looks for them: in a package's namespace or, as
  # here, in the global environment; registerS3method() also works.)
  fit_mymodel <- function(formula, data) {
    structure(list(fit = lm(formula, data), call = match.call()), class = "mymodel")
  }
  registerS3method("coef", "mymodel", function(object, ...) coef(object$fit))
  registerS3method("vcov", "mymodel", function(object, ...) vcov(object$fit))
  registerS3method("terms", "mymodel", function(x, ...) terms(x$fit))
  registerS3method("df.residual", "mymodel", function(object, ...) object$fit$df.residual)

  Oats <- as.data.frame(nlme::Oats)
  Oats$nitro <- factor(Oats$nitro)
  m <- fit_mymodel(yield ~ Block + nitro*Variety, data = Oats)
  predictmeans(m, "nitro")$means

  # Degrees of freedom of its own, e.g. normal-based:
  registerS3method("pm_df", "mymodel", function(model, L, ...) rep(Inf, nrow(L)))
  predictmeans(m, "nitro")$means

predictmeans documentation built on Nov. 24, 2026, 5:10 p.m.