Skip to contents

Estimate design-based variance components (B, W, icc, k) from a tbl_sample, for planning the next round with svyplan::n_cluster(). The method extracts everything the estimation needs from the sample's design columns, applying two conventions that are easy to get wrong by hand:

Usage

varcomp(x, ...)

# S3 method for class 'tbl_sample'
varcomp(x, ..., strata = NULL, estimator = c("expansion", "ratio"))

Arguments

x

A tbl_sample with at least one executed clustered stage.

...

The outcome as a one-sided formula, e.g. varcomp(x, ~y), mirroring the survey.design method. The expression is evaluated on the sample, so ~log(y) analyzes the log of y, and every variable it names must be a column of the sample. Nothing else belongs here: strata follows the ... and so is matched exactly, so a near miss such as strat or a second positional formula is reported rather than silently dropped.

strata

Optional one-sided formula over columns of the sample, such as ~region or ~interaction(region, urban_rural), to estimate per-stratum components by, when the first stage was not stratified. Stage-1 design strata are picked up automatically. Combining them with strata is an error, because per-stratum components crossed with design strata are ambiguous.

estimator

The estimator the components describe, passed to svyplan::varcomp(): "expansion" (default) for an estimated total, or "ratio" for a mean or a proportion, an estimated total over an estimated number of units. The two differ only when PSU sizes vary and PSUs are not drawn proportional to size.

Value

A svyplan_varcomp object. Pass it as icc to svyplan::n_cluster(). Use as.data.frame() to export either the one-row unstratified components or the per-stratum component table.

Details

  • The components are weighted by the per-stage weights below stage 1, never the compound .weight, whose stage-1 factor would overstate every cluster size. Under one clustered stage that is their product. Under two, svyplan takes the second clustered stage's weight and the element stage's (one when the elements are taken whole) as two columns, which its element-stage correction reads apart.

  • For an unequal-probability first stage, per-PSU shares are derived from the stage-1 weights and normalized to sum to 1 over the sampled PSUs (per stratum when the first stage is stratified). For an equal-probability first stage the SRS path applies. See the Details of svyplan::varcomp().

The decomposition covers the clustered stages plus the sample rows as elements: one clustered stage gives 2-stage components, two give 3-stage. Deeper designs are refused. Estimate the top stages and fold the rest into a design effect. With-replacement (WR/PMR) stages treat each draw as an independent unit, keyed by the .draw_k column as in as_svydesign(): a cluster hit twice enters the decomposition twice, with its share counted per draw. Two-phase samples are refused: the nested decomposition does not model phase sampling. So is a sample whose reserve is not settled (samplyr_error_reserve_unsettled), whose weights describe the whole list. Certainty PSUs are refused: self-representing PSUs contribute no between-PSU variance and belong in their own stratum, so estimate components on the probability part of the design.

Variance-component estimation should use this method on the tbl_sample directly, not as_svydesign(): the exported design carries the compound weight and encodes stage probabilities in fpc conventions that varcomp cannot see.

See also

svyplan::varcomp() for the estimator and its conventions, svyplan::n_cluster() for planning with the result

Other diagnostics: compare_designs(), design_effect, frame_summary(), joint_expectation(), summary.tbl_sample()

Examples

# Two-stage sample: 8 of 24 clusters by PPS, 3 persons per cluster
set.seed(7)
frame <- data.frame(
  cl = rep(sprintf("c%02d", 1:24), each = 5),
  size = rep(rep(c(80, 120, 160, 200), 6), each = 5),
  y = rnorm(120) + rep(rnorm(24, sd = 0.4), each = 5)
)
sam <- sampling_design() |>
  add_stage() |> cluster_by(cl) |>
  draw(n = 8, method = "pps_brewer", mos = size) |>
  add_stage() |> draw(n = 3) |>
  execute(frame, seed = 11)

vc <- varcomp(sam, ~y)
vc
#> Variance components (2-stage, PPS stage 1, weighted sample)
#> varb = 0.8186, varw = 31.6097
#> icc = 0.0252
#> var_ratio = 1.5866
#> Unit relvariance = 20.4390
as.data.frame(vc)
#>   stages      varb     varw        icc var_ratio unit_relvar
#> 1      2 0.8185735 31.60975 0.02524255  1.586587    20.43904

# Feed the components into next-round cluster planning
svyplan::n_cluster(stage_cost = c(500, 50), icc = vc,
                   budget = 100000)
#> Optimal 2-stage allocation
#> field design: stage_1 = 74 | stage_2 = 17 -> total n = 1258
#> cv = 0.1902, cost = 99900
#> design df = 73
#> # summary() for the continuous optimum, the design effect and costs