Skip to content

Commit

Permalink
fix for weights
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Aug 12, 2023
1 parent 995fb79 commit 605596a
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 6 deletions.
21 changes: 15 additions & 6 deletions R/means_by_group.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
87 changes: 87 additions & 0 deletions tests/testthat/_snaps/means_by_group.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions tests/testthat/_snaps/windows/means_by_group.md
Original file line number Diff line number Diff line change
@@ -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

8 changes: 8 additions & 0 deletions tests/testthat/test-means_by_group.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})

0 comments on commit 605596a

Please sign in to comment.