1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
#' @title Confidence Intervals (CI)
#' @name ci.default
#'
#' @description `ci()` attempts to return confidence intervals of model parameters.
#'
#' @param x A statistical model.
#' @param ci Confidence Interval (CI) level. Default to `0.95` (`95%`).
#' @param dof Number of degrees of freedom to be used when calculating
#' confidence intervals. If `NULL` (default), the degrees of freedom are
#' retrieved by calling [`insight::get_df()`] with approximation method defined
#' in `method`. If not `NULL`, use this argument to override the default degrees
#' of freedom used to compute confidence intervals.
#' @param method Method for computing degrees of freedom for confidence
#' intervals (CI) and the related p-values. Allowed are following options (which
#' vary depending on the model class): `"residual"`, `"normal"`, `"likelihood"`,
#' `"satterthwaite"`, `"kenward"`, `"wald"`, `"profile"`, `"boot"`, `"uniroot"`,
#' `"ml1"`, `"betwithin"`, `"hdi"`, `"quantile"`, `"ci"`, `"eti"`, `"si"`,
#' `"bci"`, or `"bcai"`. See section _Confidence intervals and approximation of
#' degrees of freedom_ in [`model_parameters()`] for further details.
#' @param component Model component for which parameters should be shown. See
#' the documentation for your object's class in [`model_parameters()`] or
#' [`p_value()`] for further details, or see section _Model components_.
#' @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 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.
#' @inheritParams standard_error
#'
#' @return A data frame containing the CI bounds.
#'
#' @inheritSection model_parameters Confidence intervals and approximation of degrees of freedom
#'
#' @inheritSection model_parameters.zcpglm Model components
#'
#' @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")
#' model <- glmmTMB::glmmTMB(
#' count ~ spp + mined + (1 | site),
#' ziformula = ~mined,
#' family = poisson(),
#' data = Salamanders
#' )
#'
#' ci(model)
#' ci(model, component = "zi")
#' }
#' @export
ci.default <- function(x,
ci = 0.95,
dof = NULL,
method = NULL,
iterations = 500,
component = "all",
vcov = NULL,
vcov_args = NULL,
verbose = TRUE,
...) {
# check for valid input
.is_model_valid(x)
.ci_generic(
model = x,
ci = ci,
dof = dof,
method = method,
component = component,
vcov = vcov,
vcov_args = vcov_args,
verbose = verbose,
...
)
}
#' @export
ci.glm <- function(x,
ci = 0.95,
dof = NULL,
method = "profile",
vcov = NULL,
vcov_args = NULL,
verbose = TRUE,
...) {
method <- insight::validate_argument(
method,
c("profile", "wald", "normal", "residual")
)
# No robust vcov for profile method
if (method == "profile") {
if ((!is.null(vcov) || !is.null(vcov_args)) && isTRUE(verbose)) {
insight::format_alert(
"The `vcov` and `vcov_args` are not available with `method=\"profile\"`."
)
}
out <- lapply(ci, function(i) .ci_profiled(model = x, ci = i))
out <- do.call(rbind, out)
} else {
out <- .ci_generic(
model = x,
ci = ci,
dof = dof,
method = method,
vcov = vcov,
vcov_args = vcov_args,
verbose = verbose,
...
)
}
# Return the CI bounds as a data frame.
row.names(out) <- NULL
out
}
# helper -----------------------------------------
#' @keywords internal
.check_component <- function(m, x, verbose = TRUE) {
if (x %in% c("zi", "zero_inflated")) {
minfo <- insight::model_info(m, verbose = FALSE)
if (!isTRUE(minfo$is_zero_inflated)) {
if (isTRUE(verbose)) {
message("Model has no zero-inflation component!")
}
x <- NULL
}
}
x
}
|