-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
20-sampling.Rmd
194 lines (148 loc) · 3.54 KB
/
20-sampling.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# Sampling
## Simple Sampling
Simple (random) Sampling
```{r}
library(dplyr)
iris_df <- iris
set.seed(1)
sample_n(iris_df, 10)
```
```{r}
library(sampling)
# set unique id number for each row
iris_df$id = 1:nrow(iris_df)
# Simple random sampling with replacement
srswor(10, length(iris_df$id))
# Simple random sampling without replacement (sequential method)
srswor1(10, length(iris_df$id))
# Simple random sampling with replacement
srswr(10, length(iris_df$id))
```
```{r}
library(survey)
data("api")
srs_design <- svydesign(data = apistrat,
weights = ~pw,
fpc = ~fpc,
id = ~1)
```
```{r, eval = FALSE}
library(sampler)
rsamp(albania,
n = 260,
over = 0.1, # desired oversampling proportion
rep = F)
```
Identify missing points between sample and collected data
```{r, eval = FALSE}
alsample <- rsamp(df = albania, 544)
alreceived <- rsamp(df = alsample, 390)
rmissing(sampdf = alsample,
colldf = alreceived,
col_name = qvKod)
```
## Stratified Sampling
A stratum is a subset of the population that has at least one common characteristic.
Steps:
1. Identify relevant stratums and their representation in the population.
2. Randomly sample to select a sufficient number of subjects from each stratum.
Stratified sampling reduces sampling error.
```{r}
library(dplyr)
# by number of rows
sample_iris <- iris %>%
group_by(Species) %>%
sample_n(5)
sample_iris
# by fraction
sample_iris <- iris %>%
group_by(Species) %>%
sample_frac(size = .15)
sample_iris
```
```{r}
library(sampler)
# Stratified sample using proportional allocation without replacement
ssamp(df=albania, n=360, strata=qarku, over=0.1)
```
Identify number of missing points by strata between sample and collected data
```{r, eval = FALSE}
alsample <- rsamp(df = albania, 544)
alreceived <- rsamp(df = alsample, 390)
smissing(
sampdf = alsample,
colldf = alreceived,
strata = qarku,
col_name = qvKod
)
```
## Unequal Probability Sampling
```{r, eval = FALSE}
UPbrewer()
UPmaxentropy()
UPmidzuno()
UPmidzunopi2()
UPmultinomial()
UPpivotal()
UPrandompivotal()
UPpoisson()
UPsampford()
UPsystematic()
UPrandomsystematic()
UPsystematicpi2()
UPtille()
UPtillepi2()
```
## Balanced Sampling
- Purpose: to get the same means in the population and the sample for all the auxiliary variables
- Balanced sampling is different from purposive selection
Balancing equations
$$
\sum_{k \in S} \frac{\mathbf{x}_k}{\pi_k} = \sum_{k \in U} \mathbf{x}_k
$$
where $\mathbf{x}_k$ is a vector of auxiliary variables
### Cube
- flight phase
- landing phase
```{r, eval = FALSE}
samplecube()
fastflightcube()
landingcube()
```
### Stratification
- Try to replicate the population based on the original multivariate histogram
```{r}
library(survey)
data("api")
srs_design <- svydesign(data = apistrat,
weights = ~pw,
fpc = ~fpc,
strata = ~stype,
id = ~1)
```
```{r, eval = FALSE}
balancedstratification()
```
### Cluster
```{r}
library(survey)
data("api")
srs_design <- svydesign(data = apiclus1,
weights = ~pw,
fpc = ~fpc,
id = ~dnum)
```
```{r, eval = FALSE}
balancedcluster()
```
### Two-stage
```{r}
library(survey)
data("api")
srs_design <- svydesign(data = apiclus2,
fpc = ~fpc1 + fpc2,
id = ~ dnum + snum)
```
```{r, eval = FALSE}
balancedtwostage()
```