Skip to content

Commit

Permalink
Merge pull request #845 from vincentarelbundock/issue840
Browse files Browse the repository at this point in the history
issue #840 blocks, clusters, and weights in datasummary_balance()
  • Loading branch information
vincentarelbundock authored Dec 30, 2024
2 parents 6fbd98f + f6c9fef commit 41ea52c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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)
<doi:10.18637/jss.v103.i01>.
Version: 2.2.0.6
Version: 2.2.0.7
Remotes: vincentarelbundock/tinytable
Authors@R: c(person("Vincent", "Arel-Bundock",
email = "[email protected]",
Expand Down Expand Up @@ -111,6 +111,7 @@ Suggests:
parallel,
pscl,
psych,
randomizr,
remotes,
rmarkdown,
rstanarm,
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
3 changes: 2 additions & 1 deletion R/datasummary_balance.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down
29 changes: 29 additions & 0 deletions inst/tinytest/test-datasummary_balance.R
Original file line number Diff line number Diff line change
Expand Up @@ -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])

0 comments on commit 41ea52c

Please sign in to comment.