This vignette describes a generalized procedure making use of the methods implemented in the R package developed in the Italian National Institute, namely R2BEAT (“Multistage Sampling Allocation and PSU selection”).

This package allows to determine the optimal allocation of both Primary Stage Units (PSUs) and Secondary Stage Units (SSU), and also to perform a selection of the PSUs such that the final sample of SSU is of the self-weighting type, i.e. the total inclusion probabilities (as resulting from the product between the inclusion probabilities of the PSUs and those of the SSUs) are near equal for all SSUs, or at least those of minimum variability.

This general flow assumes that at least a previous round of the survey, whose sampling design has to be optimized, is available, and is characterized by the following steps:

Use of ReGenesees

Perform externally the definition of the sample design, and possibly of the calibration step, using the R package ReGenesees, and make the design object and the calibrated object available.

The workspace to be loaded (R2BEAT_ReGenesees.RData) is available at the link:

https://github.com/barcaroli/R2BEAT/tree/master/data

load("R2BEAT_ReGenesees.RData")   # ReGenesees design object

This is the ‘design’ object:

des
## Stratified 2 - Stage Cluster Sampling Design (with replacement)
## - [49] strata (collapsed)
## - [789, 2236] clusters
## 
## Call:
## e.svydesign(sample_2st, ids = ~municipality + id_hh, strata = ~stratum_sub, 
##     weights = ~d, self.rep.str = ~SR, check.data = TRUE)

and this is the calibrated object:

cal
## Calibrated, Stratified 2 - Stage Cluster Sampling Design (with replacement)
## - [49] strata (collapsed)
## - [789, 2236] clusters
## 
## Call:
## e.calibrate(design = des, df.population = pop, calmodel = ~clage:sex - 
##     1, partition = ~region, calfun = "logit", bounds = c(0.7, 
##     1.7), aggregate.stage = 2, force = FALSE)

It is advisable to check the presence of lonely strata:

# Control the presence of strata with less than two units
ls <- find.lon.strata(des)
## # No lonely PSUs found!

In case, provide to collapse and re-do the calibration.

In this example, in the ReGenesees objects there are the following variables:

str(des$variables)
## 'data.frame':    2244 obs. of  17 variables:
##  $ region               : Factor w/ 3 levels "north","center",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ municipality         : num  8 8 8 8 8 8 8 8 8 8 ...
##  $ stratum              : Factor w/ 24 levels "1000","2000",..: 9 9 9 9 9 9 9 9 9 9 ...
##  $ stratum_sub          : Factor w/ 81 levels "100001","100002",..: 81 81 81 81 81 81 81 81 81 81 ...
##  $ SR                   : Factor w/ 2 levels "0","1": 2 2 2 2 2 2 2 2 2 2 ...
##  $ id_hh                : Factor w/ 2236 levels "H100070","H100410",..: 69 43 64 49 367 27 372 373 374 368 ...
##  $ sex                  : Factor w/ 2 levels "1","2": 1 1 2 2 1 2 1 2 1 1 ...
##  $ clage                : Factor w/ 5 levels "cl0_17","cl18_34",..: 3 1 2 1 5 2 2 2 3 1 ...
##  $ income_hh            : num  43741 23284 23450 22171 19904 ...
##  $ work                 : num  1 1 1 2 0 1 1 1 1 2 ...
##  $ unemployed           : num  0 0 0 0 1 0 0 0 0 0 ...
##  $ d                    : num  1238 1238 1238 1238 1238 ...
##  $ progr_str            : num  1 1 1 1 1 1 1 1 1 1 ...
##  $ var.PSU              : chr  "8.H12425" "8.H10738" "8.H12157" "8.H11208" ...
##  $ stratum_sub.collapsed: Factor w/ 49 levels "0.center.clps.1",..: 49 49 49 49 49 49 49 49 49 49 ...
##  $ active               : Factor w/ 2 levels "0","1": 2 2 2 1 1 2 2 2 2 1 ...
##  $ inactive             : Factor w/ 2 levels "0","1": 1 1 1 2 1 1 1 1 1 2 ...

where there are three potential target variables:

  1. income_hh (numeric);
  2. work (factor with values 0, 1, 2);
  3. unemployed (factor with values 0, 1).
summary(des$variables$income_hh)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##       0   11463   18516   21661   26763  532331
table(des$variables$work)
## 
##    0    1    2 
##  306 1487  451
table(des$variables$unemployed)
## 
##    0    1 
## 1938  306

Great attention must be paid to the nature of the target variables, especially of the ‘factor’ type. In fact, the procedure here illustrated is suitable only when categorical variables are binary with values 0 and 1, supposing we are willing to estimate proportions of ‘1’ in the population. If factor variables are of other nature, then an error message is printed.

