Extracting Fitted or Predicted Parameters or Terms from gamlss2 Models

Description

Methods for gamlss2 model objects for extracting fitted (in-sample) or predicted (out-of-sample) parameters, terms, etc.

Usage

## S3 method for class 'gamlss2'
predict(object, model = NULL, newdata = NULL,
  type = c("parameter", "link", "response", "terms"), terms = NULL,
  se.fit = FALSE, drop = TRUE, exclude = NULL, ...)

Arguments

object model object of class gamlss2.
model character. Which model part(s) should be predicted? Can be one or more of “mu”, “sigma”, etc. By default all model parts are included.
newdata data.frame. Optionally, a new data frame in which to look for variables with which to predict. If omitted, the original observations are used.
type character. Which type of prediction should be computed? Can be the full additive predictor(s) (“link”, before applying the link function(s)), the corresponding parameter (“parameter”, after applying the link function(s)), the individual terms of the additive predictor(s) (“terms”), or the corresponding mean of the response distribution (“response”).
terms character. Which of the terms in the additive predictor(s) should be included? By default all terms are included.
se.fit logical. Should standard errors for the predictions be included? Standard errors are computed by simulating from the approximate multivariate normal distribution of the maximum likelihood estimates. The number of simulations is controlled by the argument R, which defaults to R = 200, and can be passed via .
drop logical. Should the predictions be simplified to a vector if possible (TRUE) or always returned as a data.frame (FALSE)?
exclude character. Any terms named in this vector will be set to zero.
currently only used for catching what as an alias for model.

Details

Predictions for gamlss2 model objects are obtained in the following steps: First, the original data is extracted or some newdata is set up. Second, all of the terms in the additive predictors of all model parameters (“mu”, “sigma”, …) are computed. Third, the full additive predictor(s) are obtained by adding up all individual terms. Fourth, the parameter(s) are obtained from the additive predictor(s) by applying the inverse link function(s). In a final step, the mean of the associated probability distribution can be computed.

See also prodist.gamlss2 for setting up a full distributions3 object from which moments, probabilities, quantiles, or random numbers can be obtained.

Value

If drop = FALSE a data.frame. If drop = TRUE (the default), the data.frame might be simplified to a numeric vector, if possible.

See Also

predict, prodist.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 = NO)
GAMLSS-RS iteration  1: Global Deviance = 407.3647 eps = 0.125474     
GAMLSS-RS iteration  2: Global Deviance = 406.1879 eps = 0.002888     
GAMLSS-RS iteration  3: Global Deviance = 405.7966 eps = 0.000963     
GAMLSS-RS iteration  4: Global Deviance = 405.7908 eps = 0.000014     
GAMLSS-RS iteration  5: Global Deviance = 405.7908 eps = 0.000000     
## new data for predictions
nd <- data.frame(speed = c(10, 20, 30))

## default: additive predictors (on link scale) for all model parameters
predict(m, newdata = nd)
        mu    sigma
1 23.11757 10.05004
2 58.82097 18.56457
3 95.57069 34.28308
## mean of the response distribution
predict(m, newdata = nd, type = "response")
       1        2        3 
23.11757 58.82097 95.57069 
## model parameter(s)
predict(m, newdata = nd)
        mu    sigma
1 23.11757 10.05004
2 58.82097 18.56457
3 95.57069 34.28308
predict(m, newdata = nd, model = "sigma")
       1        2        3 
10.05004 18.56457 34.28308 
predict(m, newdata = nd, model = "sigma", drop = FALSE)
     sigma
1 10.05004
2 18.56457
3 34.28308
## individual terms in additive predictor(s)
predict(m, newdata = nd, type = "terms", model = "sigma")
  (Intercept)   s(speed)
1     2.63897 -0.3313934
2     2.63897  0.2822851
3     2.63897  0.8956823
predict(m, newdata = nd, type = "terms", model = "sigma", terms = "s(speed)")
    s(speed)
1 -0.3313934
2  0.2822851
3  0.8956823
## standard errors
predict(m, newdata = nd, se.fit = TRUE, R = 200)
    mu.fit     mu.se    sigma.fit     sigma.se
1 23.11757  4.936673 1.094468e+01 4.725302e+00
2 58.82097 12.621997 2.381664e+01 1.813702e+01
3 95.57069 98.059417 1.209940e+10 1.711069e+11