Skip to content

Commit

Permalink
address note
Browse files Browse the repository at this point in the history
  • Loading branch information
zdz2101 committed Feb 27, 2024
1 parent a62e07b commit 392dd60
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 36 deletions.
1 change: 1 addition & 0 deletions R/ard_regression.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ ard_regression.default <- function(x, tidy_fun = broom.helpers::tidy_with_broom_
tidyr::pivot_longer(
cols = -c("variable", "variable_level"),
names_to = "stat_name",
# noticed this while trying to fix ard_vif() does this need renaming to "statistic" ?
values_to = "stat"
) |>
dplyr::filter(map_lgl(.data$stat, Negate(is.na))) |>
Expand Down
43 changes: 21 additions & 22 deletions R/ard_vif.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,19 @@ ard_vif <- function(x) {
# check inputs ---------------------------------------------------------------
check_not_missing(x, "model")

vif_return <- .vif_to_tibble(x)
dplyr::tibble(
vif_return$result,
warning = vif_return["warning"],
error = vif_return["error"]
)
}


# put VIF results in data frame -- borrowed from gtsummary
.vif_to_tibble <- function(x) {
temp <- x
vif <- car::vif(x) |>
cards::eval_capture_conditions()

# if VIF is returned
# if vif failed, set result as NULL, error will be kept through eval_capture_conditions()
if (is.null(vif$result)) {
vif$result <- dplyr::tibble(variable = names(temp$coefficients)[-1], VIF = list(NULL))
} else if (!is.matrix(vif$result)) {
}
# 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
}
# if Generalized VIF is returned
else if (is.matrix(vif$result)) {
vif$result <-
vif$result |>
Expand All @@ -54,6 +46,7 @@ ard_vif <- function(x) {
dplyr::tibble()
}

# Clean-up the result to fit the ard structure through pivot
vif$result <-
vif$result |>
tidyr::pivot_longer(
Expand All @@ -62,17 +55,23 @@ ard_vif <- function(x) {
values_to = "statistic"
) |>
dplyr::mutate(
.after = "variable",
context = "vif"
) |>
dplyr::mutate(
.after = "stat_name",
context = "vif",
stat_label = ifelse(
stat_name == "aGVIF",
.data$stat_name == "aGVIF",
"Adjusted GVIF",
stat_name
.data$stat_name
)
)

return(vif)
# 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::tidy_ard_column_order() %>%
{structure(., class = c("card", class(.)))} # styler: off
}
32 changes: 18 additions & 14 deletions tests/testthat/_snaps/ard_vif.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@

Code
ard_vif(lm(AGE ~ ARM + SEX, data = cards::ADSL))
Message
{cards} data frame: 6 x 7
Output
# A tibble: 6 x 7
variable context stat_name stat_label statistic warning error
<chr> <chr> <chr> <chr> <dbl> <named list> <named list>
1 ARM vif GVIF GVIF 1.02 <NULL> <NULL>
2 ARM vif df df 2 <NULL> <NULL>
3 ARM vif aGVIF Adjusted GVIF 1.00 <NULL> <NULL>
4 SEX vif GVIF GVIF 1.02 <NULL> <NULL>
5 SEX vif df df 1 <NULL> <NULL>
6 SEX vif aGVIF Adjusted GVIF 1.01 <NULL> <NULL>
variable context stat_name stat_label statistic
1 ARM vif GVIF GVIF 1.015675
2 ARM vif df df 2.000000
3 ARM vif aGVIF Adjusted… 1.003896
4 SEX vif GVIF GVIF 1.015675
5 SEX vif df df 1.000000
6 SEX vif aGVIF Adjusted… 1.007807
Message
i 2 more variables: warning, error

# ard_vif() appropriate errors are given for model with only 1 term

Code
ard_vif(lm(AGE ~ ARM, data = cards::ADSL))
Message
{cards} data frame: 2 x 7
Output
# A tibble: 2 x 7
variable context stat_name stat_label statistic warning error
<chr> <chr> <chr> <chr> <list> <named l> <nam>
1 ARMXanomeline High Dose vif VIF VIF <NULL> <NULL> <chr>
2 ARMXanomeline Low Dose vif VIF VIF <NULL> <NULL> <chr>
variable context stat_name stat_label error statistic
1 ARMXanomeline High Dose vif VIF VIF model co… NULL
2 ARMXanomeline Low Dose vif VIF VIF model co… NULL
Message
i 1 more variable: warning

0 comments on commit 392dd60

Please sign in to comment.