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 = 405.4242 eps = 0.129640
GAMLSS-RS iteration 2: Global Deviance = 405.2294 eps = 0.000480
GAMLSS-RS iteration 3: Global Deviance = 405.2069 eps = 0.000055
GAMLSS-RS iteration 4: Global Deviance = 405.1868 eps = 0.000049
GAMLSS-RS iteration 5: Global Deviance = 405.17 eps = 0.000041
GAMLSS-RS iteration 6: Global Deviance = 405.1562 eps = 0.000033
GAMLSS-RS iteration 7: Global Deviance = 405.1452 eps = 0.000027
GAMLSS-RS iteration 8: Global Deviance = 405.1365 eps = 0.000021
GAMLSS-RS iteration 9: Global Deviance = 405.1297 eps = 0.000016
GAMLSS-RS iteration 10: Global Deviance = 405.1244 eps = 0.000012
GAMLSS-RS iteration 11: Global Deviance = 405.1206 eps = 0.000009
## new data for predictions
nd <- data.frame(speed = c(10, 20, 30))
## default: model parameter(s) for all model parts
predict(m, newdata = nd) mu sigma
1 23.02272 9.956954
2 59.18651 18.423672
3 97.22282 31.930725
## additive predictors on the link scale
predict(m, newdata = nd, type = "link") mu sigma
1 23.02272 2.298271
2 59.18651 2.913636
3 97.22282 3.463569
## mean of the response distribution
predict(m, newdata = nd, type = "response") 1 2 3
23.02272 59.18651 97.22282
## model parameter(s)
predict(m, newdata = nd, model = "sigma") 1 2 3
9.956954 18.423672 31.930725
predict(m, newdata = nd, model = "sigma", drop = FALSE) sigma
1 9.956954
2 18.423672
3 31.930725
## individual terms in additive predictor(s)
predict(m, newdata = nd, type = "terms", model = "sigma") (Intercept) s(speed)
1 2.632278 -0.3340064
2 2.632278 0.2813587
3 2.632278 0.8312911
predict(m, newdata = nd, type = "terms", model = "sigma", terms = "s(speed)") s(speed)
1 -0.3340064
2 0.2813587
3 0.8312911
## predict quantiles
quantile(m, newdata = nd, probs = c(0.1, 0.5, 0.9)) 10% 50% 90%
1 10.26237 23.02272 35.78307
2 35.57562 59.18651 82.79740
3 56.30195 97.22282 138.14369
## standard errors
predict(m, newdata = nd, se.fit = TRUE, R = 200) mu.fit mu.se sigma.fit sigma.se
1 23.02272 1.907417 10.07766 1.583013
2 59.18651 3.447267 18.67225 3.095756
3 97.22282 9.561225 34.39733 13.684037
## fast pointwise confidence bands, without sampling
bands <- predict(m, newdata = nd, level = c(0.8, 0.95))
bands$lower[["80%"]] mu sigma
1 20.45409 8.094526
2 54.72243 15.273264
3 85.75127 19.109395
bands$upper[["95%"]] mu sigma
1 26.95111 13.66691
2 66.01373 24.54339
3 114.76705 70.01636