From f6c9fef884cee4b939883b78447c162dd45e4148 Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Mon, 30 Dec 2024 09:02:13 -0500 Subject: [PATCH] issue #840 blocks, clusters, and weights in datasummary_balance() --- DESCRIPTION | 3 ++- NEWS.md | 1 + R/datasummary_balance.R | 3 ++- inst/tinytest/test-datasummary_balance.R | 29 ++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 087a1b63..0aa4ed34 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -9,7 +9,7 @@ Description: Create beautiful and customizable tables to summarize several RTF, JPG, or PNG. Tables can easily be embedded in 'Rmarkdown' or 'knitr' dynamic documents. Details can be found in Arel-Bundock (2022) . -Version: 2.2.0.6 +Version: 2.2.0.7 Remotes: vincentarelbundock/tinytable Authors@R: c(person("Vincent", "Arel-Bundock", email = "vincent.arel-bundock@umontreal.ca", @@ -111,6 +111,7 @@ Suggests: parallel, pscl, psych, + randomizr, remotes, rmarkdown, rstanarm, diff --git a/NEWS.md b/NEWS.md index 13847a0a..775d688e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,6 +8,7 @@ Bugs: * Bad horizontal rule placement with `add_rows`. Thanks to @pyoungblood for Issue #813. * `shape` creates bad columns when model names include spaces. Thanks to @daSilva5 for report #816. * Center align GOF in dot-aligned columns. Thanks to @svraka for report #827. +* Weights, blocks, and clusters ignored by `datasummary_balance()`. Thanks to @kleuveld for report #840. New features: diff --git a/R/datasummary_balance.R b/R/datasummary_balance.R index 13056f97..36e4c3ee 100644 --- a/R/datasummary_balance.R +++ b/R/datasummary_balance.R @@ -393,8 +393,9 @@ sanitize_datasummary_balance_data <- function(formula, data) { lhs <- stats::update(formula, ".~1") lhs <- all.vars(lhs) lhs <- setdiff(lhs, ".") + bonus <- c("clusters", "weights", "blocks") if (length(lhs) > 0) { - cols <- intersect(c(lhs, rhs), colnames(data)) + cols <- intersect(c(lhs, rhs, bonus), colnames(data)) if (length(cols) > 1) { data <- data[, cols, drop = FALSE] } diff --git a/inst/tinytest/test-datasummary_balance.R b/inst/tinytest/test-datasummary_balance.R index 8047050b..b98af4e7 100644 --- a/inst/tinytest/test-datasummary_balance.R +++ b/inst/tinytest/test-datasummary_balance.R @@ -327,3 +327,32 @@ expect_snapshot_print(tab, "datasummary_balance-issue711") tab <- datasummary_balance(mpg + hp ~ am, data = mtcars, output = "dataframe") expect_equal(nrow(tab), 2) + +# Issue #840 +requiet("randomizr") +requiet("estimatr") +requiet("tibble") + +dat <- tibble::tibble( + x = rnorm(n = 1000), + clusters = randomizr::simple_ra(N = 1000, num_arms = 50), + Z = randomizr::cluster_ra(clusters = clusters), + y = x + Z * 5, + weights = ifelse(y > 5, 0, 1)) # making sure the weights actually make a difference +dat <- data.frame(dat) +tab <- datasummary_balance(y ~ Z, data = dat, fmt = 4, output = "data.frame") + +wm <- weighted.mean(dat$y[dat$Z == 1], dat$weights[dat$Z == 1]) +wm <- sprintf("%.4f", wm) +expect_equivalent(wm, tab[, 4]) +wm <- weighted.mean(dat$y[dat$Z == 0], dat$weights[dat$Z == 0]) +wm <- sprintf("%.4f", wm) +expect_equivalent(wm, tab[, 2]) + +# and these should be the differences +dinm <- difference_in_means(y ~ Z, + clusters = clusters, + weights = weights, + data = dat) +dinm <- sprintf("%.4f", coef(dinm)) +expect_equivalent(dinm, tab[, 6])