select_sample: Select simple and stratified random samples from a sampling...

Description Usage Arguments Value Examples

Description

Select Simple Randmom Samples and Stratified Random Samples, select_sample_prop can be used when sampling with equal probability across strata or when selecting a simple random sample. select_sample_str selects samples when sample size varies across strata.

Usage

1
2
3
4
5
select_sample_str(sampling_frame, allocation, sample_size = sample_size,
  stratum = stratum, is_frac = FALSE, seed = NA, replace = FALSE)

select_sample_prop(sampling_frame, stratum = stratum, frac, seed = NA,
  replace = FALSE)

Arguments

sampling_frame

data.frame with the sampling frame it must contain a column with the stratum.

allocation

data.frame with a column defining the strata and a column with sample size allocations for each stratum (one line per stratum).

sample_size

unquoted column with sample sizes in the allocation data.frame

stratum

unquoted column with strata in the allocation and sampling_frame data.frame's (the columns must have the same name in the two data.frame's). If one wants to select a SRS the stratum parameter is not used.

is_frac

logical value indicating whether the allocation data.frame contains proportions to be sampled within each stratum (TRUE) or sample sizes.

seed

integer value used to set the state of the random number generator.

replace

logical value indicating whether the sample should be selected with replacement.

frac

when sampling with equal probability across strata, frac is a numeric value indicating the fraction of the data to select.

Value

A data.frame with the selected sample, it will have the same columns as the original sampling frame plus a column indicating the sample size in the stratum of each selected observation.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# stratified random sampling
library(dplyr)
sampling_frame <- data.frame(id = 1:100,
  str = sample(1:5, 100, replace = TRUE),
  val = rnorm(100))
# allocation given by column n in allo data.frame
allo <- dplyr::sampling_frame %>%
    group_by(str) %>%
    summarise(n = 0.4 * n())
select_sample_str(sampling_frame, allo, n, str)
# SRS (simple random sample)
select_sample_prop(sampling_frame, frac = 0.2)

tereom/quickcountmx documentation built on Dec. 2, 2019, 9:58 p.m.