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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
#' @export
model_parameters.nestedLogit <- function(model,
ci = 0.95,
ci_method = NULL,
component = "all",
bootstrap = FALSE,
iterations = 1000,
standardize = NULL,
exponentiate = FALSE,
p_adjust = NULL,
vcov = NULL,
vcov_args = NULL,
include_info = getOption("parameters_info", FALSE),
keep = NULL,
drop = NULL,
verbose = TRUE,
...) {
dots <- list(...)
# set default
if (is.null(ci_method)) {
if (isTRUE(bootstrap)) {
ci_method <- "quantile"
} else if (!is.null(vcov) || !is.null(vcov_args)) {
ci_method <- "wald"
} else {
ci_method <- "profile"
}
}
# "component" might be set to "conditional", when called from "compare_parameters()"
# set to "all" here.
if (identical(component, "conditional")) {
component <- "all"
}
# profiled CIs may take a long time to compute, so we warn the user about it
if (any(unlist(insight::n_obs(model)) > 1e4) && identical(ci_method, "profile")) {
insight::format_alert(
"Profiled confidence intervals may take longer time to compute.",
"Use `ci_method=\"wald\"` for faster computation of CIs."
)
}
# tell user that profiled CIs don't respect vcov-args
if (identical(ci_method, "profile") && (!is.null(vcov) || !is.null(vcov_args)) && isTRUE(verbose)) {
insight::format_alert(
"When `ci_method=\"profile\"`, `vcov` only modifies standard errors, test-statistic and p-values, but not confidence intervals.", # nolint
"Use `ci_method=\"wald\"` to return confidence intervals based on robust standard errors."
)
}
fun_args <- list(
model = model,
ci = ci,
ci_method = ci_method,
component = component,
bootstrap = bootstrap,
iterations = iterations,
merge_by = c("Parameter", "Response", "Component"),
standardize = standardize,
exponentiate = exponentiate,
p_adjust = p_adjust,
include_info = include_info,
keep_parameters = keep,
drop_parameters = drop,
vcov = vcov,
vcov_args = vcov_args
)
fun_args <- c(fun_args, dots)
out <- do.call(".model_parameters_generic", fun_args)
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(model))
out
}
#' @export
standard_error.nestedLogit <- function(model,
component = "all",
vcov = NULL,
vcov_args = NULL,
verbose = TRUE,
...) {
dots <- list(...)
se <- NULL
# vcov: matrix
if (is.matrix(vcov)) {
se <- sqrt(diag(vcov))
}
# vcov: function which returns a matrix
if (is.function(vcov)) {
fun_args <- c(list(model), vcov_args, dots)
se <- .safe(sqrt(diag(do.call("vcov", fun_args))))
}
# vcov: character
if (is.character(vcov)) {
.vcov <- insight::get_varcov(
model,
component = component,
vcov = vcov,
vcov_args = vcov_args,
verbose = verbose,
...
)
se <- unlist(lapply(.vcov, function(i) sqrt(diag(i))), use.names = FALSE)
}
# classical se from summary()
if (is.null(se)) {
se <- as.vector(as.data.frame(do.call(rbind, lapply(model$models, function(i) {
stats::coef(summary(i))
})))[, "Std. Error"])
}
# classical se from get_varcov()
if (is.null(se)) {
.vcov <- insight::get_varcov(
model,
component = component,
verbose = verbose,
...
)
se <- unlist(lapply(.vcov, function(i) sqrt(diag(i))), use.names = FALSE)
}
params <- insight::get_parameters(model, component = component)
.data_frame(
Parameter = params$Parameter,
SE = as.vector(se),
Response = params$Response,
Component = params$Component
)
}
#' @export
p_value.nestedLogit <- function(model,
dof = NULL,
method = NULL,
component = "all",
vcov = NULL,
vcov_args = NULL,
verbose = TRUE,
...) {
if (is.null(vcov)) {
p <- as.vector(as.data.frame(do.call(rbind, lapply(model$models, function(i) {
stats::coef(summary(i))
})))[, "Pr(>|z|)"])
} else {
p <- p_value.default(
model,
dof = dof,
method = method,
component = component,
vcov = vcov,
vcov_args = vcov_args,
verbose = verbose,
...
)[["p"]]
}
params <- insight::get_parameters(model, component = component)
.data_frame(
Parameter = params$Parameter,
p = p,
Response = params$Response,
Component = params$Component
)
}
#' @export
ci.nestedLogit <- function(x,
ci = 0.95,
dof = NULL,
method = "profile",
component = "all",
vcov = NULL,
vcov_args = NULL,
verbose = TRUE,
...) {
out <- lapply(
x$models,
ci,
dof = dof,
method = method,
vcov = vcov,
vcov_args = vcov_args,
verbose = verbose,
...
)
for (i in names(out)) {
out[[i]]$Component <- i
}
out <- do.call(rbind, out)
row.names(out) <- NULL
if (!is.null(component) && !identical(component, "all")) {
comp <- intersect(names(x$models), component)
if (!length(comp) && verbose) {
insight::format_alert(
paste0(
"No matching model found. Possible values for `component` are ",
toString(paste0("\"", names(x$models), "\"")),
"."
)
)
} else {
out <- out[out$Component %in% component, ]
}
}
params <- insight::get_parameters(x, component = component)
out$Response <- params$Response
out[c("Parameter", "CI", "CI_low", "CI_high", "Response", "Component")]
}
#' @export
simulate_model.nestedLogit <- function(model, iterations = 1000, ...) {
if (is.null(iterations)) iterations <- 1000
params <- insight::get_parameters(model, component = "all", verbose = FALSE)
varcov <- insight::get_varcov(model, component = "all", verbose = FALSE, ...)
out <- lapply(unique(params$Component), function(i) {
pars <- params[params$Component == i, ]
betas <- stats::setNames(pars$Estimate, pars$Parameter)
d <- as.data.frame(.mvrnorm(n = iterations, mu = betas, Sigma = varcov[[i]]))
d$Component <- i
d
})
out <- do.call(rbind, out)
class(out) <- c("parameters_simulate_model", class(out))
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(model))
out
}
#' @export
simulate_parameters.nestedLogit <- function(model,
iterations = 1000,
centrality = "median",
ci = 0.95,
ci_method = "quantile",
test = "p-value",
...) {
sim_data <- simulate_model(model, iterations = iterations, ...)
out <- lapply(unique(sim_data$Component), function(i) {
pars <- sim_data[sim_data$Component == i, ]
d <- .summary_bootstrap(
data = pars,
test = test,
centrality = centrality,
ci = ci,
ci_method = ci_method,
...
)
d$Component <- i
d
})
out <- do.call(rbind, out)
class(out) <- c("parameters_simulate", "see_parameters_simulate", class(out))
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(model))
attr(out, "iterations") <- iterations
attr(out, "ci") <- ci
attr(out, "ci_method") <- ci_method
attr(out, "centrality") <- centrality
attr(out, "simulated") <- TRUE
out
}
|