diff --git a/DESCRIPTION b/DESCRIPTION index 3453536..e11acf4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -20,6 +20,7 @@ Imports: dplyr, ggplot2, gtsummary (>= 2.1.0), + propensity (>= 0.0.0.9000), purrr, rlang, smd, @@ -43,4 +44,5 @@ LazyData: true Roxygen: list(markdown = TRUE) RoxygenNote: 7.3.2 Remotes: - r-causal/tidysmd + r-causal/tidysmd, + r-causal/propensity diff --git a/R/geom_qq2.R b/R/geom_qq2.R index b1f210a..13cc30e 100644 --- a/R/geom_qq2.R +++ b/R/geom_qq2.R @@ -53,13 +53,8 @@ #' geom_abline(intercept = 0, slope = 1, linetype = "dashed") #' #' # Compare multiple weights using long format -#' # TODO: Remove vec_data() workaround once propensity implements vctrs methods -#' # Extract numeric data from psw objects first -#' nhefs_for_pivot <- nhefs_weights -#' nhefs_for_pivot$w_ate <- vctrs::vec_data(nhefs_weights$w_ate) -#' nhefs_for_pivot$w_att <- vctrs::vec_data(nhefs_weights$w_att) #' long_data <- tidyr::pivot_longer( -#' nhefs_for_pivot, +#' nhefs_weights, #' cols = c(w_ate, w_att), #' names_to = "weight_type", #' values_to = "weight" diff --git a/R/geom_roc.R b/R/geom_roc.R index 98ad9da..50f2666 100644 --- a/R/geom_roc.R +++ b/R/geom_roc.R @@ -24,13 +24,8 @@ #' geom_abline(intercept = 0, slope = 1, linetype = "dashed") #' #' # With grouping by weight -#' # TODO: Remove vec_data() workaround once propensity implements vctrs methods -#' # Extract numeric data from psw objects first -#' nhefs_for_pivot <- nhefs_weights -#' nhefs_for_pivot$w_ate <- vctrs::vec_data(nhefs_weights$w_ate) -#' nhefs_for_pivot$w_att <- vctrs::vec_data(nhefs_weights$w_att) #' long_data <- tidyr::pivot_longer( -#' nhefs_for_pivot, +#' nhefs_weights, #' cols = c(w_ate, w_att), #' names_to = "weight_type", #' values_to = "weight" diff --git a/R/utils-validation.R b/R/utils-validation.R index 9694aae..0117702 100644 --- a/R/utils-validation.R +++ b/R/utils-validation.R @@ -17,7 +17,7 @@ validate_weights <- function(weights, n, arg_name = "weights", call = rlang::cal if (is.null(weights)) return(invisible(weights)) # Accept both numeric vectors and psw objects from propensity package - is_valid_weights <- is.numeric(weights) || inherits(weights, "psw") + is_valid_weights <- is.numeric(weights) || propensity::is_psw(weights) if (!is_valid_weights) { abort( diff --git a/R/zzz.R b/R/zzz.R new file mode 100644 index 0000000..290b0e3 --- /dev/null +++ b/R/zzz.R @@ -0,0 +1,8 @@ +.onLoad <- function(libname, pkgname) { + # Ensure propensity's namespace is loaded so its S3 methods are registered + # This is needed for psw objects in nhefs_weights to work properly without + # users having to explicitly load propensity + loadNamespace("propensity") + + invisible() +} \ No newline at end of file diff --git a/man/geom_qq2.Rd b/man/geom_qq2.Rd index fc20ae0..671919c 100644 --- a/man/geom_qq2.Rd +++ b/man/geom_qq2.Rd @@ -84,13 +84,8 @@ ggplot(nhefs_weights, aes(sample = age, treatment = qsmk, weight = w_ate)) + geom_abline(intercept = 0, slope = 1, linetype = "dashed") # Compare multiple weights using long format -# TODO: Remove vec_data() workaround once propensity implements vctrs methods -# Extract numeric data from psw objects first -nhefs_for_pivot <- nhefs_weights -nhefs_for_pivot$w_ate <- vctrs::vec_data(nhefs_weights$w_ate) -nhefs_for_pivot$w_att <- vctrs::vec_data(nhefs_weights$w_att) long_data <- tidyr::pivot_longer( - nhefs_for_pivot, + nhefs_weights, cols = c(w_ate, w_att), names_to = "weight_type", values_to = "weight" diff --git a/man/geom_roc.Rd b/man/geom_roc.Rd index 05b7791..b943857 100644 --- a/man/geom_roc.Rd +++ b/man/geom_roc.Rd @@ -59,13 +59,8 @@ ggplot(nhefs_weights, aes(estimate = .fitted, truth = qsmk)) + geom_abline(intercept = 0, slope = 1, linetype = "dashed") # With grouping by weight -# TODO: Remove vec_data() workaround once propensity implements vctrs methods -# Extract numeric data from psw objects first -nhefs_for_pivot <- nhefs_weights -nhefs_for_pivot$w_ate <- vctrs::vec_data(nhefs_weights$w_ate) -nhefs_for_pivot$w_att <- vctrs::vec_data(nhefs_weights$w_att) long_data <- tidyr::pivot_longer( - nhefs_for_pivot, + nhefs_weights, cols = c(w_ate, w_att), names_to = "weight_type", values_to = "weight" diff --git a/tests/testthat/Rplots.pdf b/tests/testthat/Rplots.pdf index 537a271..6885885 100644 Binary files a/tests/testthat/Rplots.pdf and b/tests/testthat/Rplots.pdf differ diff --git a/tests/testthat/test-compute_balance.R b/tests/testthat/test-compute_balance.R index 037bbf8..2e67e46 100644 --- a/tests/testthat/test-compute_balance.R +++ b/tests/testthat/test-compute_balance.R @@ -1506,8 +1506,8 @@ test_that("balance functions work seamlessly with psw objects from propensity pa data(nhefs_weights) # Verify we have psw objects in the dataset - expect_true(inherits(nhefs_weights$w_cat_ate, "psw")) - expect_true(inherits(nhefs_weights$w_cat_att_none, "psw")) + expect_true(propensity::is_psw(nhefs_weights$w_cat_ate)) + expect_true(propensity::is_psw(nhefs_weights$w_cat_att_none)) # Test that balance functions work directly with psw weights result_smd <- bal_smd( diff --git a/tests/testthat/test-extract_weight_data.R b/tests/testthat/test-extract_weight_data.R index 4ba3277..2d8db65 100644 --- a/tests/testthat/test-extract_weight_data.R +++ b/tests/testthat/test-extract_weight_data.R @@ -36,7 +36,7 @@ test_that("extract_weight_data extracts data from psw objects", { expect_type(result, "double") # Should not have psw class anymore - expect_false(inherits(result, "psw")) + expect_false(propensity::is_psw(result)) }) test_that("extract_weight_data preserves values from different psw estimands", { diff --git a/tests/testthat/test-geom_qq2.R b/tests/testthat/test-geom_qq2.R index f2435d4..9e4990c 100644 --- a/tests/testthat/test-geom_qq2.R +++ b/tests/testthat/test-geom_qq2.R @@ -43,14 +43,9 @@ test_that("geom_qq2 works with weights", { }) test_that("geom_qq2 works with color aesthetic for multiple weights", { - # Create long format data - need to extract numeric data from psw columns first - # TODO: Remove vec_data() workaround once propensity implements vctrs methods - nhefs_for_pivot <- nhefs_weights - nhefs_for_pivot$w_ate <- vctrs::vec_data(nhefs_weights$w_ate) - nhefs_for_pivot$w_att <- vctrs::vec_data(nhefs_weights$w_att) - + # Create long format data long_data <- tidyr::pivot_longer( - nhefs_for_pivot, + nhefs_weights, cols = c(w_ate, w_att), names_to = "weight_type", values_to = "weight" diff --git a/tests/testthat/test-geom_roc.R b/tests/testthat/test-geom_roc.R index 54c6d16..58eb3d4 100644 --- a/tests/testthat/test-geom_roc.R +++ b/tests/testthat/test-geom_roc.R @@ -75,14 +75,9 @@ test_that("geom_roc visual regression", { ) # Multiple groups with different weights - # First create long format data - need to extract numeric data from psw columns - # TODO: Remove vec_data() workaround once propensity implements vctrs methods - nhefs_for_pivot <- nhefs_weights - nhefs_for_pivot$w_ate <- vctrs::vec_data(nhefs_weights$w_ate) - nhefs_for_pivot$w_att <- vctrs::vec_data(nhefs_weights$w_att) - + # Create long format data long_data <- tidyr::pivot_longer( - nhefs_for_pivot, + nhefs_weights, cols = c(w_ate, w_att), names_to = "weight_type", values_to = "weight"