srvyr (version 0.2.1)

as_survey_design: Create a tbl_svy survey object using sampling design

Description

Create a survey object with a survey design.

Usage

as_survey_design(.data, ...)

# S3 method for data.frame as_survey_design(.data, ids = NULL, probs = NULL, strata = NULL, variables = NULL, fpc = NULL, nest = FALSE, check_strata = !nest, weights = NULL, pps = FALSE, variance = c("HT", "YG"), uid = NULL, ...)

# S3 method for tbl_sql as_survey_design(.data, ids = NULL, probs = NULL, strata = NULL, variables = NULL, fpc = NULL, nest = FALSE, check_strata = !nest, weights = NULL, pps = FALSE, variance = c("HT", "YG"), uid = NULL, ...)

# S3 method for survey.design2 as_survey_design(.data, ...)

as_survey_design_(.data, ids = NULL, probs = NULL, strata = NULL, variables = NULL, fpc = NULL, nest = FALSE, check_strata = !nest, weights = NULL, pps = FALSE, variance = c("HT", "YG"), uid = NULL)

Arguments

.data

A data frame (which contains the variables specified below)

...

ignored

ids

Variables specifying cluster ids from largest level to smallest level (leaving the argument empty, NULL, 1, or 0 indicate no clusters).

probs

Variables specifying cluster sampling probabilities.

strata

Variables specifying strata.

variables

Variables specifying variables to be included in survey. Defaults to all variables in .data

fpc

Variables specifying a finite population correct, see svydesign for more details.

nest

If TRUE, relabel cluster ids to enforce nesting within strata.

check_strata

If TRUE, check that clusters are nested in strata.

weights

Variables specifying weights (inverse of probability).

pps

"brewer" to use Brewer's approximation for PPS sampling without replacement. "overton" to use Overton's approximation. An object of class HR to use the Hartley-Rao approximation. An object of class ppsmat to use the Horvitz-Thompson estimator.

variance

For pps without replacement, use variance="YG" for the Yates-Grundy estimator instead of the Horvitz-Thompson estimator

uid

Required for databases only, variables that uniquely identify the observations of your survey.

Value

An object of class tbl_svy

Details

If provided a data.frame, it is a wrapper around svydesign. All survey variables must be included in the data.frame itself. Variables are selected by using bare column names, or convenience functions described in select. as_survey_design_ is the standard evaluation counterpart to as_survey_design.

If provided a survey.design2 object from the survey package, it will turn it into a srvyr object, so that srvyr functions will work with it

There is also limited and experimental support for databases using dplyr's tbl_sql objects. Not all operations are available for these objects. See NA for more information on setting up databases in dplyr.

Examples

Run this code
# Examples from ?survey::svydesign
library(survey)
data(api)

# stratified sample
dstrata <- apistrat %>%
  as_survey_design(strata = stype, weights = pw)

# one-stage cluster sample
dclus1 <- apiclus1 %>%
  as_survey_design(dnum, weights = pw, fpc = fpc)

# two-stage cluster sample: weights computed from population sizes.
dclus2 <- apiclus2 %>%
  as_survey_design(c(dnum, snum), fpc = c(fpc1, fpc2))

## multistage sampling has no effect when fpc is not given, so
## these are equivalent.
dclus2wr <- apiclus2 %>%
  dplyr::mutate(weights = weights(dclus2)) %>%
  as_survey_design(c(dnum, snum), weights = weights)

dclus2wr2 <- apiclus2 %>%
  dplyr::mutate(weights = weights(dclus2)) %>%
  as_survey_design(c(dnum), weights = weights)

## syntax for stratified cluster sample
## (though the data weren't really sampled this way)
apistrat %>% as_survey_design(dnum, strata = stype, weights = pw,
                           nest = TRUE)

## PPS sampling without replacement
data(election)
dpps <- election_pps %>%
  as_survey_design(fpc = p, pps = "brewer")

## as_survey_design_ uses standard evaluation
strata_var <- "stype"
weights_var <- "pw"
dstrata2 <- apistrat %>%
  as_survey_design_(strata = strata_var, weights = weights_var)

Run the code above in your browser using DataCamp Workspace