expss (version 0.10.5)

w_mean: Compute various weighted statistics

Description

  • w_mean weighted mean of a numeric vector

  • w_sd weighted sample standard deviation of a numeric vector

  • w_var weighted sample variance of a numeric vector

  • w_se weighted standard error of a numeric vector

  • w_median weighted median of a numeric vector

  • w_mad weighted mean absolute deviation from median of a numeric vector

  • w_sum weighted sum of a numeric vector

  • w_n weighted number of values of a numeric vector

  • w_cov weighted covariance matrix of a numeric matrix/data.frame

  • w_cor weighted Pearson correlation matrix of a numeric matrix/data.frame

  • w_pearson shortcut for w_cor. Weighted Pearson correlation matrix of a numeric matrix/data.frame

  • w_spearman weighted Spearman correlation matrix of a numeric matrix/data.frame

Usage

w_mean(x, weight = NULL, na.rm = TRUE)

w_median(x, weight = NULL, na.rm = TRUE)

w_var(x, weight = NULL, na.rm = TRUE)

w_sd(x, weight = NULL, na.rm = TRUE)

w_se(x, weight = NULL, na.rm = TRUE)

w_mad(x, weight = NULL, na.rm = TRUE)

w_sum(x, weight = NULL, na.rm = TRUE)

w_n(x, weight = NULL, na.rm = TRUE)

unweighted_valid_n(x, weight = NULL)

valid_n(x, weight = NULL)

w_max(x, weight = NULL, na.rm = TRUE)

w_min(x, weight = NULL, na.rm = TRUE)

w_cov(x, weight = NULL, use = c("pairwise.complete.obs", "complete.obs"))

w_cor(x, weight = NULL, use = c("pairwise.complete.obs", "complete.obs"))

w_pearson(x, weight = NULL, use = c("pairwise.complete.obs", "complete.obs"))

w_spearman(x, weight = NULL, use = c("pairwise.complete.obs", "complete.obs"))

Arguments

x

a numeric vector (matrix/data.frame for correlations) containing the values whose weighted statistics is to be computed.

weight

a vector of weights to use for each element of x. Cases with missing, zero or negative weights will be removed before calculations. If weight is missing then unweighted statistics will be computed.

na.rm

a logical value indicating whether NA values should be stripped before the computation proceeds. Note that contrary to base R statistic functions the default value is TRUE (remove missing values).

use

"pairwise.complete.obs" (default) or "complete.obs". In the first case the correlation or covariance between each pair of variables is computed using all complete pairs of observations on those variables. If use is "complete.obs" then missing values are handled by casewise deletion.

Value

a numeric value of length one/correlation matrix

Details

If argument of correlation functions is data.frame with variable labels then variables names will be replaced with labels. If this is undesirable behavior use unvr function: w_cor(unvr(x)). Weighted spearman correlation coefficients are calculated with rounded to nearest integer weights. It gives the same result as in SPSS Statistics software. By now this algorithm is not memory efficient.

Examples

Run this code
# NOT RUN {
data(mtcars)
dfs = mtcars %>% keep(mpg, disp, hp, wt)

with(dfs, w_mean(hp, weight = 1/wt))

# apply labels
dfs = modify(dfs, {
         var_lab(mpg) = "Miles/(US) gallon"
         var_lab(disp) = "Displacement (cu.in.)"
         var_lab(hp) = "Gross horsepower"
         var_lab(wt) = "Weight (1000 lbs)"
})

# weighted correlations with labels
w_cor(dfs, weight = 1/dfs$wt)

# without labels
w_cor(unvr(dfs), weight = 1/dfs$wt)
# }

Run the code above in your browser using DataLab