From 605596a12b87152ce6d6d4d1b4f2ca0b0b485700 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 12 Aug 2023 13:48:31 +0200 Subject: [PATCH] fix for weights --- R/means_by_group.R | 21 +++-- tests/testthat/_snaps/means_by_group.md | 87 +++++++++++++++++++ .../testthat/_snaps/windows/means_by_group.md | 34 ++++++++ tests/testthat/test-means_by_group.R | 8 ++ 4 files changed, 144 insertions(+), 6 deletions(-) create mode 100644 tests/testthat/_snaps/windows/means_by_group.md diff --git a/R/means_by_group.R b/R/means_by_group.R index 8cbce934b..ad32699b5 100644 --- a/R/means_by_group.R +++ b/R/means_by_group.R @@ -177,13 +177,19 @@ means_by_group.data.frame <- function(x, } # summary table data - out <- stats::aggregate(data["x"], data["group"], weighted_mean, weights = data$weights) - out_sd <- stats::aggregate(data["x"], data["group"], weighted_sd, weights = data$weights) - out_weights <- stats::aggregate(data["weights"], data["group"], sum) + groups <- split(data$x, data$group) + group_weights <- split(data$weights, data$group) + out <- do.call(rbind, Map(function(x, w) { + data.frame( + Mean = weighted_mean(x, weights = w), + SD = weighted_sd(x, weights = w), + N = round(sum(w)), + stringsAsFactors = FALSE + ) + }, groups, group_weights)) - colnames(out) <- c("Category", "Mean") - out$N <- round(out_weights[[2]]) - out$SD <- out_sd[[2]] + # add group names + out$Category <- levels(data$group) out$p <- out$CI_high <- out$CI_low <- NA # p-values of contrast-means @@ -201,6 +207,9 @@ means_by_group.data.frame <- function(x, out$p <- summary_table$p.value } + # reorder columns + out <- out[c("Category", "Mean", "N", "SD", "CI_low", "CI_high", "p")] + # finally, add total-row out <- rbind( out, diff --git a/tests/testthat/_snaps/means_by_group.md b/tests/testthat/_snaps/means_by_group.md index 26c042d86..78a43d8b4 100644 --- a/tests/testthat/_snaps/means_by_group.md +++ b/tests/testthat/_snaps/means_by_group.md @@ -49,6 +49,93 @@ Anova: R2=0.186; adj.R2=0.160; F=7.098; p<.001 +--- + + Code + means_by_group(efc, c("neg_c_7", "c12hour"), "e42dep") + Output + # Mean of Negative impact with 7 items by elder's dependency + + Category | Mean | N | SD | 95% CI | p + ----------------------------------------------------------------- + independent | 11.00 | 2 | 0.00 | [ 5.00, 17.00] | 0.567 + slightly dependent | 10.00 | 4 | 3.16 | [ 5.76, 14.24] | 0.296 + moderately dependent | 13.71 | 28 | 3.14 | [12.11, 15.32] | 0.296 + severely dependent | 14.67 | 60 | 4.78 | [13.57, 15.76] | 0.108 + Total | 14.11 | 94 | 4.34 | | + + Anova: R2=0.063; adj.R2=0.032; F=2.009; p=0.118 + + # Mean of average number of hours of care per week by elder's dependency + + Category | Mean | N | SD | 95% CI | p + ---------------------------------------------------------------------- + independent | 17.00 | 2 | 11.31 | [-68.46, 102.46] | 0.573 + slightly dependent | 34.25 | 4 | 29.97 | [-26.18, 94.68] | 0.626 + moderately dependent | 52.75 | 28 | 51.83 | [ 29.91, 75.59] | > .999 + severely dependent | 106.97 | 63 | 65.88 | [ 91.74, 122.19] | 0.001 + Total | 86.46 | 97 | 66.40 | | + + Anova: R2=0.186; adj.R2=0.160; F=7.098; p<.001 + +--- + + Code + means_by_group(efc, c("neg_c_7", "c12hour"), "e42dep", ci = NA) + Output + # Mean of Negative impact with 7 items by elder's dependency + + Category | Mean | N | SD | p + ------------------------------------------------ + independent | 11.00 | 2 | 0.00 | 0.567 + slightly dependent | 10.00 | 4 | 3.16 | 0.296 + moderately dependent | 13.71 | 28 | 3.14 | 0.296 + severely dependent | 14.67 | 60 | 4.78 | 0.108 + Total | 14.11 | 94 | 4.34 | + + Anova: R2=0.063; adj.R2=0.032; F=2.009; p=0.118 + + # Mean of average number of hours of care per week by elder's dependency + + Category | Mean | N | SD | p + --------------------------------------------------- + independent | 17.00 | 2 | 11.31 | 0.573 + slightly dependent | 34.25 | 4 | 29.97 | 0.626 + moderately dependent | 52.75 | 28 | 51.83 | > .999 + severely dependent | 106.97 | 63 | 65.88 | 0.001 + Total | 86.46 | 97 | 66.40 | + + Anova: R2=0.186; adj.R2=0.160; F=7.098; p<.001 + +--- + + Code + means_by_group(efc, c("neg_c_7", "c12hour"), "e42dep", ci = 0.99) + Output + # Mean of Negative impact with 7 items by elder's dependency + + Category | Mean | N | SD | 99% CI | p + ----------------------------------------------------------------- + independent | 11.00 | 2 | 0.00 | [ 3.05, 18.95] | 0.567 + slightly dependent | 10.00 | 4 | 3.16 | [ 4.38, 15.62] | 0.296 + moderately dependent | 13.71 | 28 | 3.14 | [11.59, 15.84] | 0.296 + severely dependent | 14.67 | 60 | 4.78 | [13.22, 16.12] | 0.108 + Total | 14.11 | 94 | 4.34 | | + + Anova: R2=0.063; adj.R2=0.032; F=2.009; p=0.118 + + # Mean of average number of hours of care per week by elder's dependency + + Category | Mean | N | SD | 99% CI | p + ---------------------------------------------------------------------- + independent | 17.00 | 2 | 11.31 | [-96.17, 130.17] | 0.573 + slightly dependent | 34.25 | 4 | 29.97 | [-45.77, 114.27] | 0.626 + moderately dependent | 52.75 | 28 | 51.83 | [ 22.50, 83.00] | > .999 + severely dependent | 106.97 | 63 | 65.88 | [ 86.80, 127.13] | 0.001 + Total | 86.46 | 97 | 66.40 | | + + Anova: R2=0.186; adj.R2=0.160; F=7.098; p<.001 + --- Code diff --git a/tests/testthat/_snaps/windows/means_by_group.md b/tests/testthat/_snaps/windows/means_by_group.md new file mode 100644 index 000000000..198069da9 --- /dev/null +++ b/tests/testthat/_snaps/windows/means_by_group.md @@ -0,0 +1,34 @@ +# meany_by_group, weighted + + Code + means_by_group(efc, "c12hour", "e42dep", weights = "weight") + Output + # Mean of average number of hours of care per week by elder's dependency + + Category | Mean | N | SD | 95% CI | p + ---------------------------------------------------------------------- + independent | 16.92 | 3 | 11.31 | [-60.82, 94.66] | 0.486 + slightly dependent | 33.56 | 4 | 29.75 | [-26.93, 94.05] | 0.593 + moderately dependent | 52.74 | 26 | 54.44 | [ 28.71, 76.76] | 0.996 + severely dependent | 108.08 | 67 | 65.40 | [ 93.01, 123.16] | < .001 + Total | 88.11 | 97 | 67.01 | | + + Anova: R2=0.191; adj.R2=0.165; F=7.329; p<.001 + +--- + + Code + means_by_group(efc, "c12hour", "e42dep", weights = "weight", ci = NA) + Output + # Mean of average number of hours of care per week by elder's dependency + + Category | Mean | N | SD | p + --------------------------------------------------- + independent | 16.92 | 3 | 11.31 | 0.486 + slightly dependent | 33.56 | 4 | 29.75 | 0.593 + moderately dependent | 52.74 | 26 | 54.44 | 0.996 + severely dependent | 108.08 | 67 | 65.40 | < .001 + Total | 88.11 | 97 | 67.01 | + + Anova: R2=0.191; adj.R2=0.165; F=7.329; p<.001 + diff --git a/tests/testthat/test-means_by_group.R b/tests/testthat/test-means_by_group.R index 943215450..7b36ffccb 100644 --- a/tests/testthat/test-means_by_group.R +++ b/tests/testthat/test-means_by_group.R @@ -9,3 +9,11 @@ test_that("meany_by_group", { expect_snapshot(means_by_group(efc$c12hour, efc$e42dep)) expect_snapshot(means_by_group(efc$c12hour, efc$e42dep, ci = NA)) }) + +test_that("meany_by_group, weighted", { + data(efc) + set.seed(123) + efc$weight <- abs(rnorm(n = nrow(efc), mean = 1, sd = 0.5)) + expect_snapshot(means_by_group(efc, "c12hour", "e42dep", weights = "weight"), variant = "windows") + expect_snapshot(means_by_group(efc, "c12hour", "e42dep", weights = "weight", ci = NA), variant = "windows") +})