Example 1: For One Vector
Step 1: Install Package
install.packages("Hmisc")
library(Hmisc)
Step 2: Create Dataset of 1 vector
x <- c(14, 19, 22, 25, 29, 31, 31, 38, 40, 41)
Step 3: Define weights
wt <- c(1, 1, 1.5, 2, 2, 1.5, 1, 2, 3, 2)
Step 4: Calculate weighted variance
weighted_var <- wtd.var(x, wt)
Step 5: Calculate weighted standard deviation
sqrt(weighted_var)
#Step 1: Install Package
install.packages("Hmisc")
library(Hmisc)
#Step 2: Create Dataset of 1 vector
x <- c(14, 19, 22, 25, 29, 31, 31, 38, 40, 41)
#Step 3: Define weights
wt <- c(1, 1, 1.5, 2, 2, 1.5, 1, 2, 3, 2)
#Step 4: Calculate weighted variance
weighted_var <- wtd.var(x, wt)
#Step 5: Calculate weighted standard deviation
sqrt(weighted_var)
Output
8.570051
How to Calculate Weighted Standard Deviation in R
The weighted standard deviation is a method to measure the dispersion of values in a dataset when some values in the dataset have higher values than others.
Mathematically, it is defined as:
where:
- N: The total number of observations
- M: The number of non-zero weights
- wi: A vector of weights
- xi: A vector of data values
- x: The weighted mean
We can use wt.var() function from the Hmisc package to Calculate Weighted Standard Deviation in R