Skip to content

Commit 290f137

Browse files
fix: strip backticks from variable names in ard_car_vif() (#336)
**What changes are proposed in this pull request?** * `ard_car_vif()` now cleans variable names by stripping backticks before pivoting they output to the ARD structure. This ensures the output matches the cleaned variable names in `gtsummary::tbl_regression()`, fixing an issue where VIF values were populated as `NA` for variables with spaces or non-syntactic names. (#335, @NourEdinDarwish) Previously, `car::vif()` returned names enclosed in backticks (e.g. `` `Marker Level` ``). Since `add_vif()` performs a `left_join` against the cleaned names from `tbl_regression()`, the join would fail. This branch introduces a call to `broom.helpers::.clean_backticks()` to ensure the ARD variable names match exactly what `gtsummary` expects. **Reference GitHub issue associated with pull request.** Fixes #335 -------------------------------------------------------------------------------- Pre-review Checklist (if item does not apply, mark is as complete) - [ ] **All** GitHub Action workflows pass with a ✅ - [ ] PR branch has pulled the most recent updates from master branch: `usethis::pr_merge_main()` - [ ] If a bug was fixed, a unit test was added. - [ ] If a new `ard_*()` function was added, it passes the ARD structural checks from `cards::check_ard_structure()`. - [ ] If a new `ard_*()` function was added, `set_cli_abort_call()` has been set. - [ ] If a new `ard_*()` function was added and it depends on another package (such as, `broom`), `is_pkg_installed("broom")` has been set in the function call and the following added to the roxygen comments: `@examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = "broom""))` - [ ] Code coverage is suitable for any new functions/features (generally, 100% coverage for new code): `devtools::test_coverage()` Reviewer Checklist (if item does not apply, mark is as complete) - [ ] If a bug was fixed, a unit test was added. - [ ] Code coverage is suitable for any new functions/features: `devtools::test_coverage()` When the branch is ready to be merged: - [ ] Update `NEWS.md` with the changes from this pull request under the heading "`# cardx (development version)`". If there is an issue associated with the pull request, reference it in parentheses at the end update (see `NEWS.md` for examples). - [ ] **All** GitHub Action workflows pass with a ✅ - [ ] Approve Pull Request - [ ] Merge the PR. Please use "Squash and merge" or "Rebase and merge". --------- Co-authored-by: Daniel Sjoberg <danield.sjoberg@gmail.com>
1 parent ba7af9e commit 290f137

5 files changed

Lines changed: 63 additions & 4 deletions

File tree

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
* Added fix to ensure `as_card` does not error after update to `cards`
44

5+
* Bug fix in `ard_car_vif()` where non-syntactic variable names (e.g. those containing spaces) were returned with backticks in the `variable` column. Since `gtsummary::tbl_regression()` stores variable names without backticks, this mismatch resulted in empty VIF columns in `gtsummary::add_vif()`. (#335, @NourEdinDarwish)
6+
57
# cardx 0.3.2
68

79
* Swapped internal use of `dplyr::case_when()` for `dplyr::recode_values()` as the former is now deprecated. (#327)

R/ard_car_vif.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
#' @rdname ard_car_vif
1515
#' @export
1616
#'
17-
#' @examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = "car"))
17+
#' @examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = c("car", "broom.helpers")))
1818
#' lm(AGE ~ ARM + SEX, data = cards::ADSL) |>
1919
#' ard_car_vif()
2020
ard_car_vif <- function(x, ...) {
2121
set_cli_abort_call()
2222

2323
# check installed packages ---------------------------------------------------
24-
check_pkg_installed("car")
24+
check_pkg_installed(c("car", "broom.helpers"))
2525

2626
# check inputs ---------------------------------------------------------------
2727
check_not_missing(x)
@@ -67,6 +67,12 @@ ard_car_vif <- function(x, ...) {
6767
# Clean-up the result to fit the ard structure through pivot
6868
vif$result <-
6969
vif$result |>
70+
dplyr::mutate(
71+
variable = broom.helpers::.clean_backticks(
72+
.data$variable,
73+
variable_names = broom.helpers::model_list_variables(x, only_variable = TRUE)
74+
)
75+
) |>
7076
tidyr::pivot_longer(
7177
cols = -c("variable"),
7278
names_to = "stat_name",

man/ard_car_vif.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/ard_car_vif.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,35 @@
2020
1 BMIBL car_vif VIF VIF 1.010522 1 NULL NULL
2121
2 EDUCLVL car_vif VIF VIF 1.010522 1 NULL NULL
2222

23+
# ard_car_vif() works with non-syntactic predictor names
24+
25+
Code
26+
as.data.frame(ard_car_vif(lm(AGE ~ `BMI Baseline` + EDUCLVL, data = df)))
27+
Output
28+
variable context stat_name stat_label stat fmt_fun warning error
29+
1 BMI Baseline car_vif VIF VIF 1.010522 1 NULL NULL
30+
2 EDUCLVL car_vif VIF VIF 1.010522 1 NULL NULL
31+
32+
# ard_car_vif() works with non-syntactic interaction-only terms
33+
34+
Code
35+
as.data.frame(ard_car_vif(lm(AGE ~ ARM + `BMI Baseline`:SEX, data = df)))
36+
Output
37+
variable context stat_name stat_label stat fmt_fun warning
38+
1 ARM car_vif GVIF GVIF 1.047141 1 NULL
39+
2 ARM car_vif df df 2 1 NULL
40+
3 ARM car_vif aGVIF Adjusted GVIF 1.011582 1 NULL
41+
4 BMI Baseline:SEX car_vif GVIF GVIF 1.047141 1 NULL
42+
5 BMI Baseline:SEX car_vif df df 2 1 NULL
43+
6 BMI Baseline:SEX car_vif aGVIF Adjusted GVIF 1.011582 1 NULL
44+
error
45+
1 NULL
46+
2 NULL
47+
3 NULL
48+
4 NULL
49+
5 NULL
50+
6 NULL
51+
2352
# ard_vif() issues friendly messaging for incorrect object passed in/can't get terms of model
2453

2554
Code

tests/testthat/test-ard_car_vif.R

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
skip_if_pkg_not_installed("car")
1+
skip_if_pkg_not_installed(c("car", "broom.helpers"))
22

33
test_that("ard_car_vif() works", {
44
expect_snapshot(
@@ -14,6 +14,28 @@ test_that("ard_car_vif() works", {
1414
)
1515
})
1616

17+
test_that("ard_car_vif() works with non-syntactic predictor names", {
18+
df <- cards::ADSL
19+
names(df)[names(df) == "BMIBL"] <- "BMI Baseline"
20+
21+
expect_snapshot(
22+
lm(AGE ~ `BMI Baseline` + EDUCLVL, data = df) |>
23+
ard_car_vif() |>
24+
as.data.frame()
25+
)
26+
})
27+
28+
test_that("ard_car_vif() works with non-syntactic interaction-only terms", {
29+
df <- cards::ADSL
30+
names(df)[names(df) == "BMIBL"] <- "BMI Baseline"
31+
32+
expect_snapshot(
33+
lm(AGE ~ ARM + `BMI Baseline`:SEX, data = df) |>
34+
ard_car_vif() |>
35+
as.data.frame()
36+
)
37+
})
38+
1739
test_that("ard_car_vif() appropriate errors are given for model with only 1 term", {
1840
expect_equal(
1941
lm(AGE ~ ARM, data = cards::ADSL) |>

0 commit comments

Comments
 (0)