From 9a894725cea185c3eaee23ca5904b7c5607f9f56 Mon Sep 17 00:00:00 2001 From: Kaminski Date: Thu, 28 Apr 2022 10:02:35 +0200 Subject: [PATCH] fixed plot_acf() factor issue #159 --- DESCRIPTION | 5 +++-- R/plot_acf.R | 10 +++++++--- tests/testthat/objects_for_tests.R | 2 ++ tests/testthat/test_plotsR.R | 1 + 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index cf68f641..b6c8d412 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: auditor Title: Model Audit - Verification, Validation, and Error Analysis -Version: 1.3.4 +Version: 1.3.5 Authors@R: c( person("Alicja", "Gosiewska", email = "alicjagosiewska@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0001-6563-5742")), @@ -10,7 +10,8 @@ Authors@R: c( comment = c(ORCID = "0000-0001-6661-5364")), person("Tomasz", "Mikołajczyk", role = c("aut")), person("Michal", "Burdukiewicz", role = c("ctb")), - person("Szymon", "Maksymiuk", role = c("ctb")) + person("Szymon", "Maksymiuk", role = c("ctb")), + person("Michal", "Kaminski", role = c("ctb")) ) Description: Provides an easy to use unified interface for creating validation plots for any model. The 'auditor' helps to avoid repetitive work consisting of writing code needed to create residual plots. diff --git a/R/plot_acf.R b/R/plot_acf.R index 76a78866..f7ac42fe 100644 --- a/R/plot_acf.R +++ b/R/plot_acf.R @@ -51,11 +51,15 @@ plot_acf <- function(object, ..., variable = NULL, alpha = 0.95) { # data frame for ggplot object df <- make_dataframe(object, ..., variable = variable, type = "res") - result_df <- data.frame(acf = numeric(), label = character(), lag = numeric(), ymin = numeric()) - for (label in unique(df$`_label_`)) { + + result_df <- data.frame(acf = numeric(), label = factor(), lag = numeric(), ymin = numeric()) + + label_levels <- levels(df$`_label_`) + + for (label in label_levels) { orderedResiduals <- df[which(df$`_label_` == label), "_residuals_"] acf <- acf(orderedResiduals, plot = FALSE) - result_df <- rbind(result_df, data.frame(acf = acf$acf[-1], label = label, lag = acf$lag[-1], ymin = 0)) + result_df <- rbind(result_df, data.frame(acf = acf$acf[-1], label = factor(label, levels = label_levels), lag = acf$lag[-1], ymin = 0)) } df <- result_df diff --git a/tests/testthat/objects_for_tests.R b/tests/testthat/objects_for_tests.R index 819baa90..b64af2ef 100644 --- a/tests/testthat/objects_for_tests.R +++ b/tests/testthat/objects_for_tests.R @@ -45,11 +45,13 @@ model_class_glm2 <- glm(factor(y) ~ x3, family=binomial, data=artifficial_classi exp_class_glm2 <- explain(model_class_glm2, data = artifficial_classif, y = artifficial_classif$y, verbose = FALSE) exp_lm <- explain(model_lm, label = "lm", data = artifficial_regr, y = artifficial_regr$y, verbose = FALSE) exp_glm <- explain(model_glm, label = "glm", data = artifficial_classif_2, y = artifficial_classif_2$y, verbose = FALSE) +exp_glm2 <- explain(model_glm, label = "glm2", data = artifficial_classif_2, y = artifficial_classif_2$y, verbose = FALSE) exp_rf <- explain(model_rf, label="rf", data =artifficial_classif_2, y = artifficial_classif_2$y, verbose = FALSE) exp_class_glm <- explain(model_class_glm, label="class glm", data = artifficial_classif, y = artifficial_classif$y, verbose = FALSE) mr_rf <- model_residual(exp_rf) mr_glm <- model_residual(exp_glm) +mr_glm2 <- model_residual(exp_glm2) cd_lm <- model_cooksdistance(exp_lm) cd_rf <- model_cooksdistance(exp_rf) diff --git a/tests/testthat/test_plotsR.R b/tests/testthat/test_plotsR.R index 4a7cde94..d5e44832 100644 --- a/tests/testthat/test_plotsR.R +++ b/tests/testthat/test_plotsR.R @@ -7,6 +7,7 @@ test_that("plot_acf", { expect_is(plot_acf(mr_rf, variable = "_y_"), "gg") expect_is(plot_acf(mr_rf, variable = "_y_hat_"), "gg") expect_is(plot_acf(mr_rf), "gg") + expect_error(plot_acf(mr_rf, mr_glm, mr_glm2), NA) }) test_that("plot_autocorrelation", {