Build ‘strata’, ‘deff’, ‘effst’ and ‘rho’ dataframes

Using ReGenesees objects as input, produce the following dataframes (function ‘input_to_beat.2st_1’):

  1. the ‘stratif’ dataframe containing:
  • STRATUM: identifier of the single stratum
  • N: total population in terms of final sampling units
  • Mi,Si: mean and standard deviation of target variables (i=1,2,..,P)
  • DOMk: domain(s) to which the stratum belongs
  1. the ‘deff’ (design effect) dataframe, containing the following information:
  • STRATUM: the stratum identifier
  • DEFFi: the design effect for each target variable i (i=1,2,…,P)
  1. the ‘effst’ (estimator effect) dataframe, containing the following information:
  • STRATUM: the stratum identifier
  • EFFSTi: the estimator effect for each target variable i (i=1,2,…,P)
  1. the ‘rho’ (intraclass coefficient of correlation) dataframe, containing the following information:
  • STRATUM: the stratum identifier
  • RHO_ARi: the intraclass coefficient of correlation in self-representative PSUs for each target variable i (i=1,2,…,P)
  • RHO_NARi: the intraclass coefficient of correlation in non self-representative PSUs for each target variable i (i=1,2,…,P)

Actually, the ‘deff’ dataframe is not used in the following steps, it just remains for documentation purposes.

Here is the way we can produce the above items:

load("pop.RData")
samp_frame <- pop
RGdes <- des
RGcal <- cal
strata_var <- c("stratum")      
target_vars <- c("income_hh",
                 "active",
                 "inactive",
                 "unemployed")   
weight_var <- "weight"
deff_var <- "stratum"            
id_PSU <- c("municipality")      
id_SSU <- c("id_hh")             
domain_var <- c("region") 
delta <- 1                   
minimum <- 50                

inp <- prepareInputToAllocation2(
        samp_frame,  # sampling frame
        RGdes,       # ReGenesees design object
        RGcal,       # ReGenesees calibrated object
        id_PSU,      # identification variable of PSUs
        id_SSU,      # identification variable of SSUs
        strata_var,  # strata variables
        target_vars, # target variables
        deff_var,    # deff variables
        domain_var,  # domain variables
        delta,       # Average number of SSUs for each selection unit
        minimum      # Minimum number of SSUs to be selected in each PSU
      )

and these are the results:

head(inp$strata)
##   stratum STRATUM      N       M1        M2        M3         M4       S1        S2        S3        S4 COST CENS DOM1   DOM2
## 1    1000    1000 197451 22266.58 0.6404431 0.2323140 0.12724293 14554.88 0.4798705 0.4223082 0.3332449    1    0    1 center
## 2   10000   10000 106106 27985.40 0.7679285 0.2114187 0.02065276 24367.97 0.4221544 0.4083146 0.1422189    1    0    1  north
## 3   11000   11000 202700 29173.85 0.8029080 0.1730880 0.02400395 39232.92 0.3978024 0.3783234 0.1530613    1    0    1  north
## 4   12000   12000  57420 26937.42 0.7764955 0.2075926 0.01591188 15743.78 0.4165936 0.4055834 0.1251347    1    0    1  north
## 5   13000   13000 103089 26357.25 0.7185271 0.2814729 0.00000000 14592.50 0.4497176 0.4497176 0.0000000    1    0    1  north
## 6   14000   14000  84653 20538.42 0.7518236 0.2131042 0.03507211 14285.81 0.4319547 0.4095007 0.1839621    1    0    1  north
head(inp$deff)
##   stratum STRATUM    DEFF1    DEFF2    DEFF3    DEFF4    b_nar
## 1    1000    1000 0.951705 0.991140 1.006731 0.954024 56.50000
## 2   10000   10000 0.856598 1.687606 1.404308 0.819854 26.75000
## 3   11000   11000 1.811807 1.261816 1.346654 1.339464 23.77778
## 4   12000   12000 1.086363 0.502458 0.483954 0.700691 21.00000
## 5   13000   13000 1.000924 1.000924 1.000924 1.000000 95.00000
## 6   14000   14000 0.633543 0.856820 0.845580 0.677276 33.66667
head(inp$effst)
##   stratum STRATUM    EFFST1 EFFST2 EFFST3    EFFST4
## 1    1000    1000 0.9689494      1      1 0.9420958
## 2   10000   10000 0.9500011      1      1 1.1915475
## 3   11000   11000 0.9544521      1      1 1.0546196
## 4   12000   12000 1.0429461      1      1 0.9732493
## 5   13000   13000 0.9914219      1      1 1.0000000
## 6   14000   14000 0.9829167      1      1 1.0974521
head(inp$rho)
##   STRATUM RHO_AR1        RHO_NAR1 RHO_AR2        RHO_NAR2 RHO_AR3        RHO_NAR3 RHO_AR4      RHO_NAR4
## 1    1000       1 -0.000870180180       1 -0.000159639640       1  0.000121279279       1 -0.0008283964
## 2   10000       1 -0.005569009709       1  0.026703145631       1  0.015701281553       1 -0.0069959612
## 3   11000       1  0.035640307317       1  0.011494360976       1  0.015218956098       1  0.0149032976
## 4   12000       1  0.004318150000       1 -0.024877100000       1 -0.025802300000       1 -0.0149654500
## 5   13000       1  0.000009829787       1  0.000009829787       1  0.000009829787       1  0.0000000000
## 6   14000       1 -0.011218071429       1 -0.004383061224       1 -0.004727142857       1 -0.0098793061
head(inp$psu_file)
##   PSU_ID STRATUM PSU_MOS
## 1    309    1000   50845
## 2    330    1000  146162
## 3    292    2000   24794
## 4    293    2000   19609
## 5    300    2000   13897
## 6    304    2000   36195
head(inp$des_file)
##   STRATUM STRAT_MOS DELTA MINIMUM
## 1    1000    197007     1      50
## 2    2000    261456     1      50
## 3    3000    115813     1      50
## 4    4000     17241     1      50
## 5    5000    101067     1      50
## 6    6000     47218     1      50

