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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
# model_parameters -----------------------------------------
#' @export
model_parameters.svyglm <- function(model,
ci = 0.95,
ci_method = "wald",
standardize = NULL,
exponentiate = FALSE,
p_adjust = NULL,
include_info = getOption("parameters_info", FALSE),
keep = NULL,
drop = NULL,
verbose = TRUE,
...) {
if (insight::n_obs(model) > 1e4 && ci_method == "likelihood") {
insight::format_alert(
"Likelihood confidence intervals may take longer time to compute. Use 'ci_method=\"wald\"' for faster computation of CIs." # nolint
)
}
# validation check, warn if unsupported argument is used.
dot_args <- .check_dots(
dots = list(...),
not_allowed = c("vcov", "vcov_args", "bootstrap"),
class(model)[1],
verbose = verbose
)
fun_args <- list(
model,
ci = ci,
ci_method = ci_method,
standardize = standardize,
exponentiate = exponentiate,
p_adjust = p_adjust,
keep_parameters = keep,
drop_parameters = drop,
include_info = include_info,
verbose = verbose
)
fun_args <- c(fun_args, dot_args)
out <- do.call(".model_parameters_generic", fun_args)
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(model))
out
}
# simulate_model -----------------------------------------
#' @export
simulate_model.svyglm.nb <- simulate_model.default
#' @export
simulate_model.svyglm.zip <- simulate_model.default
# standard erors -----------------------------------------
#' @export
standard_error.svyglm.nb <- function(model, ...) {
if (!isNamespaceLoaded("survey")) {
requireNamespace("survey", quietly = TRUE)
}
se <- sqrt(diag(stats::vcov(model, stderr = "robust")))
.data_frame(
Parameter = .remove_backticks_from_string(names(se)),
SE = as.vector(se)
)
}
#' @export
standard_error.svyglm.zip <- standard_error.svyglm.nb
#' @export
standard_error.svyglm <- function(model, ...) {
vc <- insight::get_varcov(model)
.data_frame(
Parameter = .remove_backticks_from_string(row.names(vc)),
SE = as.vector(sqrt(diag(vc)))
)
}
#' @export
standard_error.svyolr <- standard_error.svyglm
# confidence intervals -----------------------------------
#' @export
ci.svyglm <- function(x, ci = 0.95, method = "wald", ...) {
method <- match.arg(method, choices = c("wald", "residual", "normal", "likelihood"))
if (method == "likelihood") {
out <- lapply(ci, function(i) .ci_likelihood(model = x, ci = i))
out <- do.call(rbind, out)
} else {
out <- .ci_generic(model = x, ci = ci, method = method, ...)
}
row.names(out) <- NULL
out
}
#' @export
ci.svyolr <- ci.svyglm
# p values -----------------------------------------------
## TODO how to calculate p when ci-method is "likelihood"?
#' @export
p_value.svyglm <- function(model, verbose = TRUE, ...) {
statistic <- insight::get_statistic(model)
dof <- insight::get_df(model, type = "residual")
p <- 2 * stats::pt(-abs(statistic$Statistic), df = dof)
.data_frame(
Parameter = statistic$Parameter,
p = as.vector(p)
)
}
#' @export
p_value.svyolr <- p_value.svyglm
#' @export
p_value.svyglm.nb <- function(model, ...) {
if (!isNamespaceLoaded("survey")) {
requireNamespace("survey", quietly = TRUE)
}
est <- stats::coef(model)
se <- sqrt(diag(stats::vcov(model, stderr = "robust")))
p <- 2 * stats::pt(abs(est / se), df = insight::get_df(model, type = "wald"), lower.tail = FALSE)
.data_frame(
Parameter = .remove_backticks_from_string(names(p)),
p = as.vector(p)
)
}
#' @export
p_value.svyglm.zip <- p_value.svyglm.nb
# helper --------------------
.ci_likelihood <- function(model, ci) {
glm_ci <- tryCatch(
{
out <- as.data.frame(stats::confint(model, level = ci, method = "likelihood"), stringsAsFactors = FALSE)
names(out) <- c("CI_low", "CI_high")
out$CI <- ci
out$Parameter <- insight::get_parameters(model, effects = "fixed", component = "conditional")$Parameter
out <- out[c("Parameter", "CI", "CI_low", "CI_high")]
rownames(out) <- NULL
out
},
error = function(e) {
NULL
}
)
if (is.null(glm_ci)) {
glm_ci <- .ci_generic(model, ci = ci)
}
glm_ci
}
|