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

Support performance_simres class #312

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ S3method(plot,see_parameters_sem)
S3method(plot,see_parameters_simulate)
S3method(plot,see_performance_pp_check)
S3method(plot,see_performance_roc)
S3method(plot,see_performance_simres)
S3method(plot,see_point_estimate)
S3method(plot,see_rope)
S3method(plot,see_si)
Expand Down
5 changes: 5 additions & 0 deletions R/data_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ add_plot_attributes <- function(x) {
obj_name <- attr(x, "object_name", exact = TRUE)
dat <- NULL

# for simulated residuals, we save all necessary information in the object
if (inherits(x, "performance_simres")) {
return(x$fittedModel)
}

if (!is.null(obj_name)) {
# first try, parent frame
dat <- tryCatch(get(obj_name, envir = parent.frame()), error = function(e) NULL)
Expand Down
23 changes: 19 additions & 4 deletions R/plot.check_normality.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#'
#' @return A ggplot2-object.
#'
#' @seealso See also the vignette about [`check_model()`](https://easystats.github.io/performance/articles/check_model.html).

Check warning on line 27 in R/plot.check_normality.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/plot.check_normality.R,line=27,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 125 characters.

Check warning on line 27 in R/plot.check_normality.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/plot.check_normality.R,line=27,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 125 characters.
#'
#' @examplesIf require("performance")
#' m <<- lm(mpg ~ wt + cyl + gear + disp, data = mtcars)
Expand Down Expand Up @@ -70,7 +70,10 @@
} else {
if (type == "qq") { # nolint
model_info <- attributes(x)$model_info
if (inherits(model, c("lme", "lmerMod", "merMod", "afex_aov", "BFBayesFactor"))) {
if (inherits(x, "performance_simres")) {
dat <- stats::na.omit(data.frame(y = x$scaledResiduals))
model_info$is_simulated_residuals <- TRUE
} else if (inherits(model, c("lme", "lmerMod", "merMod", "glmmTMB", "afex_aov", "BFBayesFactor"))) {
res_ <- suppressMessages(sort(stats::residuals(model), na.last = NA))
dat <- stats::na.omit(data.frame(y = res_))
} else if (inherits(model, "glmmTMB")) {
Expand All @@ -97,7 +100,11 @@
model_class = class(model)[1]
)
} else if (type == "density") {
r <- suppressMessages(stats::residuals(model))
if (inherits(x, "performance_simres")) {
r <- attributes(x)$data
} else {
r <- suppressMessages(stats::residuals(model))
}
dat <- as.data.frame(bayestestR::estimate_density(r))
dat$curve <- stats::dnorm(
seq(min(dat$x), max(dat$x), length.out = nrow(dat)),
Expand All @@ -106,7 +113,11 @@
)
.plot_diag_norm(dat, size_line = size_line)
} else if (type == "pp") {
x <- suppressMessages(sort(stats::residuals(model), na.last = NA))
if (inherits(x, "performance_simres")) {
x <- attributes(x)$data
} else {
x <- suppressMessages(sort(stats::residuals(model), na.last = NA))
}
dat <- data.frame(res = x)
.plot_diag_pp(
dat,
Expand All @@ -122,6 +133,10 @@
}


#' @export
plot.see_performance_simres <- plot.see_check_normality


# normality plot: density -------------------------

.plot_diag_norm <- function(x,
Expand Down Expand Up @@ -174,7 +189,7 @@
model_class = NULL) {
qhalfnorm <- function(p) stats::qnorm((p + 1) / 2)
# qq-halfnorm for GLM
if (isTRUE(model_info$is_binomial) || isTRUE(model_info$is_count)) {
if (!isTRUE(model_info$is_simulated_residuals) && (isTRUE(model_info$is_binomial) || isTRUE(model_info$is_count))) {
gg_init <- ggplot2::ggplot(x, ggplot2::aes(x = .data$x, y = .data$y))
qq_stuff <- list(
ggplot2::geom_point(
Expand Down Expand Up @@ -240,7 +255,7 @@
)
},
ggplot2::geom_qq(
mapping = if (detrend) ggplot2::aes(y = ggplot2::after_stat(.data$sample) - ggplot2::after_stat(.data$theoretical)),

Check warning on line 258 in R/plot.check_normality.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/plot.check_normality.R,line=258,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 124 characters.

Check warning on line 258 in R/plot.check_normality.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/plot.check_normality.R,line=258,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 124 characters.
shape = 16,
na.rm = TRUE,
stroke = 0,
Expand Down
Loading