
Stack materialized periods or occasions into a long table
Source:R/stack-periods.R
stack_periods.RdVerifies that two or more materialized samples come from one executed master and stacks them into a plain long table, one row per active master row per element. This is the structural handoff to an inference layer for change between reporting periods, or between occasions. Which estimator of change is appropriate, and under what conditions, is a separate question that depends on the design and the overlap.
Row-binding samples by hand does not reproduce the checks this performs.
Two executions of one design produce identical .sample_id values, so
samples of different masters stack without complaint. A sample edited
after execution keeps a correct provenance record while its rows no longer
match it. Both are refused here.
Value
A tibble with period, subround, master_id, panel, visit
and design_weight first, then the columns of the elements
themselves, with a level attribute. One row per active master row per
element: under with-replacement selection a population unit may occupy
several master rows, so a row is a selection occurrence rather than
necessarily a distinct population unit. master_id is unique within an
element either way. It is an ordinary tibble, not a tbl_sample: it
holds several realizations and repeats the unit key on purpose.
Details
What it stacks
The elements are all whole periods, as execute(master, period = 2)
gives, for change between periods, or all single occasions, as
execute(master, period = 2, subround = 1) gives, for change between
subrounds. The two are not mixed, and the result records which in its
level attribute, "period" or "occasion". On a schedule without
subrounds a period is one occasion, and the level is "period".
A whole period is accepted when each panel is active at one of its
subrounds, so each unit has one row, at its own subround. A period in which
a panel is active at two of its subrounds would hold a unit twice, and the
stack's key would repeat.
It is refused, and so is any other window, whose window-averaged weights
a period label would misdescribe. execute() materializes such a
window on its own.
What the columns mean
Six columns are generated, and their names are chosen to prevent the two substitutions that would quietly invalidate a downstream estimate:
period,subroundThe occasion each row was observed at. A row of a whole period keeps the subround it was observed at.
master_idA master-local unit key, taken from
.sample_id. It matches units across the elements of one master and means nothing outside it. It is not a population identity, and it is not generally a primary sampling unit: a clustered design's own cluster variable is carried through unchanged and is what a consumer'sPSUargument wants.panelThe rotation panel the row's assignment unit was given. Always present: a master drawn without panels has no schedule to materialize, so it has nothing to stack. That unit is the one the master's
panel_stagenames, so several rows can share one panel: all the members of a rotating household under a retained primary unit carry its label, not one of their own.visitThe interview number of the row's unit within its cohort, from the schedule, which is what conditioning and attrition follow. Permanent units are active at every occasion outside the rotation and have
NA.design_weightsamplyr's exact design weight for that element, including the activation factor. It is not a final weight: nothing here is adjusted for nonresponse or calibrated, so a consumer asking for
w_finalis asking for something the analysis must supply.
Every other column of each element is carried through unchanged,
including the strata and cluster variables a consumer needs. samplyr's
internal columns are dropped, stage-specific quantities such as .fpc_1
among them.
Masters drawn one occasion at a time
A master whose later stages are drawn one occasion at a time stacks too,
as a master whose panels open several cohorts always is. A cohort keeps
its units' master_id over its visits, and the next cohort of a panel
takes new ones. Each element records the master's first stages and the
draws its rows come from. The stack is refused
(samplyr_error_occasion_master_mismatch) when two elements read
different draws of one cohort, as after drawing a period again with
another seed, and when some elements come from the master once its draws
were complete, since completing them numbers the units again. It is also
refused when the elements read occasions drawn into separate copies of
the master, each drawn from the same earlier master, since each copy
numbers its new units from the same point.
The variance of a change
The variance of a change between two periods is that of the window of
both, exported by as_svydesign(), which reads the activation's quota
blocks and the periods each unit is active in as second-phase strata
(panel_assignment). Each period enters the window at half its weight,
so the change is the window's total of the outcome times 2 in the later
period and -2 in the earlier:
both <- execute(master, period = c(1, 2))
both$change <- ifelse(both$.period == 2, 2, -2) * both$y
survey::svytotal(~change, as_svydesign(both))A design built on the stack, clustered by the primary units, must at
least cross the design's strata with panel. Without it, a change
between periods that read different panels mixes their totals in one
stratum, which overstates its variance. Crossing with panel still
leaves out the quota blocks and the calibration the window's export
applies, so it overstates too. The window, execute(master, period = )
with as_svydesign(), is the design for a change.
What it does not do
It does not compute a covariance, a correlation, or a variance of change.
Those belong to the inference layer, and which estimator is appropriate
depends on the overlap and the design. It does not reshape outcomes:
attach each element's measurement under a common name before stacking.
It does not span the selections of a rotation_program(), because
identity across frame vintages is not established.
See also
execute() for materializing a period or an occasion,
as_svydesign() for the export that supplies each element's own
variance, joint_expectation() with period for how far two occasions
overlap
Other panels, rotation and periods:
panel_assignment,
rotation_program(),
subrounds()
Examples
rotation <- data.frame(
panel = rep(1:4, times = 4),
period = rep(1:4, each = 4),
active = c(
TRUE, TRUE, FALSE, FALSE,
FALSE, TRUE, TRUE, FALSE,
FALSE, FALSE, TRUE, TRUE,
TRUE, FALSE, FALSE, TRUE
)
)
master <- sampling_design() |>
draw(n = 40) |>
execute(bfa_eas, seed = 2025, panels = rotation)
stack_periods(execute(master, period = 1), execute(master, period = 2))
#> # A tibble: 40 × 19
#> period subround master_id panel visit design_weight ea_id region province
#> <int> <int> <int> <int> <int> <dbl> <int> <fct> <fct>
#> 1 1 1 3 1 1 2228. 37130 Hauts-Bas… Tuy
#> 2 1 1 4 2 1 2228. 15678 Centre Kadiogo
#> 3 1 1 6 2 1 2228. 11042 Boucle du… Banwa
#> 4 1 1 8 1 1 2228. 12542 Cascades Leraba
#> 5 1 1 9 2 1 2228. 8163 Centre-Est Koulpel…
#> 6 1 1 12 1 1 2228. 23664 Nord Yatenga
#> 7 1 1 14 2 1 2228. 808 Centre-Est Boulgou
#> 8 1 1 15 1 1 2228. 15891 Centre Kadiogo
#> 9 1 1 19 2 1 2228. 22755 Sahel Oudalan
#> 10 1 1 20 1 1 2228. 30483 Sahel Seno
#> # ℹ 30 more rows
#> # ℹ 10 more variables: commune <fct>, urban_rural <fct>, population <int>,
#> # households <int>, area_km2 <dbl>, pop_density <dbl>, longitude <dbl>,
#> # latitude <dbl>, remoteness <fct>, fieldwork_cost <int>