Predictions from gamlss2 Models

Description

Predict response distributions, parameters, additive predictors, terms, or response means from a gamlss2 model.

Usage

## S3 method for class 'gamlss2'
predict(object, parameter = NULL, newdata = NULL,
  type = c("distribution", "parameter", "link", "response", "terms"),
  terms = NULL, se.fit = FALSE, drop = TRUE, ...,
  level = NULL, interval = c("none", "wald"), unconditional = FALSE)

Arguments

object A gamlss2 model.
parameter Distribution parameter name(s) or index(es), such as “mu” or “sigma”; defaults to all parameters.
newdata Data frame for predictions; defaults to the fitted data.
type “distribution” (default) returns distributions3 objects; “parameter” returns parameters on their natural scale; “link” returns additive predictors; “response” returns response means; “terms” returns individual additive terms. Specifying parameter without type selects “parameter”.
terms Additive terms to include; defaults to all. Names are partially matched unless nogrep = TRUE is passed in ….
se.fit For ordinary predictions, TRUE computes standard errors from coefficient draws; R in … sets the number of draws (default 200).
drop Simplify the result when possible.
level Numeric confidence level(s) between zero and one; supplying levels selects pointwise Wald intervals.
interval “none” (default) returns ordinary predictions; “wald” returns analytic pointwise confidence intervals, at the 95% confidence level by default.
unconditional If TRUE, include approximate smoothing-parameter uncertainty in Wald intervals and coefficient draws. This requires a likelihood fit without stored posterior samples.
… Further controls, including FUN (a function summarizing predictions across coefficient draws), R (number of Gaussian draws, default 200), seed (default 123; FALSE continues the current random-number stream), and burnin for simulation. For likelihood fits, FUN or se.fit = TRUE requests simulation; R alone does not.

Details

Coefficient draws stored in the fit are used when available; R does not replace stored posterior samples. Otherwise, Gaussian draws use the full joint coefficient covariance, retaining dependence among terms and distributional parameters. For penalized likelihood fits, this approximation is motivated by the Bayesian interpretation of smoothing penalties. Nonlinear transformations can produce asymmetric uncertainty distributions.

To propagate uncertainty to a quantile or event probability, retain the joint distribution draws with FUN = identity, evaluate the quantity within each draw, and then summarize across draws at each prediction location. These intervals describe estimation uncertainty in the chosen quantity. Quantiles of the fitted response distribution instead give plug-in prediction intervals for a future observation.

Wald intervals are approximate pointwise confidence intervals for fitted values, not prediction intervals for future observations. They are computed without simulation and may be asymmetric on transformed scales. They condition on fitted smoothing parameters by default and exclude model-selection uncertainty; see vcov.gamlss2.

Wald intervals support maximum-likelihood fits with linear terms and coefficient-linear mgcv smooths, but not nonlinear custom special terms. They cannot be combined with FUN, R, seed, or burnin. An indefinite joint information matrix or non-estimable coefficient causes an error rather than a working-information approximation.

Value

Ordinary predictions return a distribution object or values in the selected form. Term predictions are data frames or, for multiple parameters, lists of matrices. With se.fit = TRUE, simulation summaries use names fit (simulation mean) and se (simulation standard deviation), prefixed by parameter or term names when needed. A supplied FUN determines the simulation summary; its center can differ from the prediction at the estimated coefficients. With FUN = identity, simulated distribution predictions form a list of distribution objects, one per joint coefficient draw.

Wald predictions return fit, se.fit, lower, and upper. For multiple levels, lower and upper are lists named by confidence percentage (for example, “80%” and “95%”). fit and each bound retain the structure selected by parameter, terms, and drop; se.fit is shared across levels.

See Also

predict, marginal_predict, prodist.gamlss2, quantile.gamlss2

Examples

library("gamlss2")

## fit heteroscedastic normal GAMLSS model
## stopping distance (ft) explained by speed (mph)
data("cars", package = "datasets")
m <- gamlss2(dist ~ s(speed) | s(speed), data = cars, family = Normal)
GAMLSS-RS iteration  1: Global Deviance = 405.0351 eps = 0.130476     
GAMLSS-RS iteration  2: Global Deviance = 405.6171 eps = 0.001437     
GAMLSS-RS iteration  3: Global Deviance = 405.6598 eps = 0.000105     
GAMLSS-RS iteration  4: Global Deviance = 405.6622 eps = 0.000005     
## new data for predictions
nd <- data.frame(speed = c(10, 20, 30))

