Learn R Programming

⚠️There's a newer version (1.3.0) of this package.Take me there.

srvyr

srvyr brings parts of dplyr's syntax to survey analysis, using the survey package.

srvyr focuses on calculating summary statistics from survey data, such as the mean, total or quantile. It allows for the use of many dplyr verbs, such as summarize, group_by, and mutate, the convenience of pipe-able functions, lazyeval's style of non-standard evaluation and more consistent return types than the survey package.

You can try it out:

install.packages("srvyr")
# or for development version
# devtools::install_github("gergness/srvyr")

To create a tbl_svy object (the core concept behind the srvyr package), use the function as_survey_design() with the bare column names of the names you would use in survey::svydesign() object.

library(survey)
data(api)

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

Now many of the dplyr verbs are available.

  • Use mutate() if you want to add or modify a variable.

    dstrata <- dstrata %>%
      mutate(api_diff = api00 - api99)
  • summarise() calculates summary statistics such as mean, total, quantile or ratio.

    dstrata %>% 
      summarise(api_diff = survey_mean(api_diff, vartype = "ci")))
  • Use group_by() if you want to summarise by groups.

    dstrata %>% 
      group_by(stype) %>%
      summarise(api_diff = survey_mean(api_diff, vartype = "ci")))

You can still use functions from the survey package if you'd like to:

svyglm(api99 ~ stype, dstrata)

If you'd like to contribute, please let me know! I started this as a way to learn about R package development, so you'll have to bear with me as I learn, but I would appreciate bug reports, pull requests or other suggestions!

Copy Link

Version

Install

install.packages('srvyr')

Monthly Downloads

5,191

Version

0.2.1

License

GPL-2 | GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Greg Freedman

Last Published

April 26th, 2017

Functions in srvyr (0.2.1)

as_survey

Create a tbl_svy from a data.frame
as_survey_design

Create a tbl_svy survey object using sampling design
as_tibble

Coerce survey variables to a data frame (tibble)
cascade

Summarise multiple values into cascading groups
as_survey_rep

Create a tbl_svy survey object using replicate weights
as_survey_twophase

Create a tbl_svy survey object using two phase design
survey_total

Calculate the total and its variation using survey methods
svychisq

Chisquared tests of association for survey data.
tbl_svy

tbl_svy object.
tbl_vars

List variables produced by a tbl.
groups

Get/set the grouping variables for tbl.
%>%

Pipe operator
survey_quantile

Calculate the quantile and its variation using survey methods
survey_ratio

Calculate the ratio and its variation using survey methods
dplyr_single

Single table verbs from dplyr
group_by

Group a (survey) dataset by one or more variables.
srvyr

svrvyr: A package for 'dplyr'-Like Syntax for Summary Statistics of Survey Data.
summarise

Summarise multiple values to a single value.
unweighted

Calculate the an unweighted summary statistic from a survey
summarise_all

Summarise and mutate multiple columns.
survey_mean

Calculate the mean and its variation using survey methods