-
Notifications
You must be signed in to change notification settings - Fork 3
/
surveysd.Rmd
77 lines (57 loc) · 2.37 KB
/
surveysd.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
---
title: "Introduction to surveysd"
date: "`r Sys.Date()`"
output:
rmarkdown::html_vignette:
df_print: kable
vignette: >
%\VignetteIndexEntry{surveysd}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
The goal of surveysd is to combine all necessary steps to use calibrated bootstrapping with
custom estimation functions. This vignette will cover the usage of the most important functions.
For insights in the theory used in this package, refer to `vignette("methodology")`.
### Load dummy data
A test data set based on `data(eusilc, package = "laeken")` can be created with `demo.eusilc()`
```{r}
library(surveysd)
set.seed(1234)
eusilc <- demo.eusilc(n = 2, prettyNames = TRUE)
eusilc[1:5, .(year, povertyRisk, gender, pWeight)]
```
### Draw bootstrap replicates
Use stratified resampling without replacement to generate 10 samples. Those samples are
consistent with respect to the reference periods.
```{r}
dat_boot <- draw.bootstrap(eusilc, REP = 10, hid = "hid", weights = "pWeight",
strata = "region", period = "year")
```
### Calibrate bootstrap replicates
Calibrate each sample according to the distribution of `gender` (on a personal level) and `region`
(on a household level).
```{r}
dat_boot_calib <- recalib(dat_boot, conP.var = "gender", conH.var = "region",
epsP = 1e-2, epsH = 2.5e-2, verbose = TRUE)
dat_boot_calib[1:5, .(year, povertyRisk, gender, pWeight, w1, w2, w3, w4)]
```
### Estimate with respect to a grouping variable
Estimate relative amount of persons at risk of poverty per period and `gender`.
```{r}
err.est <- calc.stError(dat_boot_calib, var = "povertyRisk", fun = weightedRatio, group = "gender")
err.est$Estimates
```
The output contains estimates (`val_povertyRisk`) as well as standard errors (`stE_povertyRisk`)
measured in percent. The rows with `gender = NA` denotes the aggregate over all genders for the corresponding year.
### Estimate with respect to several variables
Estimate relative amount of persons at risk of poverty per period for each `region`,
`gender`, and combination of both.
```{r}
group <- list("gender", "region", c("gender", "region"))
err.est <- calc.stError(dat_boot_calib, var = "povertyRisk", fun = weightedRatio, group = group)
head(err.est$Estimates)
## skipping 54 more rows
```