Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #10 add ard_vif() #61

Merged
merged 31 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
59c80d4
feat: #10 basic vif draft up
zdz2101 Feb 22, 2024
d336a0f
good draft
zdz2101 Feb 23, 2024
b1d589c
Merge branch 'main' into 10-add-variance-inflation-factor-ard
zdz2101 Feb 23, 2024
4178c5a
edit pkgdown
zdz2101 Feb 23, 2024
6c89045
update packages on description and man page
zdz2101 Feb 23, 2024
50ff2ec
spellcheck
zdz2101 Feb 23, 2024
6ad5b37
modfiy closer to desired structure
zdz2101 Feb 26, 2024
a62e07b
styler
zdz2101 Feb 26, 2024
392dd60
address note
zdz2101 Feb 27, 2024
75f13b5
styler
zdz2101 Feb 27, 2024
2c26294
adopt feedback and add quick qeuestion
zdz2101 Feb 28, 2024
3d97500
remove unnecesary comment
zdz2101 Feb 28, 2024
be9b0bc
Merge branch 'main' into 10-add-variance-inflation-factor-ard
ddsjoberg Mar 1, 2024
dadac1c
Merge branch 'main' into 10-add-variance-inflation-factor-ard
ddsjoberg Mar 2, 2024
5c225ad
add ... passthrough
zdz2101 Mar 4, 2024
8058c24
Merge branch 'main' into 10-add-variance-inflation-factor-ard
ddsjoberg Mar 4, 2024
2547d99
Merge branch 'main' into 10-add-variance-inflation-factor-ard
ddsjoberg Mar 5, 2024
fbd4d14
forgot to add elipsis in the function formals
zdz2101 Mar 5, 2024
769bd48
run document()
zdz2101 Mar 5, 2024
3cdd708
feat: #10 add fmt_fn
zdz2101 Mar 6, 2024
ac42fde
Merge branch 'main' into 10-add-variance-inflation-factor-ard
zdz2101 Mar 6, 2024
6e1ad06
Merge branch 'main' into 10-add-variance-inflation-factor-ard
ddsjoberg Mar 6, 2024
096c5a2
Merge branch 'main' into 10-add-variance-inflation-factor-ard
ddsjoberg Mar 6, 2024
8374765
feat: #10 adopt feedback
zdz2101 Mar 8, 2024
fa11f4e
address note
zdz2101 Mar 8, 2024
fe5bc9a
get test coverage to 100%
zdz2101 Mar 8, 2024
8982c6d
Merge branch 'main' into 10-add-variance-inflation-factor-ard
ddsjoberg Mar 8, 2024
7ae1f02
adopt feedback and add example of said messaing
zdz2101 Mar 8, 2024
919d2ec
better way
zdz2101 Mar 8, 2024
f08eca3
styler
zdz2101 Mar 8, 2024
d48fb3c
tryCatch() re-work
ddsjoberg Mar 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Imports:
Suggests:
broom (>= 1.0.5),
broom.helpers (>= 1.13.0),
car (>= 3.0-11),
spelling,
testthat (>= 3.2.0),
withr
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export(ard_paired_wilcoxtest)
export(ard_proportion_ci)
export(ard_regression)
export(ard_ttest)
export(ard_vif)
export(ard_wilcoxtest)
export(contains)
export(ends_with)
Expand Down
79 changes: 79 additions & 0 deletions R/ard_vif.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#' Regression VIF ARD
#'
#' Function takes a regression model object and converts it to a ARD
ddsjoberg marked this conversation as resolved.
Show resolved Hide resolved
#' structure using the `broom.helpers` package.
#'
#' @param x regression model object
#' See car::vif() for details
#'
#' Do I need to add "type" or "..." as pass-through?
ddsjoberg marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @return data frame
#' @name ard_vif
#' @rdname ard_vif
#' @export
#'
#' @examples
#' lm(AGE ~ ARM + SEX, data = cards::ADSL) |>
#' ard_vif()
ard_vif <- function(x) {
# check installed packages ---------------------------------------------------
cards::check_pkg_installed("broom.helpers", reference_pkg = "cards")
ddsjoberg marked this conversation as resolved.
Show resolved Hide resolved

# check inputs ---------------------------------------------------------------
check_not_missing(x, "model")

temp <- x
ddsjoberg marked this conversation as resolved.
Show resolved Hide resolved
vif <- car::vif(x) |>
cards::eval_capture_conditions()

# 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))
ddsjoberg marked this conversation as resolved.
Show resolved Hide resolved
ddsjoberg marked this conversation as resolved.
Show resolved Hide resolved
}
# 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 = "vif",
stat_label = ifelse(
.data$stat_name == "aGVIF",
"Adjusted GVIF",
.data$stat_name
)
)

# 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
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ reference:
- contents:
- ard_proportion_ci
- ard_regression
- ard_vif

- title: "Helpers"
- contents:
Expand Down
2 changes: 2 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Lifecycle
McNemar's
Newcombe
Su
VIF
XG
Xin
agresti
Expand All @@ -18,6 +19,7 @@ funder
jeffreys
pearson
strat
vif
wald
waldcc
wilson
Expand Down
25 changes: 25 additions & 0 deletions man/ard_vif.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions tests/testthat/_snaps/ard_vif.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# ard_vif() works
ddsjoberg marked this conversation as resolved.
Show resolved Hide resolved

Code
ard_vif(lm(AGE ~ ARM + SEX, data = cards::ADSL))
Message
{cards} data frame: 6 x 7
Output
variable context stat_name stat_label stat
1 ARM vif GVIF GVIF 1.016
2 ARM vif df df 2
3 ARM vif aGVIF Adjusted… 1.004
4 SEX vif GVIF GVIF 1.016
5 SEX vif df df 1
6 SEX vif aGVIF Adjusted… 1.008
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
variable context stat_name stat_label stat error
1 ARMXanomeline High Dose vif VIF VIF model co…
2 ARMXanomeline Low Dose vif VIF VIF model co…
Message
i 1 more variable: warning

21 changes: 21 additions & 0 deletions tests/testthat/test-ard_vif.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
test_that("ard_vif() works", {
expect_snapshot(
lm(AGE ~ ARM + SEX, data = cards::ADSL) |>
ard_vif()
)
})

test_that("ard_vif() appropriate errors are given for model with only 1 term", {
expect_snapshot(
lm(AGE ~ ARM, data = cards::ADSL) |>
ard_vif()
)
zdz2101 marked this conversation as resolved.
Show resolved Hide resolved
expect_equal(
lm(AGE ~ ARM, data = cards::ADSL) |>
ard_vif() |>
dplyr::select(error) |>
unlist() |>
unique(),
"model contains fewer than 2 terms"
)
})
Loading