Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove pp_check() alias #706

Merged
merged 14 commits into from
Mar 30, 2024
90 changes: 74 additions & 16 deletions R/check_predictions.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,83 @@
# args
type <- match.arg(type, choices = c("density", "discrete_dots", "discrete_interval", "discrete_both"))

if (isTRUE(minfo$is_bayesian) && isFALSE(inherits(object, "BFBayesFactor"))) {
insight::check_if_installed(
"bayesplot",
"to create posterior prediction plots for Stan models"
)
bayesplot::pp_check(object)
pp_check.lm(
object,
iterations = iterations,
check_range = check_range,
re_formula = re_formula,
bandwidth = bandwidth,
type = type,
verbose = verbose,
model_info = minfo,
...
)
}

#' @export
check_predictions.stanreg <- function(object,
iterations = 50,
check_range = FALSE,
re_formula = NULL,
bandwidth = "nrd",
type = "density",
verbose = TRUE,
...) {
# retrieve model information
minfo <- insight::model_info(object, verbose = FALSE)

Check warning on line 141 in R/check_predictions.R

View check run for this annotation

Codecov / codecov/patch

R/check_predictions.R#L141

Added line #L141 was not covered by tests

# try to find sensible default for "type" argument
suggest_dots <- (minfo$is_bernoulli || minfo$is_count || minfo$is_ordinal || minfo$is_categorical || minfo$is_multinomial) # nolint
if (missing(type) && suggest_dots) {
type <- "discrete_interval"

Check warning on line 146 in R/check_predictions.R

View check run for this annotation

Codecov / codecov/patch

R/check_predictions.R#L144-L146

Added lines #L144 - L146 were not covered by tests
}

# args
type <- match.arg(type, choices = c("density", "discrete_dots", "discrete_interval", "discrete_both"))

Check warning on line 150 in R/check_predictions.R

View check run for this annotation

Codecov / codecov/patch

R/check_predictions.R#L150

Added line #L150 was not covered by tests

# convert to type-argument for pp_check
type <- switch(type,
density = "density",
"bars"

Check warning on line 155 in R/check_predictions.R

View check run for this annotation

Codecov / codecov/patch

R/check_predictions.R#L153-L155

Added lines #L153 - L155 were not covered by tests
)

insight::check_if_installed(
"bayesplot",
"to create posterior prediction plots for Stan models"

Check warning on line 160 in R/check_predictions.R

View check run for this annotation

Codecov / codecov/patch

R/check_predictions.R#L158-L160

Added lines #L158 - L160 were not covered by tests
)

if (inherits(object, "brmsfit")) {
out <- as.data.frame(bayesplot::pp_check(object, type = type, ndraws = iterations, ...))

Check warning on line 164 in R/check_predictions.R

View check run for this annotation

Codecov / codecov/patch

R/check_predictions.R#L163-L164

Added lines #L163 - L164 were not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we even need to call bayesplot? Couldn't we directly compute predictions like in #202 (comment)

though it might be less efficient

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how we can get dot-plots for discrete/integer outcomes?

} else {
pp_check.lm(
object,
iterations = iterations,
check_range = check_range,
re_formula = re_formula,
bandwidth = bandwidth,
type = type,
verbose = verbose,
model_info = minfo,
...
out <- as.data.frame(bayesplot::pp_check(object, type = type, nreps = iterations, ...))

Check warning on line 166 in R/check_predictions.R

View check run for this annotation

Codecov / codecov/patch

R/check_predictions.R#L166

Added line #L166 was not covered by tests
}

# bring data into shape, like we have for other models with `check_predictions()`
if (type == "density") {
d <- as.data.frame(out$data)
d_filter <- d[!d$is_y, ]
d_filter <- datawizard::data_to_wide(
d_filter,
id_cols = "y_id",
values_from = "value",
names_from = "rep_id"

Check warning on line 177 in R/check_predictions.R

View check run for this annotation

Codecov / codecov/patch

R/check_predictions.R#L170-L177

Added lines #L170 - L177 were not covered by tests
)
d_filter$y_id <- NULL
colnames(d_filter) <- paste0("sim_", colnames(d_filter))
d_filter$y <- d$value[d$is_y, ]
out <- d_filter

Check warning on line 182 in R/check_predictions.R

View check run for this annotation

Codecov / codecov/patch

R/check_predictions.R#L179-L182

Added lines #L179 - L182 were not covered by tests
} else {
colnames(out) <- c("x", "y", "CI_low", "Mean", "CI_high")

Check warning on line 184 in R/check_predictions.R

View check run for this annotation

Codecov / codecov/patch

R/check_predictions.R#L184

Added line #L184 was not covered by tests
}

attr(out, "is_stan") <- TRUE
attr(out, "check_range") <- check_range
attr(out, "response_name") <- resp_string
attr(out, "bandwidth") <- bandwidth
attr(out, "model_info") <- minfo
attr(out, "type") <- type
class(out) <- c("performance_pp_check", "see_performance_pp_check", class(out))
out

Check warning on line 194 in R/check_predictions.R

View check run for this annotation

Codecov / codecov/patch

R/check_predictions.R#L187-L194

Added lines #L187 - L194 were not covered by tests
}

#' @export
Expand Down
Loading