Extracting Fitted or Predicted Probability Distributions from gamlss2 Models

Description

Methods for gamlss2 model objects for extracting fitted (in-sample) or predicted (out-of-sample) probability distributions as distributions3 objects.

Usage

## S3 method for class 'gamlss2'
prodist(object, ...)

Arguments

object A model object of class gamlss2.
Arguments passed on to predict.gamlss2, e.g., newdata.

Details

To facilitate making probabilistic forecasts based on gamlss2 model objects, the prodist method extracts fitted or predicted probability distribution objects. Internally, the predict.gamlss2 method is used first to obtain the distribution parameters (mu, sigma, tau, nu, or a subset thereof). Subsequently, the corresponding distribution object is set up using the GAMLSS class from the gamlss.dist package, enabling the workflow provided by the distributions3 package (see Zeileis et al. 2022).

Note that these probability distributions only reflect the random variation in the dependent variable based on the model employed (and its associated distributional assumption for the dependent variable). This does not capture the uncertainty in the parameter estimates.

Value

An object of class GAMLSS inheriting from distribution.

References

Zeileis A, Lang MN, Hayes A (2022). “distributions3: From Basic Probability to Probabilistic Regression.” Presented at useR! 2022 - The R User Conference. Slides, video, vignette, code at https://www.zeileis.org/news/user2022/.

See Also

GAMLSS, predict.gamlss2

Examples

library("gamlss2")


## packages, code, and data
library("distributions3")
data("cars", package = "datasets")

## fit heteroscedastic normal GAMLSS model
## stopping distance (ft) explained by speed (mph)
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.1777 eps = 0.002913     
GAMLSS-RS iteration  3: Global Deviance = 405.8035 eps = 0.000921     
GAMLSS-RS iteration  4: Global Deviance = 405.7928 eps = 0.000026     
GAMLSS-RS iteration  5: Global Deviance = 405.7928 eps = 0.000000     
## obtain predicted distributions for three levels of speed
d <- prodist(m, newdata = data.frame(speed = c(10, 20, 30)))
print(d)
                                     1                                      2 
"GAMLSS NO(mu = 23.13, sigma = 10.05)" "GAMLSS NO(mu = 58.79, sigma = 18.57)" 
                                     3 
"GAMLSS NO(mu = 95.49, sigma = 34.32)" 
## obtain quantiles (works the same for any distribution object 'd' !)
quantile(d, 0.5)
       1        2        3 
23.12533 58.79229 95.49447 
quantile(d, c(0.05, 0.5, 0.95), elementwise = FALSE)
     q_0.05    q_0.5    q_0.95
1  6.600763 23.12533  39.64990
2 28.245303 58.79229  89.33928
3 39.041768 95.49447 151.94716
quantile(d, c(0.05, 0.5, 0.95), elementwise = TRUE)
         1          2          3 
  6.600763  58.792294 151.947164 
## visualization
plot(dist ~ speed, data = cars)
nd <- data.frame(speed = 0:240/4)
nd$dist <- prodist(m, newdata = nd)
nd$fit <- quantile(nd$dist, c(0.05, 0.5, 0.95))
matplot(nd$speed, nd$fit, type = "l", lty = 1, col = "slategray", add = TRUE)

## moments
mean(d)
       1        2        3 
23.12533 58.79229 95.49447 
variance(d)
        1         2         3 
 100.9266  344.8914 1177.9175 
## simulate random numbers
random(d, 5)
       r_1      r_2      r_3       r_4       r_5
1 20.74772 34.08110 23.94397  11.98021  25.43647
2 53.55362 64.30583 81.33928  65.54942  47.21962
3 64.90748 77.56831 58.49795 109.21138 100.80322
## density and distribution
pdf(d, 50 * -2:2)
        d_-100        d_-50          d_0        d_50        d_100
1 9.593018e-35 1.241567e-13 0.0028074163 0.001109086 7.655011e-15
2 2.860520e-18 7.588365e-10 0.0001431525 0.019204244 1.832074e-03
3 1.046908e-09 1.455282e-06 0.0002422388 0.004828343 1.152419e-02
cdf(d, 50 * -2:2)
        p_-100        p_-50          p_0       p_50     p_100
1 7.812120e-35 1.682930e-13 0.0106706090 0.99626467 1.0000000
2 6.131240e-18 2.340931e-09 0.0007733678 0.31795118 0.9867532
3 6.129224e-09 1.121339e-05 0.0026978609 0.09249186 0.5522219
## Poisson example
data("FIFA2018", package = "distributions3")
m2 <- gamlss2(goals ~ s(difference), data = FIFA2018, family = PO)
GAMLSS-RS iteration  1: Global Deviance = 355.3922 eps = 0.045332     
GAMLSS-RS iteration  2: Global Deviance = 355.3922 eps = 0.000000     
d2 <- prodist(m2, newdata = data.frame(difference = 0))
print(d2)
                      1 
"GAMLSS PO(mu = 1.237)" 
quantile(d2, c(0.05, 0.5, 0.95))
[1] 0 1 3
## note that log_pdf() can replicate logLik() value
sum(log_pdf(prodist(m2), FIFA2018$goals))
[1] -177.6961
logLik(m2)
'log Lik.' -177.6961 (df=2.005144)