Skip to contents

Each domain's estimate against a reference domain: the difference, its standard error from the design (the covariance between the two estimates included, which is what a pair of separate intervals leaves out), a confidence interval and a two-sided p-value from the normal approximation. Use it to test whether two regions or groups differ, rather than checking whether their separate intervals overlap: overlapping intervals do not mean no difference, and domains of one survey are not independent.

Usage

svycmp(
  des,
  data,
  y,
  by = NULL,
  stat = c("mean", "total", "proportion", "ratio"),
  den = NULL,
  lvls = NULL,
  ref = NULL,
  conflvl = 0.95
)

Arguments

des

A svydes() built on data.

data

The data frame the design was built on.

y

Character scalar, the analysis column: numeric for a mean, total or ratio; any type for a proportion (each distinct value is a category).

by

The domain columns to compare across.

stat

"mean", "total", "proportion" or "ratio" (of y to den).

den

For a ratio, the denominator column.

lvls

For a proportion, the categories to report (default all observed).

ref

The reference domain, as its label (for several by columns, the labels joined with " / "); a number is matched as the label it prints as. Default NULL: the first domain.

conflvl

Numeric scalar in (0, 1), the confidence level; default 0.95.

Value

A data frame of class c("rmbl_svy_compare", "data.frame"), one row per non-reference domain (and category): domain, level (for a proportion only), numeric estimate (the domain's), reference (the reference domain's), difference, se, lower, upper and p_value. Attributes ref, stat, y and conflvl.

Details

The estimates come from svyest() with Wald intervals. For domain \(i\) and reference \(0\) the difference is \(d = \hat\theta_i - \hat\theta_0\) with \(SE(d) = \sqrt{V_{ii} + V_{00} - 2V_{i0}}\) from the design covariance; the interval is \(d \pm z\,SE(d)\) and the p-value \(2\Phi(-|d|/SE(d))\), both on the normal distribution (there is no df argument). For a proportion every category is compared separately. No adjustment for multiple comparisons is made.

Errors: by missing or empty; fewer than two domains; a ref that is not a single domain label present in the data; and every error of svyest().

Examples

set.seed(1)
d <- data.frame(region = rep(c("north", "south", "east"), length.out = 150),
                smoker = rbinom(150, 1, 0.3), w = runif(150, 50, 150))
des <- svydes(d, w = "w")
svycmp(des, d, "smoker", by = "region", stat = "proportion",
               lvls = "1", ref = "north")
#> ╭── Survey proportion of smoker against north ───────────────────────────╮
#> │   Variable          smoker                                             │
#> │   Reference domain  north                                              │
#> │   Confidence        95%                                                │
#> │                                                                        │
#> │   domain  level  estimate  reference  difference       se              │
#> │   ------  -----  --------  ---------  ----------  -------              │
#> │   east    1        0.2538     0.3701     -0.1164  0.09485              │
#> │   south   1        0.2352     0.3701     -0.1350  0.09464              │
#> │                                                                        │
#> │   domain  95% CI             p_value                                   │
#> │   ------  -----------------  -------                                   │
#> │   east    [-0.3023, 0.0695]    0.220                                   │
#> │   south   [-0.3205, 0.0505]    0.154                                   │
#> │                                                                        │
#> │   Not distinguishable from 0 (interval includes it): east (p = 0.220)  │
#> │   and south (p = 0.154).                                               │
#> │                                                                        │
#> │   Each difference is a domain's estimate minus the reference domain's, │
#> │   with the design's covariance between the two taken into account.     │
#> ╰────────────────────────────────────────────────────────────────────────╯

# means, against the default (first) domain, at 90 percent
d$drinks <- rpois(150, 3)
svycmp(des, d, "drinks", by = "region", conflvl = 0.90)
#> ╭── Survey mean of drinks against east ──────────────────────────────────╮
#> │   Variable          drinks                                             │
#> │   Reference domain  east                                               │
#> │   Confidence        90%                                                │
#> │                                                                        │
#> │   domain  estimate  reference  difference      se  90% CI              │
#> │   ------  --------  ---------  ----------  ------  -----------------   │
#> │   north      3.066      3.361     -0.2952  0.3947  [-0.9444, 0.3539]   │
#> │   south      3.083      3.361     -0.2779  0.3811  [-0.9048, 0.3490]   │
#> │                                                                        │
#> │   domain  p_value                                                      │
#> │   ------  -------                                                      │
#> │   north     0.454                                                      │
#> │   south     0.466                                                      │
#> │                                                                        │
#> │   Not distinguishable from 0 (interval includes it): north (p = 0.454) │
#> │   and south (p = 0.466).                                               │
#> │                                                                        │
#> │   Each difference is a domain's estimate minus the reference domain's, │
#> │   with the design's covariance between the two taken into account.     │
#> ╰────────────────────────────────────────────────────────────────────────╯

# the reference must be one of the domains
try(svycmp(des, d, "drinks", by = "region", ref = "west"))
#> Error : `ref` is not one of the domains: east, north, south