1.3 Usage Examples

In the following example, we’ve loaded the data set eusilc from the R library laeken (Alfons and Templ 2013Alfons, Andreas, and Matthias Templ. 2013. “Estimation of Social Exclusion Indicators from Complex Surveys: The R Package laeken.” Journal of Statistical Software 54 (15): 1–25. http://www.jstatsoft.org/v54/i15/.).

library(laeken)
data(eusilc)

Next, we create an object of class survey.design using the function svydesign of the survey library:

library(survey)
des_eusilc <-
  svydesign(
    ids = ~ rb030,
    strata =  ~ db040,
    weights = ~ rb050,
    data = eusilc
  )

Right after the creation of the design object des_eusilc, we should use the function convey_prep that adds an attribute to the survey design which saves information on the design object based upon the whole sample, needed to work with subset designs.

library(convey)
des_eusilc <- convey_prep(des_eusilc)

To estimate the at-risk-of-poverty rate, we use the function svyarpt:

svyarpr( ~ eqIncome, design = des_eusilc)
            arpr     SE
eqIncome 0.14444 0.0028

To estimate the at-risk-of-poverty rate across domains defined by the variable db040 we use:

svyby(
  ~ eqIncome,
  by = ~ db040,
  design = des_eusilc,
  FUN = svyarpr,
  deff = FALSE
)
                      db040  eqIncome          se
Burgenland       Burgenland 0.1953984 0.017202852
Carinthia         Carinthia 0.1308627 0.010606502
Lower Austria Lower Austria 0.1384362 0.006513217
Salzburg           Salzburg 0.1378734 0.011581408
Styria               Styria 0.1437464 0.007453192
Tyrol                 Tyrol 0.1530819 0.009884094
Upper Austria Upper Austria 0.1088977 0.005933094
Vienna               Vienna 0.1723468 0.007684540
Vorarlberg       Vorarlberg 0.1653731 0.013756389

Using the same data set, we estimate the quintile share ratio:

# for the whole population
svyqsr( ~ eqIncome, design = des_eusilc, alpha1 = .20)
          qsr     SE
eqIncome 3.97 0.0426
# for domains
svyby(
  ~ eqIncome,
  by = ~ db040,
  design = des_eusilc,
  FUN = svyqsr,
  alpha1 = .20,
  deff = FALSE
)
                      db040 eqIncome         se
Burgenland       Burgenland 5.008486 0.32755685
Carinthia         Carinthia 3.562404 0.10909726
Lower Austria Lower Austria 3.824539 0.08783599
Salzburg           Salzburg 3.768393 0.17015086
Styria               Styria 3.464305 0.09364800
Tyrol                 Tyrol 3.586046 0.13629739
Upper Austria Upper Austria 3.668289 0.09310624
Vienna               Vienna 4.654743 0.13135731
Vorarlberg       Vorarlberg 4.366511 0.20532075

These functions can be used as S3 methods for the classes survey.design and svyrep.design.

Let’s create a design object of class svyrep.design and run the function convey_prep on it:

des_eusilc_rep <- as.svrepdesign(des_eusilc, type = "bootstrap")
des_eusilc_rep <- convey_prep(des_eusilc_rep)

The function svyarpr produces matching coefficients and near-identical standard errors on the replication design:

svyarpr( ~ eqIncome, design = des_eusilc_rep)
            arpr     SE
eqIncome 0.14444 0.0025
svyby(
  ~ eqIncome,
  by = ~ db040,
  design = des_eusilc_rep,
  FUN = svyarpr,
  deff = FALSE
)
                      db040  eqIncome se.eqIncome
Burgenland       Burgenland 0.1953984 0.016713791
Carinthia         Carinthia 0.1308627 0.012061625
Lower Austria Lower Austria 0.1384362 0.007294696
Salzburg           Salzburg 0.1378734 0.010050357
Styria               Styria 0.1437464 0.008558783
Tyrol                 Tyrol 0.1530819 0.010328225
Upper Austria Upper Austria 0.1088977 0.006212301
Vienna               Vienna 0.1723468 0.007259732
Vorarlberg       Vorarlberg 0.1653731 0.012792618

The functions of the convey library are called in a similar way to the functions in survey library.

It is also possible to discard missing values by using the argument na.rm:

# survey.design using a variable with missings
svygini( ~ py010n , design = des_eusilc)
       gini SE
py010n   NA NA
svygini( ~ py010n , design = des_eusilc , na.rm = TRUE)
          gini     SE
py010n 0.64606 0.0036
# svyrep.design using a variable with missings
svygini( ~ py010n , design = des_eusilc_rep)
       gini SE
py010n   NA NA
svygini( ~ py010n , design = des_eusilc_rep , na.rm = TRUE)
          gini     SE
py010n 0.64606 0.0043