predict_glm: Predict from a GLM with modified coefficients

View source: R/glmest.R

predict_glmR Documentation

Predict from a GLM with modified coefficients

Description

Compute predictions from a fitted stats::glm object, optionally substituting new parameter values. Unlike stats::predict.glm, this function allows the user to supply an arbitrary coefficient vector p, which is useful for computing predictions at counterfactual parameter values (e.g., during optimization or simulation). The returned value also includes a "grad" attribute containing the Jacobian of predictions with respect to the coefficients.

Usage

predict_glm(
  object,
  p = coef(object),
  data,
  offset = NULL,
  type = c("response", "link"),
  ...
)

Arguments

object

A fitted glm object.

p

Numeric vector of coefficients (defaults to coef(x)).

data

Optional data frame for computing the model matrix and response. If missing, the original model matrix from the fitted object is used.

offset

Optional offset vector. If NULL (default), the offset stored in the fitted model (x$offset) is used.

type

Character; "response" (default) returns predictions on the response scale via the inverse link function, "link" returns predictions on the linear predictor scale.

...

Additional arguments (currently unused).

Value

Numeric vector of predictions with a "grad" attribute containing the gradient (Jacobian) of predictions with respect to the coefficients. When type = "link", the gradient is simply the model matrix X.

See Also

stats::predict.glm, score.glm

Examples

m <- glm(mpg ~ hp + wt, data = mtcars)
p0 <- coef(m)
# Predictions at fitted coefficients match predict.glm
all.equal(as.numeric(predict_glm(m)), fitted(m))
# Predictions with modified coefficients
predict_glm(m, p = p0 * 1.1)

lava documentation built on Oct. 23, 2026, 1:09 a.m.