Virata manual

svy: mean

The average for the whole population, from a survey sample.

What we have

  • the population mean with a linearised standard error
  • over() for domains, and an if as a subpopulation
  • the interval uses the design degrees of freedom, not the number of people

Coming later

  • replicate weights
  • pooling plausible values

How you write it

svy: mean | total | proportion | ratio | regress | tabulate  ...

Square brackets mean optional. A vertical bar means choose one.

What it does

svy: mean mpce gives the population average with a standard error that accounts for the weights, the strata and the clusters recorded by svyset. The interval uses the design's degrees of freedom — the number of clusters minus the number of strata — rather than the number of people.

over(sector) reports it separately by group, still using the whole design.

Watch out. An if condition here means a subpopulation, not a smaller dataset: the rest of the sample stays in the design. Deleting those rows first would lose clusters and shrink the standard errors incorrectly.

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: mean mpce, over(sector)
Survey: Mean estimation

Number of strata = 12   Number of PSUs   = 400
Number of obs   = 6,000
Design df       = 388
                 |          Mean     Std. err.    [95% conf.     interval]
-----------------+--------------------------------------------------------
           Rural |      3394.003        21.863      3351.018      3436.987
           Urban |      4339.799      35.40776      4270.184      4409.414
The same thing in pandas and in R

pandas

# mean of ['mpce'] with linearised standard errors

R

library(survey)
svyby(~mpce, ~sector, design, svymean)

# This R was run in a real R (4.6.1) and gave the same numbers.
. svy: total hhsize
Survey: Total estimation

Number of strata = 12   Number of PSUs   = 400
Number of obs   = 6,000
Design df       = 388
                 |         Total     Std. err.    [95% conf.     interval]
-----------------+--------------------------------------------------------
          hhsize |   4.80755e+07      713048.5   4.66736e+07   4.94774e+07
The same thing in pandas and in R

pandas

# total of ['hhsize'] with linearised standard errors

R

library(survey)
svytotal(~hhsize, design, na.rm = TRUE)

# This R was run in a real R (4.6.1) and gave the same numbers.
. svy: proportion sector
Survey: Proportion estimation

Number of strata = 12   Number of PSUs   = 400
Number of obs   = 6,000
Design df       = 388
                 |    Proportion     Std. err.    [95% conf.     interval]
-----------------+--------------------------------------------------------
    sector=Rural |      .7302614    .005520317      .7194079      .7411149
    sector=Urban |      .2697386    .005520317      .2588851      .2805921
The same thing in pandas and in R

pandas

# proportion of ['sector'] with linearised standard errors

R

library(survey)
svymean(~factor(sector), design, na.rm = TRUE)

# This R was run in a real R (4.6.1) and gave the same numbers.
. svy: ratio mpce/hhsize
Survey: Ratio estimation

Number of obs   = 6,000
Design df       = 388

                 |         Ratio     Std. err.    [95% conf.     interval]
-----------------+--------------------------------------------------------
     mpce/hhsize |      805.5617      6.908603      791.9787      819.1447
The same thing in pandas and in R

R

# no R form for this one: a ratio needs a numerator and a denominator

Nearby

svyset svy: total svy: proportion svy: ratio svy: regress svy: tabulate