Learn R Programming

srvyr (version 0.1.1)

survey_mean: Calculate the mean and its variation using survey methods

Description

Calculate means and proportions from complex survey data. A wrapper around svymean, or if proportion = TRUE, svyciprop. survey_mean should always be called from summarise.

Usage

survey_mean(x, na.rm = FALSE, vartype = c("se", "ci", "var", "cv"),
  level = 0.95, proportion = FALSE, prop_method = c("logit", "likelihood",
  "asin", "beta", "mean"), ...)

Arguments

x
A variable or expression, or empty
na.rm
A logical value to indicate whether missing values should be dropped
vartype
Report variability as one or more of: standard error ("se", default), confidence interval ("ci"), variance ("var") or coefficient of variation ("cv").
level
A single number or vector of numbers indicating the confidence level
proportion
Use methods to calculate the proportion that may have more accurate confidence intervals near 0 and 1. Based on svyciprop.
prop_method
Type of proportion method to use if proportion is TRUE. See svyciprop for details.
...
Ignored

Examples

Run this code
library(survey)
data(api)

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

dstrata %>%
  summarise(api99 = survey_mean(api99),
            api_diff = survey_mean(api00 - api99, vartype = c("ci", "cv")))

dstrata %>%
  group_by(awards) %>%
  summarise(api00 = survey_mean(api00))

# Leave x empty to calculate the proportion in each group
dstrata %>%
  group_by(awards) %>%
  summarise(pct = survey_mean())

# Setting proportion = TRUE uses another method for calculating confidence intervals
dstrata %>%
  summarise(high_api = survey_mean(api00 > 875, proportion = TRUE, vartype = "ci"))

# level takes a vector for multiple levels of confidence intervals
dstrata %>%
  summarise(api99 = survey_mean(api99, vartype = "ci", level = c(0.95, 0.65)))

Run the code above in your browser using DataLab