svy: tabulate
Cross-tabulation with survey-correct percentages and test.
What we have
- one-way and two-way tables with design-based errors
row,column,count,seandci- the Rao-Scott corrected F test, which matches R's
svychisqexactly (checked)
Coming later
- standard errors inside the main table — here they follow it as separate tables
How you write it
svy: tabulate var1 [var2] [if] [, row column count se ci]
Square brackets mean optional. A vertical bar means choose one.
What it does
One variable gives population shares; two give a grid with a test of whether the two are related.
row, column and count choose what the cells hold; se and ci add the precision as tables underneath. The test of independence is the Rao-Scott corrected version of chi-squared — the ordinary one assumes independent observations and a clustered survey has none. Its degrees of freedom are not whole numbers, which is normal and not a bug.
Try it
Every example below was run after sysuse vira.sample02, clear.
. svyset village [pw=multiplier], strata(stratum)
pweight: multiplier
VCE: linearized
Single unit: missing
Strata 1: stratum
Sampling unit 1: village
FPC 1: <zero>
The same thing in pandas and in R
pandas
design = dict(psu='village', strata='stratum', weight='multiplier')
R
library(survey)
design <- svydesign(ids = ~village, strata = ~stratum,
weights = ~multiplier, data = df, nest = TRUE)
# This R was run in a real R (4.6.1) and gave the same numbers.
. svy: tabulate sector sex, row
Survey: Two-way tabulation
Number of strata = 12 Number of obs = 6,000
Number of PSUs = 400 Population size = 10,612,910
Design df = 388
sex
sector | Male Female | Total
-------------+---------------------+----------
Rural | .5037 .4963 | 1
Urban | .4893 .5107 | 1
-------------+---------------------+----------
Total | .4998 .5002 | 1
Key: row proportions
Pearson:
Uncorrected chi2(1) = 0.9812
Design-based F(1.0000, 388.00) = 1.0683
P = 0.3020
The same thing in pandas and in R
pandas
# survey table: ratio estimators, Taylor-linearised; Rao-Scott second-order corrected test of independence
R
library(survey) tab <- svytable(~sector + sex, design) prop.table(tab, 1) svychisq(~sector + sex, design) # This R was run in a real R (4.6.1) and gave the same numbers.
The full built-in help (help svy: tabulate)
svy: tabulate var1 [var2] [if] [, row column count se ci]
Survey tables with design-based errors, using the design from svyset. One
variable gives a one-way table of proportions; two give a two-way table and a
test of independence.
. svyset village [pw=multiplier], strata(stratum)
. svy: tabulate sector sex, row se
(nothing) cell proportions: each cell over the whole table
row row proportions: each row adds to 1
column column proportions: each column adds to 1
count estimated population counts instead of proportions
se add a table of linearised standard errors
ci add a table of 95% confidence intervals (a t interval on the
logit scale for proportions, so it stays between 0 and 1)
The test of independence is the Rao-Scott second-order corrected Pearson
statistic, which Stata calls the design-based F. Its numerator degrees of
freedom are not whole numbers. It matches R's survey::svychisq exactly
(checked); it has not been compared with a real Stata run.
Stata prints standard errors and intervals inside the main table; here they
follow it as separate tables. The numbers are the same.Nearby
svyset svy: mean svy: total svy: proportion svy: ratio svy: regress