w_mean {expss} | R Documentation |
Compute various weighted statistics
Description
w_mean
weighted mean of a numeric vectorw_sd
weighted sample standard deviation of a numeric vectorw_var
weighted sample variance of a numeric vectorw_se
weighted standard error of a numeric vectorw_median
weighted median of a numeric vectorw_mad
weighted mean absolute deviation from median of a numeric vectorw_sum
weighted sum of a numeric vectorw_n
weighted number of values of a numeric vectorw_cov
weighted covariance matrix of a numeric matrix/data.framew_cor
weighted Pearson correlation matrix of a numeric matrix/data.framew_pearson
shortcut forw_cor
. Weighted Pearson correlation matrix of a numeric matrix/data.framew_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
|
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 |
|
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 drop_var_labs function: w_cor(drop_var_labs(x))
. Weighted
Spearman correlation coefficients are calculated with weights rounded to nearest
integer. It gives the same result as in SPSS Statistics software. By
now this algorithm is not memory efficient.
Value
a numeric value of length one/correlation matrix
Examples
data(mtcars)
dfs = mtcars %>% columns(mpg, disp, hp, wt)
with(dfs, w_mean(hp, weight = 1/wt))
# apply labels
mtcars = mtcars %>%
apply_labels(
mpg = "Miles/(US) gallon",
cyl = "Number of cylinders",
disp = "Displacement (cu.in.)",
hp = "Gross horsepower",
drat = "Rear axle ratio",
wt = "Weight (lb/1000)",
qsec = "1/4 mile time",
vs = "Engine",
vs = c("V-engine" = 0,
"Straight engine" = 1),
am = "Transmission",
am = c(automatic = 0,
manual=1),
gear = "Number of forward gears",
carb = "Number of carburetors"
)
# weighted correlations with labels
w_cor(dfs, weight = 1/dfs$wt)
# without labels
w_cor(drop_var_labs(dfs), weight = 1/dfs$wt)