Skip to content

Commit

Permalink
p_significance() for model_parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Sep 16, 2024
1 parent a068851 commit 6a6389e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: parameters
Title: Processing of Model Parameters
Version: 0.22.2.9
Version: 0.22.2.10
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ S3method(p_significance,lm)
S3method(p_significance,lme)
S3method(p_significance,merMod)
S3method(p_significance,mixed)
S3method(p_significance,parameters_model)
S3method(p_significance,rma)
S3method(p_significance,svyglm)
S3method(p_significance,wbm)
Expand Down
26 changes: 24 additions & 2 deletions R/p_significance.R
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,27 @@ p_significance.lm <- function(x, threshold = "default", ci = 0.95, verbose = TRU

.posterior_ci <- function(x, ci, ...) {
# first, we need CIs
out <- ci(x, ci = ci, ...)
dof <- .safe(insight::get_df(x, type = "wald"), Inf)
if (inherits(x, "parameters_model")) {
# for model_parameters objects, directly extract CIs
out <- as.data.frame(x)[intersect(
c("Parameter", "CI_low", "CI_high", "Component", "Effects"),
colnames(x)
)]
ci <- attributes(x)$ci
# and extract degrees of freedom
df_column <- grep("(df|df_error)", colnames(x))
if (length(df_column) > 0) {
dof <- unique(x[[df_column]])
if (length(dof) > 1) {
dof <- Inf
}
} else {
dof <- Inf
}
} else {
out <- ci(x, ci = ci, ...)
dof <- .safe(insight::get_df(x, type = "wald"), Inf)
}
# we now iterate all confidence intervals and create an approximate normal
# distribution that covers the CI-range.
posterior <- as.data.frame(lapply(seq_len(nrow(out)), function(i) {
Expand Down Expand Up @@ -281,3 +300,6 @@ p_significance.zeroinfl <- p_significance.lm

#' @export
p_significance.rma <- p_significance.lm

#' @export
p_significance.parameters_model <- p_significance.lm
5 changes: 5 additions & 0 deletions tests/testthat/test-p_significance.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ test_that("p_significance", {
expect_named(x, c("Parameter", "CI", "CI_low", "CI_high", "ps"))
expect_snapshot(print(x))

mp <- model_parameters(m)
set.seed(123)
x2 <- p_significance(mp)
expect_equal(x$ps, x2$ps, tolerance = 1e-4)

set.seed(123)
x <- p_significance(m, ci = 0.8)
expect_equal(x$ps, c(1, 0.3983, 0.9959, 0.6188, 0), tolerance = 1e-3)
Expand Down

0 comments on commit 6a6389e

Please sign in to comment.