Learn R Programming

psycho (version 0.4.91)

get_predicted.stanreg: Compute predicted values of stanreg models.

Description

Compute predicted from a stanreg model.

Usage

# S3 method for stanreg
get_predicted(fit, newdata = "model", prob = 0.9,
  odds_to_probs = TRUE, keep_iterations = FALSE, draws = NULL,
  posterior_predict = FALSE, seed = NULL, transform = FALSE,
  re.form = "default", ...)

Arguments

fit

A stanreg model.

newdata

A data frame in which to look for variables with which to predict. If omitted, the model matrix is used. If "model", the model's data is used.

prob

Probability of credible intervals (0.9 (default) will compute 5-95% CI). Can also be a list of probs (e.g., c(0.90, 0.95)).

odds_to_probs

Transform log odds ratios in logistic models to probabilies.

keep_iterations

Keep all prediction iterations.

draws

An integer indicating the number of draws to return. The default and maximum number of draws is the size of the posterior sample.

posterior_predict

Posterior draws of the outcome instead of the link function (i.e., the regression "line").

seed

An optional seed to use.

transform

If posterior_predict is False, should the linear predictor be transformed using the inverse-link function? The default is FALSE, in which case the untransformed linear predictor is returned.

re.form

If object contains group-level parameters, a formula indicating which group-level parameters to condition on when making predictions. re.form is specified in the same form as for predict.merMod. NULL indicates that all estimated group-level parameters are conditioned on. To refrain from conditioning on any group-level parameters, specify NA or ~0. The newdata argument may include new levels of the grouping factors that were specified when the model was estimated, in which case the resulting posterior predictions marginalize over the relevant variables (see posterior_predict.stanreg). If "default", then will ne NULL if the random are present in the data, and NA if not.

...

Arguments passed to or from other methods.

Value

dataframe with predicted values.

Examples

Run this code
# NOT RUN {
library(psycho)
library(ggplot2)
require(rstanarm)

fit <- rstanarm::stan_glm(Tolerating ~ Adjusting, data = affective)

refgrid <- psycho::refdata(affective, "Adjusting")
predicted <- get_predicted(fit, newdata = refgrid)

ggplot(predicted, aes(x = Adjusting, y = Tolerating_Median)) +
  geom_line() +
  geom_ribbon(aes(
    ymin = Tolerating_CI_5,
    ymax = Tolerating_CI_95
  ),
  alpha = 0.1
  )

fit <- rstanarm::stan_glm(Sex ~ Adjusting, data = affective, family = "binomial")

refgrid <- psycho::refdata(affective, "Adjusting")
predicted <- get_predicted(fit, newdata = refgrid)

ggplot(predicted, aes(x = Adjusting, y = Sex_Median)) +
  geom_line() +
  geom_ribbon(aes(
    ymin = Sex_CI_5,
    ymax = Sex_CI_95
  ),
  alpha = 0.1
  )
# }

Run the code above in your browser using DataLab