Examples for 'srvyr::as_survey_design'


Create a tbl_svy survey object using sampling design

Aliases: as_survey_design as_survey_design.data.frame as_survey_design.survey.design2 as_survey_design.tbl_lazy

Keywords:

### ** Examples

# Examples from ?survey::svydesign
library(survey)
Loading required package: grid
Loading required package: Matrix
Loading required package: survival
Attaching package: 'survey'
The following object is masked from 'package:graphics':

    dotchart
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)
Stratified 1 - level Cluster Sampling design (with replacement)
With (162) clusters.
Called via srvyr
Sampling variables:
 - ids: dnum
 - strata: stype
 - weights: pw
Data variables: cds (chr), stype (fct), name (chr), sname (chr), snum (dbl),
  dname (chr), dnum (int), cname (chr), cnum (int), flag (int), pcttest (int),
  api00 (int), api99 (int), target (int), growth (int), sch.wide (fct),
  comp.imp (fct), both (fct), awards (fct), meals (int), ell (int), yr.rnd
  (fct), mobility (int), acs.k3 (int), acs.46 (int), acs.core (int), pct.resp
  (int), not.hsg (int), hsg (int), some.col (int), col.grad (int), grad.sch
  (int), avg.ed (dbl), full (int), emer (int), enroll (int), api.stu (int), pw
  (dbl), fpc (dbl)
## PPS sampling without replacement
data(election)
dpps <- election_pps %>%
  as_survey_design(fpc = p, pps = "brewer")

# dplyr 0.7 introduced new style of NSE called quosures
# See `vignette("programming", package = "dplyr")` for details
st <- quo(stype)
wt <- quo(pw)
dstrata <- apistrat %>%
  as_survey_design(strata = !!st, weights = !!wt)

[Package srvyr version 1.1.1 Index]