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.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
## obtain predicted distributions for three levels of speed
d <- prodist(m, newdata = data.frame(speed = c(10, 20, 30)))
print(d) 1 2
"GDF NO(mu = 23.12, sigma = 10.05)" "GDF NO(mu = 58.82, sigma = 18.56)"
3
"GDF NO(mu = 95.57, sigma = 34.28)"
## obtain quantiles (works the same for any distribution object 'd' !)
quantile(d, 0.5) 1 2 3
23.11757 58.82097 95.57069
quantile(d, c(0.05, 0.5, 0.95), elementwise = FALSE) q_0.05 q_0.5 q_0.95
1 6.586727 23.11757 39.64840
2 28.284971 58.82097 89.35696
3 39.180036 95.57069 151.96134
quantile(d, c(0.05, 0.5, 0.95), elementwise = TRUE) 1 2 3
6.586727 58.820966 151.961337
## 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.11757 58.82097 95.57069
variance(d) 1 2 3
101.0032 344.6431 1175.3296
## simulate random numbers
random(d, 5) r_1 r_2 r_3 r_4 r_5
1 8.657994 27.50428 18.94314 31.51712 19.16080
2 76.247476 45.87117 72.70055 61.95744 34.79499
3 144.423690 73.13722 133.38308 103.56546 148.34871
## density and distribution
pdf(d, 50 * -2:2) d_-100 d_-50 d_0 d_50 d_100
1 1.024798e-34 1.273422e-13 0.0028169995 0.001109383 7.777801e-15
2 2.750602e-18 7.430283e-10 0.0001419917 0.019195539 1.835767e-03
3 9.985419e-10 1.414926e-06 0.0002389622 0.004810090 1.153999e-02
cdf(d, 50 * -2:2) p_-100 p_-50 p_0 p_50 p_100
1 8.352306e-35 1.727573e-13 0.0107171145 0.99626197 1.0000000
2 5.890414e-18 2.289984e-09 0.0007662613 0.31733979 0.9867278
3 5.831408e-09 1.087434e-05 0.0026542456 0.09188323 0.5513996
## 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
"GDF 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)