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

Make vcov and vcov_args arguments explicit #1017

Merged
merged 2 commits into from
Sep 24, 2024
Merged
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
3 changes: 2 additions & 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.11
Version: 0.22.2.12
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down Expand Up @@ -194,6 +194,7 @@ Suggests:
RcppEigen,
rmarkdown,
rms,
rstan,
rstanarm,
sandwich,
see (>= 0.8.1),
Expand Down
9 changes: 3 additions & 6 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
way to test the direction of the effect, which formerly was already (and still
is) possible with `pd = TRUE` in `model_parameters()`.

* `p_function()` gets a `vcov` and `vcov_args` argument to compute robust
standard errors for the confidence curves.

* Functions `p_significance()` and `equivalence_test()` now pass arguments
`vcov` and `vcov_args` to `p_value()` and `ci()`, hence, tests can be based
on robust standard errors.
* `p_function()`, `p_significance()` and `equivalence_test()` get a `vcov` and
`vcov_args` argument, so that results can be based on robust standard errors
and confidence intervals.

* `equivalence_test()` and `p_significance()` work with objects returned by
`model_parameters()`.
Expand Down
43 changes: 32 additions & 11 deletions R/equivalence_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
#' See [`?ggeffects::test_predictions`](https://strengejacke.github.io/ggeffects/reference/test_predictions.html)
#' for details.
#' @param verbose Toggle warnings and messages.
#' @param ... Arguments passed to or from other methods, e.g. `ci()`. Arguments
#' like `vcov` or `vcov_args` can be used to compute confidence intervals or
#' p-values using a specific variance-covariance matrix for the standard
#' errors..
#' @param ... Arguments passed to or from other methods.
#' @inheritParams model_parameters.merMod
#' @inheritParams p_value
#'
Expand Down Expand Up @@ -244,10 +241,21 @@
range = "default",
ci = 0.95,
rule = "classic",
vcov = NULL,
vcov_args = NULL,
verbose = TRUE,
...) {
rule <- match.arg(tolower(rule), choices = c("bayes", "classic", "cet"))
out <- .equivalence_test_frequentist(x, range, ci, rule, verbose, ...)
out <- .equivalence_test_frequentist(
x,
range = range,
ci = ci,
rule = rule,
vcov = vcov,
vcov_args = vcov_args,
verbose,
...
)

if (is.null(attr(out, "pretty_names", exact = TRUE))) {
attr(out, "pretty_names") <- format_parameters(x)
Expand Down Expand Up @@ -308,6 +316,8 @@
ci = 0.95,
rule = "classic",
effects = c("fixed", "random"),
vcov = NULL,
vcov_args = NULL,
verbose = TRUE,
...) {
# ==== argument matching ====
Expand All @@ -319,7 +329,16 @@
# ==== equivalent testing for fixed or random effects ====

if (effects == "fixed") {
out <- .equivalence_test_frequentist(x, range, ci, rule, verbose, ...)
out <- .equivalence_test_frequentist(
x,
range = range,
ci = ci,
rule = rule,
vcov = vcov,
vcov_args = vcov_args,
verbose,
...
)
} else {
out <- .equivalence_test_frequentist_random(x, range, ci, rule, verbose, ...)
}
Expand Down Expand Up @@ -496,6 +515,8 @@
range = "default",
ci = 0.95,
rule = "classic",
vcov = NULL,
vcov_args = NULL,
verbose = TRUE,
...) {
# ==== define rope range ====
Expand All @@ -522,14 +543,14 @@

# ==== requested confidence intervals ====

params <- conf_int <- .ci_generic(x, ci = ci, ...)
params <- conf_int <- .ci_generic(x, ci = ci, vcov = vcov, vcov_args = vcov_args, ...)
conf_int <- as.data.frame(t(conf_int[, c("CI_low", "CI_high")]))


# ==== the "narrower" intervals (1-2*alpha) for CET-rules. ====

alpha <- 1 - ci
conf_int2 <- .ci_generic(x, ci = (ci - alpha), ...)
conf_int2 <- .ci_generic(x, ci = (ci - alpha), vcov = vcov, vcov_args = vcov_args, ...)
conf_int2 <- as.data.frame(t(conf_int2[, c("CI_low", "CI_high")]))


Expand Down Expand Up @@ -562,7 +583,7 @@

# ==== (adjusted) p-values for tests ====

out$p <- .add_p_to_equitest(x, ci, range, ...)
out$p <- .add_p_to_equitest(x, ci, range, vcov = vcov, vcov_args = vcov_args, ...)

attr(out, "rope") <- range
out
Expand Down Expand Up @@ -650,12 +671,12 @@

#' @keywords internal
.equivalence_test_numeric <- function(ci = 0.95,
ci_wide,

Check warning on line 674 in R/equivalence_test.R

View workflow job for this annotation

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

file=R/equivalence_test.R,line=674,col=39,[function_argument_linter] Arguments without defaults should come before arguments with defaults.
ci_narrow,

Check warning on line 675 in R/equivalence_test.R

View workflow job for this annotation

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

file=R/equivalence_test.R,line=675,col=39,[function_argument_linter] Arguments without defaults should come before arguments with defaults.
range_rope,

Check warning on line 676 in R/equivalence_test.R

View workflow job for this annotation

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

file=R/equivalence_test.R,line=676,col=39,[function_argument_linter] Arguments without defaults should come before arguments with defaults.
rule,

Check warning on line 677 in R/equivalence_test.R

View workflow job for this annotation

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

file=R/equivalence_test.R,line=677,col=39,[function_argument_linter] Arguments without defaults should come before arguments with defaults.
dof = Inf,
verbose) {

Check warning on line 679 in R/equivalence_test.R

View workflow job for this annotation

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

file=R/equivalence_test.R,line=679,col=39,[function_argument_linter] Arguments without defaults should come before arguments with defaults.
final_ci <- NULL

# ==== HDI+ROPE decision rule, by Kruschke ====
Expand Down Expand Up @@ -758,7 +779,7 @@
# same range / limits as the confidence interval, thus indeed representing a
# normally distributed confidence interval. We then calculate the probability
# mass of this interval that is inside the ROPE.
.rope_coverage <- function(ci = 0.95, range_rope, ci_range, dof = Inf) {

Check warning on line 782 in R/equivalence_test.R

View workflow job for this annotation

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

file=R/equivalence_test.R,line=782,col=39,[function_argument_linter] Arguments without defaults should come before arguments with defaults.

Check warning on line 782 in R/equivalence_test.R

View workflow job for this annotation

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

file=R/equivalence_test.R,line=782,col=51,[function_argument_linter] Arguments without defaults should come before arguments with defaults.
out <- .generate_posterior_from_ci(ci, ci_range, dof = dof)
# compare: ci_range and range(out)
# The SGPV refers to the proportion of the confidence interval inside the
Expand All @@ -768,7 +789,7 @@
}


.generate_posterior_from_ci <- function(ci = 0.95, ci_range, dof = Inf, precision = 10000) {

Check warning on line 792 in R/equivalence_test.R

View workflow job for this annotation

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

file=R/equivalence_test.R,line=792,col=52,[function_argument_linter] Arguments without defaults should come before arguments with defaults.
# this function creates an approximate normal distribution that covers the
# CI-range, i.e. we "simulate" a posterior distribution from a frequentist CI

Expand Down Expand Up @@ -804,7 +825,7 @@
}


.add_p_to_equitest <- function(model, ci, range, ...) {
.add_p_to_equitest <- function(model, ci, range, vcov = NULL, vcov_args = NULL, ...) {
tryCatch(
{
params <- insight::get_parameters(model)
Expand All @@ -816,7 +837,7 @@
params$mu <- params$Estimate * -1

# se
se <- standard_error(model, ...)
se <- standard_error(model, vcov = vcov, vcov_args = vcov_args, ...)

stats::pt((range[1] - params$mu) / se$SE, df = dof, lower.tail = TRUE) +
stats::pt((range[2] - params$mu) / se$SE, df = dof, lower.tail = FALSE)
Expand Down
4 changes: 3 additions & 1 deletion R/p_direction.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ p_direction.lm <- function(x,
ci = 0.95,
method = "direct",
null = 0,
vcov = NULL,
vcov_args = NULL,
...) {
# generate normal distribution based on CI range
result <- .posterior_ci(x, ci, ...)
result <- .posterior_ci(x, ci, vcov = vcov, vcov_args = vcov_args, ...)

# copy
out <- result$out
Expand Down
18 changes: 11 additions & 7 deletions R/p_significance.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ bayestestR::p_significance
#' @inheritParams bayestestR::p_significance
#' @inheritParams model_parameters.default
#' @param verbose Toggle warnings and messages.
#' @param ... Arguments passed to other methods, e.g. `ci()`. Arguments like
#' `vcov` or `vcov_args` can be used to compute confidence intervals using a
#' specific variance-covariance matrix for the standard errors.
#' @param ... Arguments passed to other methods.
#'
#' @seealso For more details, see [`bayestestR::p_significance()`]. See also
#' [`equivalence_test()`], [`p_function()`] and [`bayestestR::p_direction()`]
Expand Down Expand Up @@ -143,9 +141,15 @@ bayestestR::p_significance
#' plot(result)
#' }
#' @export
p_significance.lm <- function(x, threshold = "default", ci = 0.95, verbose = TRUE, ...) {
p_significance.lm <- function(x,
threshold = "default",
ci = 0.95,
vcov = NULL,
vcov_args = NULL,
verbose = TRUE,
...) {
# generate normal distribution based on CI range
result <- .posterior_ci(x, ci, ...)
result <- .posterior_ci(x, ci, vcov = vcov, vcov_args = vcov_args, ...)

# copy
out <- result$out
Expand Down Expand Up @@ -203,7 +207,7 @@ p_significance.lm <- function(x, threshold = "default", ci = 0.95, verbose = TRU

# helper ----------------------------------------------------------------------

.posterior_ci <- function(x, ci, ...) {
.posterior_ci <- function(x, ci, vcov = NULL, vcov_args = NULL, ...) {
# first, we need CIs
if (inherits(x, "parameters_model")) {
# for model_parameters objects, directly extract CIs
Expand All @@ -223,7 +227,7 @@ p_significance.lm <- function(x, threshold = "default", ci = 0.95, verbose = TRU
dof <- Inf
}
} else {
out <- ci(x, ci = ci, ...)
out <- ci(x, ci = ci, vcov = vcov, vcov_args = vcov_args, ...)
dof <- .safe(insight::get_df(x, type = "wald"), Inf)
}
# we now iterate all confidence intervals and create an approximate normal
Expand Down
5 changes: 1 addition & 4 deletions man/cluster_analysis.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 30 additions & 4 deletions man/equivalence_test.lm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 34 additions & 1 deletion man/p_direction.lm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 35 additions & 4 deletions man/p_significance.lm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/testthat/test-brms.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
skip_if_offline()
skip_if_not_installed("withr")
skip_if_not_installed("brms")
skip_if_not_installed("rstan")

withr::with_options(
list(parameters_warning_exponentiate = TRUE),
{

Check warning on line 12 in tests/testthat/test-brms.R

View workflow job for this annotation

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

file=tests/testthat/test-brms.R,line=12,col=3,[unnecessary_nesting_linter] Reduce the nesting of this statement by removing the braces {}.
test_that("mp, footer exp", {
m <- suppressWarnings(insight::download_model("brms_bernoulli_1"))
out <- parameters::model_parameters(m, exponentiate = FALSE)
Expand Down
Loading
Loading