-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ard_car_vif.R
106 lines (99 loc) · 2.87 KB
/
ard_car_vif.R
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
#' Regression VIF ARD
#'
#' @description
#' Function takes a regression model object and returns the variance inflation factor (VIF)
#' using [`car::vif()`] and converts it to a ARD structure
#'
#' @param x regression model object
#' See car::vif() for details
#'
#' @param ... arguments passed to `car::vif(...)`
#'
#' @return data frame
#' @name ard_car_vif
#' @rdname ard_car_vif
#' @export
#'
#' @examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = "car"))
#' lm(AGE ~ ARM + SEX, data = cards::ADSL) |>
#' ard_car_vif()
ard_car_vif <- function(x, ...) {
set_cli_abort_call()
# check installed packages ---------------------------------------------------
check_pkg_installed("car")
# check inputs ---------------------------------------------------------------
check_not_missing(x)
vif <- cards::eval_capture_conditions(car::vif(x, ...))
# if vif failed, set result as NULL, error will be kept through eval_capture_conditions()
if (is.null(vif$result)) {
# try to capture variable names from `terms()`
lst_terms <- cards::eval_capture_conditions(attr(stats::terms(x), "term.labels"))
# we cannot get variable names, error out
if (!is.null(lst_terms[["error"]])) {
cli::cli_abort(
c("There was an error running {.fun car::vif}. See below.", x = vif[["error"]]),
call = get_cli_abort_call()
)
}
vif$result <- dplyr::tibble(
variable = lst_terms[["result"]],
VIF = list(NULL),
GVIF = list(NULL),
aGVIF = list(NULL),
df = list(NULL)
)
}
# if VIF is returned
else if (!is.matrix(vif$result)) {
vif$result <- dplyr::tibble(variable = names(vif$result), VIF = vif$result)
}
# if Generalized VIF is returned
else if (is.matrix(vif$result)) {
vif$result <-
vif$result |>
as.data.frame() %>%
dplyr::mutate(., variable = rownames(.), .before = 1L) |>
dplyr::rename(
aGVIF = "GVIF^(1/(2*Df))",
df = "Df"
) |>
dplyr::tibble()
}
# Clean-up the result to fit the ard structure through pivot
vif$result <-
vif$result |>
tidyr::pivot_longer(
cols = -c("variable"),
names_to = "stat_name",
values_to = "stat"
) |>
dplyr::mutate(
context = "car_vif",
stat = as.list(.data$stat),
stat_label = ifelse(
.data$stat_name == "aGVIF",
"Adjusted GVIF",
.data$stat_name
),
fmt_fn = map(
.data$stat,
function(.x) {
# styler: off
if (is.integer(.x)) return(0L)
if (is.numeric(.x)) return(1L)
# styler: on
NULL
}
)
)
# Bind the results and possible warning/errors together
vif_return <- dplyr::tibble(
vif$result,
warning = vif["warning"],
error = vif["error"]
)
# Clean up return object
vif_return |>
cards::as_card() |>
cards::tidy_ard_column_order()
}