Check the coherence of populations in strata and PSUs

It may happen that the population in strata (variable ‘N’ in ‘inp1$strata’ dataset) and the one derived by the PSU dataset (variable ‘STRAT_MOS’ in ‘inp2$des_file’ dataset) are not the same.

We can check it by applying the function ‘check_input’ in this way:

newstrata <- check_input(strata=inp$strata,
                         des=inp$des_file,
                         strata_var_strata="STRATUM",
                         strata_var_des="STRATUM")
## 
## --------------------------------------------------
##  Differences between population in strata and PSUs  
## --------------------------------------------------
##    STRATUM N_in_strata N_in_PSUs relative_difference
## 1     1000      197451    197007              -0.002
## 12    2000      258193    261456               0.012
## 18    3000      116213    115813              -0.003
## 19    4000       17879     17241              -0.037
## 20    5000      102706    101067              -0.016
## 21    6000       47477     47218              -0.005
## 22    7000       30193     30370               0.006
## 23    8000       26580     26518              -0.002
## 24    9000       94610     92833              -0.019
## 2    10000      106106    106030              -0.001
## 3    11000      202700    205900               0.016
## 4    12000       57420     57657               0.004
## 5    13000      103089    102933              -0.002
## 6    14000       84653     83983              -0.008
## 7    15000      187343    186390              -0.005
## 8    16000      108621    108816               0.002
## 9    17000       59483     61117               0.027
## 10   18000       71642     74255               0.035
## 11   19000      145891    140383              -0.039
## 13   20000       62130     60853              -0.021
## 14   21000       51552     55144               0.065
## 15   22000       41688     41791               0.002
## 16   23000       72809     72165              -0.009
## 17   24000       12081     11567              -0.044
## 
## --------------------------------------------------
## Population of PSUs has been attributed to strata

Together with the print of the differences between the two populations, the function produces a new version of the strata dataset, where the population has been changed to the one derived by the PSUs dataset.

It is preferable to use this new version:

inp$strata <- newstrata

Optimal allocation of units in each stratum

Using the function ‘beat.2st’ in ‘R2BEAT’ package execute the optimization of PSU and SSU allocation in strata:

cv <- as.data.frame(list(DOM=c("DOM1","DOM2"),
                         CV1=c(0.02,0.03),
                         CV2=c(0.03,0.05),
                         CV3=c(0.03,0.05),
                         CV4=c(0.05,0.08)))
cv
##    DOM  CV1  CV2  CV3  CV4
## 1 DOM1 0.02 0.03 0.03 0.05
## 2 DOM2 0.03 0.05 0.05 0.08
set.seed(1234)
minPSUstrat <- 2
inp$des_file$MINIMUM <- 50
alloc <- beat.2st(stratif = inp$strata, 
                  errors = cv, 
                  des_file = inp$des_file, 
                  psu_file = inp$psu_file, 
                  rho = inp$rho, 
                  deft_start = NULL, 
                  effst = inp$effst, 
                  minnumstrat = 2, 
                  minPSUstrat)
