|
| 1 | +#' Pool a scalar estimate across imputations |
| 2 | +#' |
| 3 | +#' Applies Rubin's rules to combine scalar estimates and their |
| 4 | +#' within-imputation variance estimates. |
| 5 | +#' |
| 6 | +#' @details For `m` imputations, the pooled estimate is the mean of `q`. The |
| 7 | +#' pooled variance combines the mean within-imputation variance with the |
| 8 | +#' between-imputation variance, using the usual `(1 + 1 / m)` multiplier. |
| 9 | +#' The returned standard error is the square root of the pooled variance. |
| 10 | +#' |
| 11 | +#' @param q (`numeric`) |
| 12 | +#' Vector of scalar estimates, with one value for each imputed dataset. |
| 13 | +#' @param u (`numeric`) |
| 14 | +#' Vector of within-imputation variance estimates corresponding to `q`. |
| 15 | +#' |
| 16 | +#' @return A named `list` with the pooled estimate (`est`), standard error |
| 17 | +#' (`se`), variance (`var`), and number of imputations (`m`). |
| 18 | +#' |
| 19 | +#' @seealso [pool_z_stat()] |
| 20 | +#' @export |
| 21 | +#' |
| 22 | +#' @examples |
| 23 | +#' pool_rubin_scalar( |
| 24 | +#' q = c(0.20, 0.25, 0.15), |
| 25 | +#' u = c(0.01, 0.016, 0.012) |
| 26 | +#' ) |
| 27 | +pool_rubin_scalar <- function(q, u) { |
| 28 | + # q: vector of estimates |
| 29 | + # u: vector of within-imputation variances |
| 30 | + m <- length(q) |
| 31 | + qbar <- mean(q, na.rm = TRUE) |
| 32 | + ubar <- mean(u, na.rm = TRUE) |
| 33 | + b <- stats::var(q, na.rm = TRUE) |
| 34 | + tvar <- ubar + (1 + 1 / m) * b |
| 35 | + se <- sqrt(tvar) |
| 36 | + list(est = qbar, se = se, var = tvar, m = m) |
| 37 | +} |
| 38 | + |
| 39 | +#' Pool z statistics across imputations |
| 40 | +#' |
| 41 | +#' Combines z statistics from multiple imputed datasets and calculates a |
| 42 | +#' two-sided p-value for the pooled statistic. |
| 43 | +#' |
| 44 | +#' @details The within-imputation variance of a z statistic is approximately 1. |
| 45 | +#' The between-imputation variance is pooled using the same |
| 46 | +#' `(1 + 1 / m)` multiplier as [pool_rubin_scalar()]. The returned p-value is |
| 47 | +#' calculated from the standard normal distribution. |
| 48 | +#' |
| 49 | +#' @param z_stat_vals (`numeric`) |
| 50 | +#' Vector of z statistics, with one value for each imputed dataset. |
| 51 | +#' |
| 52 | +#' @return A named `list` with the pooled z statistic (`z`) and its two-sided |
| 53 | +#' p-value (`p`). |
| 54 | +#' |
| 55 | +#' @seealso [pool_rubin_scalar()] |
| 56 | +#' @export |
| 57 | +#' |
| 58 | +#' @examples |
| 59 | +#' pool_z_stat(c(1.1, 1.3, 1.2)) |
| 60 | +pool_z_stat <- function(z_stat_vals) { |
| 61 | + m <- length(z_stat_vals) |
| 62 | + qbar <- mean(z_stat_vals, na.rm = TRUE) |
| 63 | + b <- stats::var(z_stat_vals, na.rm = TRUE) |
| 64 | + ubar <- 1 # approx var(z) ~ 1 because it is a z statistic |
| 65 | + tvar <- ubar + (1 + 1 / m) * b |
| 66 | + z_pool <- qbar / sqrt(tvar) |
| 67 | + p_pool <- 2 * stats::pnorm(abs(z_pool), lower.tail = FALSE) |
| 68 | + list(z = z_pool, p = p_pool) |
| 69 | +} |
0 commit comments