## default: a "distribution" object
d <- predict(m, newdata = nd)
print(d)
                                  1                                   2 
"Normal(mu = 23.07, sigma = 10.03)" "Normal(mu = 58.88, sigma = 18.55)" 
                                  3 
"Normal(mu = 96.16, sigma = 34.31)" 
## can be used with distributions3 methods
pdf(d, 20)
           1            2            3 
0.0379561830 0.0023911863 0.0009891498 
cdf(d, 20)
         1          2          3 
0.37993725 0.01804292 0.01320648 
quantile(d, 0.8)
        1         2         3 
 31.50817  74.49405 125.03656 
mean(d)
       1        2        3 
23.06591 58.88142 96.16345 
variance(d)
        1         2         3 
 100.6198  344.1271 1176.9385 
random(d, 3)
       r_1       r_2       r_3
1 15.69355  18.48105  9.130123
2 59.33921  94.08898 35.777362
3 17.90342 114.47704 95.676874
## additive predictors on the link scale
predict(m, newdata = nd, type = "link")
        mu    sigma
1 23.06591 2.305675
2 58.88142 2.920505
3 96.16345 3.535336
## mean of the response distribution
predict(m, newdata = nd, type = "response")
[1] 23.06591 58.88142 96.16345
## same as
mean(d)
       1        2        3 
23.06591 58.88142 96.16345 
## all model parameters
predict(m, newdata = nd, type = "parameter")
        mu    sigma
1 23.06591 10.03094
2 58.88142 18.55066
3 96.16345 34.30654
## parameter selects parameter predictions if type is omitted
predict(m, newdata = nd, parameter = "sigma")
       1        2        3 
10.03094 18.55066 34.30654 
predict(m, newdata = nd, parameter = "sigma", drop = FALSE)
     sigma
1 10.03094
2 18.55066
3 34.30654
## individual terms in additive predictor(s)
predict(m, newdata = nd, type = "terms", parameter = "sigma")
  (Intercept)   s(speed)
1    2.637683 -0.3320086
2    2.637683  0.2828221
3    2.637683  0.8976526
predict(m, newdata = nd, type = "terms", parameter = "sigma", terms = "s(speed)")
    s(speed)
1 -0.3320086
2  0.2828221
3  0.8976526
## predict quantiles
quantile(m, newdata = nd, probs = c(0.1, 0.5, 0.9))
       10%      50%       90%
1 10.21074 23.06591  35.92109
2 35.10779 58.88142  82.65505
3 52.19786 96.16345 140.12905
## same as
quantile(d, probs = c(0.1, 0.5, 0.9), elementwise = FALSE)
     q_0.1    q_0.5     q_0.9
1 10.21074 23.06591  35.92109
2 35.10779 58.88142  82.65505
3 52.19786 96.16345 140.12905
## standard errors
predict(m, type = "parameter", newdata = nd, se.fit = TRUE, R = 200)
    mu.fit    mu.se sigma.fit  sigma.se
1 23.06591 1.916477  10.15516  1.613394
2 58.88142 3.470764  18.79351  3.066396
3 96.16345 9.332039  36.44916 13.061156
## fast pointwise confidence bands, without sampling
bands <- predict(m, type = "parameter", newdata = nd, level = c(0.8, 0.95))
bands$lower[["80%"]]
        mu     sigma
1 20.48404  7.966721
2 54.39246 15.066589
3 84.93721 18.766883
bands$upper[["95%"]]
         mu    sigma
1  27.01455 13.18790
2  65.74669 23.87909
3 113.33251 58.07239
## prediction uncertainty using multiple draws
d <- predict(m, newdata = cars, R = 1000, FUN = identity)

## predict 1000 quantiles
q <- sapply(d, quantile, probs = 0.95)

i95 <- function(x) {
  setNames(quantile(x, c(0.025, 0.5, 0.975), names = FALSE),
    c("lower", "fit", "upper"))
}

## quantiles of quantile samples
q <- t(apply(q, 1, i95))

## plot quantile with uncertainty bands
plot(dist ~ speed, data = cars,
  ylim = range(cars$dist, q))
matlines(cars$speed, q,
  lty = c(2, 1, 2), col = 4, lwd = 2)