|
| 1 | +\name{prodist.gamlss} |
| 2 | + |
| 3 | +\alias{prodist.gamlss} |
| 4 | + |
| 5 | +\title{Extracting Fitted or Predicted Probability Distributions from gamlss Models} |
| 6 | + |
| 7 | +\description{ |
| 8 | +Methods for \pkg{gamlss} model objects for extracting fitted (in-sample) or |
| 9 | +predicted (out-of-sample) probability distributions as \pkg{distributions3} |
| 10 | +objects. |
| 11 | +} |
| 12 | + |
| 13 | +\usage{ |
| 14 | +\method{prodist}{gamlss}(object, ...) |
| 15 | +} |
| 16 | +\arguments{ |
| 17 | + \item{object}{A model object of class \code{\link{gamlss}}.} |
| 18 | + \item{...}{Arguments passed on to \code{\link{predictAll}}, e.g., \code{newdata}.} |
| 19 | +} |
| 20 | + |
| 21 | +\details{ |
| 22 | +To facilitate making probabilistic forecasts based on \code{\link{gamlss}} |
| 23 | +model objects, the \code{\link[distributions3]{prodist}} method extracts fitted |
| 24 | +or predicted probability \code{distribution} objects. Internally, the |
| 25 | +\code{\link{predictAll}} method is used first to obtain the distribution |
| 26 | +parameters (\code{mu}, \code{sigma}, \code{tau}, \code{nu}, or a subset thereof). |
| 27 | +Subsequently, the corresponding \code{distribution} object is set up using the |
| 28 | +\code{\link[gamlss.dist]{GAMLSS}} class from the \pkg{gamlss.dist} package, |
| 29 | +enabling the workflow provided by the \pkg{distributions3} package (see Zeileis |
| 30 | +et al. 2022). |
| 31 | + |
| 32 | +Note that these probability distributions only reflect the random variation in |
| 33 | +the dependent variable based on the model employed (and its associated |
| 34 | +distributional assumption for the dependent variable). This does not capture the |
| 35 | +uncertainty in the parameter estimates. |
| 36 | +} |
| 37 | + |
| 38 | +\value{ |
| 39 | +An object of class \code{GAMLSS} inheriting from \code{distribution}. |
| 40 | +} |
| 41 | + |
| 42 | +\references{ |
| 43 | +Zeileis A, Lang MN, Hayes A (2022). |
| 44 | +\dQuote{distributions3: From Basic Probability to Probabilistic Regression.} |
| 45 | +Presented at \emph{useR! 2022 - The R User Conference}. |
| 46 | +Slides, video, vignette, code at \url{https://www.zeileis.org/news/user2022/}. |
| 47 | +} |
| 48 | + |
| 49 | +\seealso{ |
| 50 | +\code{\link[gamlss.dist]{GAMLSS}}, \code{\link{predictAll}} |
| 51 | +} |
| 52 | + |
| 53 | +\examples{ |
| 54 | +\dontshow{ if(!requireNamespace("distributions3")) { |
| 55 | + if(interactive() || is.na(Sys.getenv("_R_CHECK_PACKAGE_NAME_", NA))) { |
| 56 | + stop("not all packages required for the example are installed") |
| 57 | + } else q() } |
| 58 | +} |
| 59 | +## packages, code, and data |
| 60 | +library("gamlss") |
| 61 | +library("distributions3") |
| 62 | +data("cars", package = "datasets") |
| 63 | + |
| 64 | +## fit heteroscedastic normal GAMLSS model |
| 65 | +## stopping distance (ft) explained by speed (mph) |
| 66 | +m <- gamlss(dist ~ pb(speed), ~ pb(speed), data = cars, family = "NO") |
| 67 | + |
| 68 | +## obtain predicted distributions for three levels of speed |
| 69 | +d <- prodist(m, newdata = data.frame(speed = c(10, 20, 30))) |
| 70 | +print(d) |
| 71 | + |
| 72 | +## obtain quantiles (works the same for any distribution object 'd' !) |
| 73 | +quantile(d, 0.5) |
| 74 | +quantile(d, c(0.05, 0.5, 0.95), elementwise = FALSE) |
| 75 | +quantile(d, c(0.05, 0.5, 0.95), elementwise = TRUE) |
| 76 | + |
| 77 | +## visualization |
| 78 | +plot(dist ~ speed, data = cars) |
| 79 | +nd <- data.frame(speed = 0:240/4) |
| 80 | +nd$dist <- prodist(m, newdata = nd) |
| 81 | +nd$fit <- quantile(nd$dist, c(0.05, 0.5, 0.95)) |
| 82 | +matplot(nd$speed, nd$fit, type = "l", lty = 1, col = "slategray", add = TRUE) |
| 83 | + |
| 84 | +## moments |
| 85 | +mean(d) |
| 86 | +variance(d) |
| 87 | + |
| 88 | +## simulate random numbers |
| 89 | +random(d, 5) |
| 90 | + |
| 91 | +## density and distribution |
| 92 | +pdf(d, 50 * -2:2) |
| 93 | +cdf(d, 50 * -2:2) |
| 94 | + |
| 95 | +## Poisson example |
| 96 | +data("FIFA2018", package = "distributions3") |
| 97 | +m2 <- gamlss(goals ~ pb(difference), data = FIFA2018, family = "PO") |
| 98 | +d2 <- prodist(m2, newdata = data.frame(difference = 0)) |
| 99 | +print(d2) |
| 100 | +quantile(d2, c(0.05, 0.5, 0.95)) |
| 101 | + |
| 102 | +## note that log_pdf() can replicate logLik() value |
| 103 | +sum(log_pdf(prodist(m2), FIFA2018$goals)) |
| 104 | +logLik(m2) |
| 105 | +} |
| 106 | + |
| 107 | +\keyword{distribution} |
0 commit comments