1.3 Usage Examples

In the following example, we’ve loaded the data set eusilc from the R libraries vardpoor and laeken.

library(vardpoor)
data(eusilc)

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

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.017202243
Carinthia         Carinthia 0.1308627 0.010610622
Lower Austria Lower Austria 0.1384362 0.006517660
Salzburg           Salzburg 0.1378734 0.011579280
Styria               Styria 0.1437464 0.007452360
Tyrol                 Tyrol 0.1530819 0.009880430
Upper Austria Upper Austria 0.1088977 0.005928336
Vienna               Vienna 0.1723468 0.007682826
Vorarlberg       Vorarlberg 0.1653731 0.013754670

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) 

and then use the function svyarpr:

svyarpr(~eqIncome, design=des_eusilc_rep)
            arpr     SE
eqIncome 0.14444 0.0028
svyby(~eqIncome, by = ~db040, design = des_eusilc_rep, FUN = svyarpr, deff = FALSE)
                      db040  eqIncome se.eqIncome
Burgenland       Burgenland 0.1953984 0.017655273
Carinthia         Carinthia 0.1308627 0.009317586
Lower Austria Lower Austria 0.1384362 0.006206377
Salzburg           Salzburg 0.1378734 0.010593892
Styria               Styria 0.1437464 0.008744064
Tyrol                 Tyrol 0.1530819 0.009198533
Upper Austria Upper Austria 0.1088977 0.005326377
Vienna               Vienna 0.1723468 0.007720307
Vorarlberg       Vorarlberg 0.1653731 0.013955803

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

It is also possible to deal with 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.0033