The “predictive_interval” Function in R

  • Package: rstanarm

  • Purpose: Computes posterior predictive intervals for new observations based on a fitted model.

  • General class: Statistical modeling

  • Required argument(s):

    • object: The fitted model object.

  • Notable optional arguments:

    • newdata: New data for prediction. If not provided, predictions are based on the original data.

    • draws: Number of posterior samples to generate (posterior_predict is used on the backend).

    • prob: The probability for the predictive interval, the width of the interval from the center. The default is 0.9.

    • ...: Additional arguments controlling the prediction process.

  • Example:

  • # Load the rstanarm package
    library(rstanarm)

    # Fit a Bayesian linear regression model
    x = rnorm(1000)
    y = 2*x + 1 + rnorm(1000) # y ~ 2x + 1 + N(0,1)
    data <- data.frame(x,y)

    model <- stan_glm(y ~ x, data = data)

    # Compute predictive interval for new observations
    pred_interval <- predictive_interval(model, newdata = data.frame(x = 1:10), prob = 0.50, draws = 1000)

    # View the predictive interval
    print(pred_interval)

  • This example demonstrates how to use the predictive_interval function from the rstanarm package to compute posterior predictive intervals for new observations based on a Bayesian linear regression model (model). The newdata argument allows specifying new data for prediction, and prob controls the probability for the predictive interval. Additional optional arguments can be used to further customize the prediction process.

Previous
Previous

The “stan_lmer” Function in R

Next
Next

The “posterior_predict” Function in R