Takes a fitted bamlss object and computes predictions. Predictions can be based on estimated parameters of optimizer functions or on samples returned from sampler functions. It is possible to compute predictions on multiple cores using the parallel and to chunk predictions to save computation time and memory storage. Predictions can be computed for full distributional parameters or specific model terms. If a link{bamlss} model was fitted on multiple cores, i.e., the samples are provided as link{mcmc.list} where each list entry represents samples from one core, function predict.bamlss() computes combined predictions based on samples of all cores.

# S3 method for bamlss
predict(object, newdata, model = NULL, term = NULL,
match.names = TRUE, intercept = TRUE, type = c("link", "parameter"),
FUN = function(x) { mean(x, na.rm = TRUE) }, trans = NULL,
what = c("samples", "parameters"), nsamps = NULL,
verbose = FALSE, drop = TRUE,
cores = NULL, chunks = 1, ...)

Arguments

object

An object of class "bamlss"

newdata

A data frame or list containing the values of the model covariates at which predictions are required. Note that depending on argument term, only covariates that are needed by the corresponding model terms need to be supplied.

model

Character or integer, specifies the model for which predictions should be computed.

term

Character or integer, specifies the model terms for which predictions are required. Note that, e.g., term = c("s(x1)", "x2") will compute the combined prediction s(x1) + x2.

match.names

Should partial string matching be used to select the terms for prediction. Note that, e.g., term = "x1" will select all terms including "x1" if match.names = TRUE.

intercept

Should the intercept be included?

type

If type = "link" the predictor of the corresponding model is returned. If type = "parameter" predictions on the distributional parameter scale are returned.

FUN

A function that should be applied on the samples of predictors or parameters, depending on argument type.

trans

A transformer function or named list of transformer functions that computes transformed predictions. If trans is a list, the list names must match the names of the parameters of the bamlss.family.

what

Predictions can be computed from samples or estimated parameters of optimizer functions. If no samples are available the default is to use estimated parameters.

nsamps

If the fitted bamlss object contains samples of parameters, computing predictions may take quite some time. Therefore, to get a first feeling it can be useful to compute predictions only based on nsamps samples, i.e., nsamps specifies the number of samples which are extracted on equidistant intervals.

verbose

If predictions are chunked, information on the prediction process can be printed.

drop

If predictions for only one model are returned, the list structure is dropped.

cores

Specifies the number of cores that should be used for prediction. Note that this functionality is based on the parallel package.

chunks

Should computations be split into chunks? Prediction is then processed sequentially.

...

Arguments passed to prediction functions that are part of a bamlss.family object, i.e., the objects has a \$predict() function that should be used instead.

Value

Depending on arguments model, FUN and the structure of the bamlss

model, a list of predictions or simple vectors or matrices of predictions.

link{bamlss}, fitted.bamlss.

Examples

if (FALSE) ## Generate some data.
d <- GAMart()

## Model formula.
f <- list(
num ~ s(x1) + s(x2) + s(x3) + te(lon,lat),
sigma ~ s(x1) + s(x2) + s(x3) + te(lon,lat)
)

## Estimate model.
b <- bamlss(f, data = d)
#> Error in bamlss.model.frame(formula, data, family, weights, subset, offset,     na.action, specials, contrasts): object 'd' not found

## Predictions.
p <- predict(b)
str(b)

## Prediction for "mu" model and term "s(x2)".
p <- predict(b, model = "mu", term = "s(x2)")
#> Error in predict(b, model = "mu", term = "s(x2)"): object 'b' not found

## Plot effect
plot2d(p ~ x2, data = d)
#> Error in plot2d(p ~ x2, data = d): object 'd' not found

## Same for "sigma" model.
p <- predict(b, model = "sigma", term = "s(x2)")
#> Error in predict(b, model = "sigma", term = "s(x2)"): object 'b' not found
plot2d(p ~ x2, data = d)
#> Error in plot2d(p ~ x2, data = d): object 'd' not found

## Prediction for "mu" model and term "s(x1)" + "s(x2)"
## without intercept.
p <- predict(b, model = "mu", term = c("s(x1)", "s(x2)"),
intercept = FALSE)
#> Error in predict(b, model = "mu", term = c("s(x1)", "s(x2)"), intercept = FALSE): object 'b' not found

## Prediction based on quantiles.
p <- predict(b, model = "mu", term = "s(x2)", FUN = c95)
#> Error in predict(b, model = "mu", term = "s(x2)", FUN = c95): object 'b' not found
plot2d(p ~ x2, data = d)
#> Error in plot2d(p ~ x2, data = d): object 'd' not found

## Extract samples of predictor for "s(x2)".
p <- predict(b, model = "mu", term = "s(x2)",
intercept = FALSE, FUN = function(x) { x })
#> Error in predict(b, model = "mu", term = "s(x2)", intercept = FALSE, FUN = function(x) {    x}): object 'b' not found
print(dim(p))
plot2d(p ~ x2, data = d, col.lines = rgb(0.1, 0.1, 0.1, alpha = 0.1))
#> Error in plot2d(p ~ x2, data = d, col.lines = rgb(0.1, 0.1, 0.1, alpha = 0.1)): object 'd' not found

## Or using specific combinations of terms.
p <- predict(b, model = "mu", term = c("s(x2)", "te(lon,lat)"),
intercept = FALSE, FUN = function(x) { x })
#> Error in predict(b, model = "mu", term = c("s(x2)", "te(lon,lat)"), intercept = FALSE,     FUN = function(x) {        x    }): object 'b' not found

## Prediction using new data.
## Only need x3 data when predicting
## for s(x3).
nd <- data.frame("x3" = seq(0, 1, length = 100))
nd <- cbind(nd, predict(b, newdata = nd, term = "s(x3)"))
#> Error in predict(b, newdata = nd, term = "s(x3)"): object 'b' not found