Learn R Programming

sjstats (version 0.12.0)

wtd_sd: Weighted statistics for variables

Description

wtd_sd() and wtd_se() compute weighted standard deviation or standard error for a variable or for all variables of a data frame. svy_md() computes the median for a variable in a survey-design (see svydesign).

Usage

wtd_sd(x, weights = NULL)

wtd_se(x, weights = NULL)

svy_md(x, design)

Arguments

x

(Numeric) vector or a data frame. For svy_md(), the bare (unquoted) variable name, or a character vector with the variable name.

weights

Numeric vector of weights.

design

An object of class svydesign, providing a specification of the survey design.

Value

The weighted standard deviation or standard error of x, or for each variable if x is a data frame.

Examples

Run this code
# NOT RUN {
wtd_sd(rnorm(n = 100, mean = 3),
       runif(n = 100))

data(efc)
wtd_sd(efc[, 1:3], runif(n = nrow(efc)))
wtd_se(efc[, 1:3], runif(n = nrow(efc)))


# median for variables from weighted survey designs
library(survey)
data(nhanes_sample)

des <- svydesign(
  id = ~SDMVPSU,
  strat = ~SDMVSTRA,
  weights = ~WTINT2YR,
  nest = TRUE,
  data = nhanes_sample
)

svy_md(total, des)
svy_md("total", des)

# }

Run the code above in your browser using DataLab