From 6b075ec751393f1deaff452acb1ebcaf9a0e5612 Mon Sep 17 00:00:00 2001 From: Chen-Yang Su Date: Fri, 27 Nov 2026 10:12:37 -0800 Subject: [PATCH] Use each row's own slope for esarey p-values in estimate_slopes() --- DESCRIPTION | 2 +- NEWS.md | 6 ++++++ R/p_adjust.R | 13 +++++-------- tests/testthat/test-estimate_slopes.R | 22 +++++++++++++++++++++- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 244a48832..34c93dc3a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: modelbased Title: Estimation of Model-Based Predictions, Contrasts and Means -Version: 0.19.0.3 +Version: 0.19.0.4 Authors@R: c(person(given = "Dominique", family = "Makowski", diff --git a/NEWS.md b/NEWS.md index 31ae51746..235d81d2f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -26,6 +26,12 @@ also returned in increasing order for decreasing inverse links (e.g., `Gamma("inverse")`). +* `estimate_slopes()` with `p_adjust = "esarey"` now returns the p-value of + each row at its own value of the moderator, adjusted by the same factor as + the confidence intervals. Before, the p-values were unadjusted and taken from + other values of the moderator, and some were `NA` when the output had more + than 10 rows. + # modelbased 0.19.0 ## Breaking Changes diff --git a/R/p_adjust.R b/R/p_adjust.R index ccfc82b65..9aa83aaba 100644 --- a/R/p_adjust.R +++ b/R/p_adjust.R @@ -470,13 +470,10 @@ x$conf.low <- x$estimate - tcrit * x$std.error x$conf.high <- x$estimate + tcrit * x$std.error - # update p-values - we need to ensure that length of "statisic" matches number - # of rows, so we pick just as many values from "statistic" as required - range_mod <- floor((1:nrow(x)) * (nrow(x) / 100) * length(range_sequence)) - if (length(range_sequence) == length(statistic)) { - statistic <- statistic[range_mod] - # update p-values - x$p.value <- 2 * stats::pt(abs(statistic), df = dof, lower.tail = FALSE) - } + # update p-values, using the statistic of each row. They are scaled by the + # same factor as the significance level, so that p < 1 - ci exactly when the + # confidence interval excludes zero + p_rows <- 2 * stats::pt(abs(x$estimate / x$std.error), df = dof, lower.tail = FALSE) + x$p.value <- pmin(1, p_rows / multipliers[i]) x } diff --git a/tests/testthat/test-estimate_slopes.R b/tests/testthat/test-estimate_slopes.R index 98feacb57..3ee8db161 100644 --- a/tests/testthat/test-estimate_slopes.R +++ b/tests/testthat/test-estimate_slopes.R @@ -213,7 +213,7 @@ test_that("estimate_slopes, johnson-neyman p-adjust", { ) expect_equal( out$p, - c(0.03438, 0.14813, 0.50393, 0.83427, 0.24689, 0.03326, 0.00219, 9e-05, 0, 0), + c(0.01624, 0.09023, 0.44097, 1, 1, 0.19092, 0.0123, 0.00041, 1e-05, 0), tolerance = 1e-2 ) @@ -244,6 +244,26 @@ test_that("estimate_slopes, johnson-neyman p-adjust", { }) +test_that("estimate_slopes, esarey p-values belong to their rows", { + data(iris) + model <- lm(Sepal.Width ~ Petal.Width * Petal.Length, data = iris) + for (n in c(10, 20)) { + unadjusted <- estimate_slopes(model, "Petal.Width", by = "Petal.Length", length = n) + out <- estimate_slopes( + model, + "Petal.Width", + by = "Petal.Length", + length = n, + p_adjust = "esarey" + ) + expect_false(anyNA(out$p)) + expect_true(all(out$p >= unadjusted$p)) + # p < .05 exactly when the 95% CI excludes zero + expect_identical(out$p < 0.05, out$CI_low > 0 | out$CI_high < 0) + } +}) + + test_that("estimate_slopes, custom comparison", { data(iris) m <- lm(Sepal.Width ~ Sepal.Length * Species, data = iris)