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

Add vcov option to docs and examples #1014

Merged
merged 3 commits into from
Sep 16, 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
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.8
Version: 0.22.2.9
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
* `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.

* Revision / enhancement of some documentation.

# parameters 0.22.2
Expand Down
16 changes: 14 additions & 2 deletions R/2_ci.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,25 @@
#' @param iterations The number of bootstrap replicates. Only applies to models
#' of class `merMod` when `method=boot`.
#' @param verbose Toggle warnings and messages.
#' @param ... Additional arguments
#' @param ... Additional arguments passed down to the underlying functions.
#' E.g., arguments like `vcov` or `vcov_args` can be used to compute confidence
#' intervals using a specific variance-covariance matrix for the standard
#' errors.
#'
#' @return A data frame containing the CI bounds.
#'
#' @inheritSection model_parameters Confidence intervals and approximation of degrees of freedom
#'
#' @examplesIf require("glmmTMB")
#' @examplesIf require("glmmTMB") && requireNamespace("sandwich")
#' data(qol_cancer)
#' model <- lm(QoL ~ time + age + education, data = qol_cancer)
#'
#' # regular confidence intervals
#' ci(model)
#'
#' # using heteroscedasticity-robust standard errors
#' ci(model, vcov = "HC3")
#'
#' \donttest{
#' library(parameters)
#' data(Salamanders, package = "glmmTMB")
Expand Down
20 changes: 13 additions & 7 deletions R/equivalence_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
#' 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.
#' @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..
#' @inheritParams model_parameters.merMod
#' @inheritParams p_value
#'
Expand Down Expand Up @@ -218,13 +221,16 @@
#' Synthese 200, 89 (2022). \doi{10.1007/s11229-022-03560-x}
#'
#' @return A data frame.
#' @examples
#' @examplesIf requireNamespace("sandwich")
#' data(qol_cancer)
#' model <- lm(QoL ~ time + age + education, data = qol_cancer)
#'
#' # default rule
#' equivalence_test(model)
#'
#' # using heteroscedasticity-robust standard errors
#' equivalence_test(model, vcov = "HC3")
#'
#' # conditional equivalence test
#' equivalence_test(model, rule = "cet")
#'
Expand Down Expand Up @@ -504,14 +510,14 @@

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

params <- conf_int <- .ci_generic(x, ci = ci)
params <- conf_int <- .ci_generic(x, ci = ci, ...)
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), ...)
conf_int2 <- as.data.frame(t(conf_int2[, c("CI_low", "CI_high")]))


Expand Down Expand Up @@ -544,7 +550,7 @@

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

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

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

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

Check warning on line 641 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=641,col=39,[function_argument_linter] Arguments without defaults should come before arguments with defaults.
ci_narrow,

Check warning on line 642 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=642,col=39,[function_argument_linter] Arguments without defaults should come before arguments with defaults.
range_rope,

Check warning on line 643 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=643,col=39,[function_argument_linter] Arguments without defaults should come before arguments with defaults.
rule,

Check warning on line 644 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=644,col=39,[function_argument_linter] Arguments without defaults should come before arguments with defaults.
dof = Inf,
verbose) {

Check warning on line 646 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=646,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 @@ -740,7 +746,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 749 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=749,col=39,[function_argument_linter] Arguments without defaults should come before arguments with defaults.

Check warning on line 749 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=749,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 @@ -750,7 +756,7 @@
}


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

Check warning on line 759 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=759,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 @@ -786,7 +792,7 @@
}


.add_p_to_equitest <- function(model, ci, range) {
.add_p_to_equitest <- function(model, ci, range, ...) {
tryCatch(
{
params <- insight::get_parameters(model)
Expand All @@ -798,7 +804,7 @@
params$mu <- params$Estimate * -1

# se
se <- standard_error(model)
se <- standard_error(model, ...)

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
9 changes: 7 additions & 2 deletions R/p_direction.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ bayestestR::p_direction
#' @param x A statistical model.
#' @inheritParams bayestestR::p_direction
#' @inheritParams model_parameters.default
#' @param ... Arguments passed to other methods, e.g. `ci()`.
#' @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.
#'
#' @seealso See also [`equivalence_test()`], [`p_function()`] and
#' [`p_significance()`] for functions related to checking effect existence and
Expand Down Expand Up @@ -70,11 +72,14 @@ bayestestR::p_direction
#'
#' @return A data frame.
#'
#' @examplesIf requireNamespace("bayestestR") && require("see", quietly = TRUE)
#' @examplesIf requireNamespace("bayestestR") && require("see", quietly = TRUE) && requireNamespace("sandwich")
#' data(qol_cancer)
#' model <- lm(QoL ~ time + age + education, data = qol_cancer)
#' p_direction(model)
#'
#' # based on heteroscedasticity-robust standard errors
#' p_direction(model, vcov = "HC3")
#'
#' result <- p_direction(model)
#' plot(result)
#' @export
Expand Down
8 changes: 5 additions & 3 deletions R/p_function.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
#' (i.e. assumptions are taken as given). The unconditional interpretation (B),
#' however, questions all these assumptions.
#'
#' \if{html}{\cr \figure{unconditional_interpretation.png}{options: alt="Conditional versus unconditional interpretations of P-values"} \cr}

Check warning on line 65 in R/p_function.R

View workflow job for this annotation

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

file=R/p_function.R,line=65,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 140 characters.
#'
#' "Emphasizing unconditional interpretations helps avoid overconfident and
#' misleading inferences in light of uncertainties about the assumptions used
Expand Down Expand Up @@ -119,9 +119,11 @@
#' whereas values values far from the observed point estimate (where _p_
#' approaches 0) are least compatible with the data and model assumptions
#' (_Schweder and Hjort 2016, pp. 60-61; Amrhein and Greenland 2022_). In this
#' regards, _p_-values are are statements about confidence/compatibility, not
#' probability per se, but still the interpretation of _p_-values might be
#' guided using [`bayestestR::p_to_pd()`]
#' regards, _p_-values are statements about _confidence_ or _compatibility_ and
#' can be considered as _epistemic probability_ - "not necessarily of the
#' hypothesis being true, but of it _possibly_ being true" (_Schweder 2018_).
#' Hence, the interpretation of _p_-values might be guided using
#' [`bayestestR::p_to_pd()`]
#'
#' ## Compatibility intervals - is their interpretation conditional or not?
#'
Expand Down
10 changes: 7 additions & 3 deletions R/p_significance.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ 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()`.
#' @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.
#'
#' @seealso For more details, see [`bayestestR::p_significance()`]. See also
#' [`equivalence_test()`], [`p_function()`] and [`bayestestR::p_direction()`]
Expand Down Expand Up @@ -126,14 +128,16 @@ bayestestR::p_significance
#' intervals and the values for practical significance. Higher values indicate
#' more practical significance (upper bound is one).
#'
#' @examplesIf requireNamespace("bayestestR") && packageVersion("bayestestR") > "0.14.0"
#' @examplesIf requireNamespace("bayestestR") && packageVersion("bayestestR") > "0.14.0" && requireNamespace("sandwich")
#' data(qol_cancer)
#' model <- lm(QoL ~ time + age + education, data = qol_cancer)
#'
#' p_significance(model)
#' p_significance(model, threshold = c(-0.5, 1.5))
#'
#' # plot method
#' # based on heteroscedasticity-robust standard errors
#' p_significance(model, vcov = "HC3")
#'
#' if (require("see", quietly = TRUE)) {
#' result <- p_significance(model)
#' plot(result)
Expand Down
16 changes: 14 additions & 2 deletions man/ci.default.Rd

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

5 changes: 4 additions & 1 deletion man/cluster_analysis.Rd

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

10 changes: 9 additions & 1 deletion man/equivalence_test.lm.Rd

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

9 changes: 7 additions & 2 deletions man/p_direction.lm.Rd

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

8 changes: 5 additions & 3 deletions man/p_function.Rd

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

10 changes: 7 additions & 3 deletions man/p_significance.lm.Rd

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

5 changes: 4 additions & 1 deletion man/p_value_betwithin.Rd

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

5 changes: 4 additions & 1 deletion man/p_value_ml1.Rd

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

5 changes: 4 additions & 1 deletion man/p_value_satterthwaite.Rd

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

Loading
Loading