##    iterations PSU_SR PSU NSR PSU Total   SSU
## 1           0      0       0         0  7701
## 2           1     82      60       142 10535
## 3           2     63     134       197  8813
## 4           3     35     128       163 10330
## 5           4     63     130       193  8829
## 6           5     35     128       163 10330
## 7           6     63     130       193  8829
## 8           7     35     128       163 10330
## 9           8     63     130       193  8829
## 10          9     35     128       163 10330
## 11         10     63     130       193  8829
## 12         11     35     128       163 10330
## 13         12     63     130       193  8829
## 14         13     35     128       163 10330
## 15         14     63     130       193  8829
## 16         15     35     128       163 10330
## 17         16     63     130       193  8829
## 18         17     35     128       163 10330
## 19         18     63     130       193  8829
## 20         19     35     128       163 10330
## 21         20     63     130       193  8829

This is the sensitivity of the solution:

alloc$sensitivity
##    Type Dom V1 V2  V3   V4
## 2  DOM1   1  1  0   1    1
## 6  DOM2   1  1  0  11 1241
## 10 DOM2   2  1  1 212    1
## 14 DOM2   3  1  1 302    1

i.e., for each domain value and for each variable it is reported the gain in terms of reduction in the sample size if the corresponding precision constraint is reduced of 10%.

These are the expected values of the coefficients of variation:

alloc$expected
##    Type Dom     V1     V2     V3     V4
## 2  DOM1   1 0.0139 0.0103 0.0297 0.0328
## 6  DOM2   1 0.0178 0.0132 0.0499 0.0800
## 10 DOM2   2 0.0290 0.0202 0.0499 0.0666
## 14 DOM2   3 0.0278 0.0246 0.0499 0.0398

Selection of PSUs

Using the function ‘select_SSU’ execute the selection of PSU in strata:

set.seed(1234)
sample_1st <- select_PSU(alloc, type="ALLOC", pps=TRUE, plot=TRUE)

This is the overall sample design:

sample_1st$PSU_stats
##    STRATUM PSU PSU_SR PSU_NSR   SSU SSU_SR SSU_NSR
## 1     1000   2      2       0   273    273       0
## 2     2000   7      3       4   350    150     200
## 3     3000   4      0       4   214      0     214
## 4     4000   2      0       2   100      0     100
## 5     5000   2      2       0   199    199       0
## 6     6000   2      0       2   100      0     100
## 7     7000   2      0       2   100      0     100
## 8     8000   2      0       2   100      0     100
## 9     9000   1      1       0   828    828       0
## 10   10000   6      6       0   667    667       0
## 11   11000  40     16      24  2000    800    1200
## 12   12000   4      0       4   200      0     200
## 13   13000   1      1       0    50     50       0
## 14   14000   4      4       0   630    630       0
## 15   15000  27      9      18  1350    450     900
## 16   16000  58     16      42  2900    800    2100
## 17   17000   1      1       0   158    158       0
## 18   18000   3      1       2   230     50     180
## 19   19000  12      0      12   610      0     610
## 20   20000   4      0       4   210      0     210
## 21   21000   1      1       0   149    149       0
## 22   22000   2      0       2   100      0     100
## 23   23000   4      0       4   208      0     208
## 24   24000   2      0       2   100      0     100
## 25   Total 193     63     130 11826   5204    6622

Selection of SSUs

Finally, we are able to select the Secondary Sample Units (the individuals) from the already selected PSUs (the municipalities). We proceed to select the sample in this way:

samp <- select_SSU(df=pop,
                   PSU_code="municipality",
                   SSU_code="id_ind",
                   PSU_sampled=sample_1st$sample_PSU,
                   verbose=FALSE)

To check that the total amount is practically equal to what determined in the allocation step:

nrow(samp)
## [1] 11826
sum(alloc$alloc$ALLOC[-nrow(alloc$alloc)])
## [1] 8829

and that the sum of weights equalize population size:

nrow(pop)
## [1] 2258507
sum(samp$weight)
## [1] 2258507

This is the distribution of weights:

par(mfrow=c(1, 2))
boxplot(samp$weight,col="orange")
title("Weights distribution (total sample)",cex.main=0.7)
boxplot(weight ~ region, data=samp,col="orange")
title("Weights distribution by region",cex.main=0.7)

boxplot(weight ~ province, data=samp,col="orange")
title("Weights distribution by province",cex.main=0.7)
boxplot(weight ~ stratum, data=samp,col="orange")
title("Weights distribution by stratum",cex.main=0.7)

It can be seen that the sample is fully self-weighted inside strata, and approximately self-weighted in aggregations of strata, that is the result we wanted to obtain.