Learn R Programming

sjstats (version 0.19.0)

survey_median: Weighted statistics for variables

Description

weighted_se() computes weighted standard errors of a variable or for all variables of a data frame. survey_median() computes the median for a variable in a survey-design (see [survey::svydesign()]). weighted_correlation() computes a weighted correlation for a two-sided alternative hypothesis.

Usage

survey_median(x, design)

weighted_correlation(data, ...)

# S3 method for default weighted_correlation(data, x, y, weights, ci.lvl = 0.95, ...)

# S3 method for formula weighted_correlation(formula, data, ci.lvl = 0.95, ...)

weighted_se(x, weights = NULL)

Value

The weighted (test) statistic.

Arguments

x

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

design

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

data

A data frame.

...

Currently not used.

y

Optional, bare (unquoted) variable name, or a character vector with the variable name.

weights

Bare (unquoted) variable name, or a character vector with the variable name of the numeric vector of weights. If weights = NULL, unweighted statistic is reported.

ci.lvl

Confidence level of the interval.

formula

A formula of the form lhs ~ rhs1 + rhs2 where lhs is a numeric variable giving the data values and rhs1 a factor with two levels giving the corresponding groups and rhs2 a variable with weights.

Examples

Run this code
if (FALSE) { # requireNamespace("survey")
data(efc)
weighted_se(efc$c12hour, abs(runif(n = nrow(efc))))

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

des <- survey::svydesign(
  id = ~SDMVPSU,
  strat = ~SDMVSTRA,
  weights = ~WTINT2YR,
  nest = TRUE,
  data = nhanes_sample
)
survey_median(total, des)
survey_median("total", des)
}

Run the code above in your browser using DataLab