Open In App

How to Calculate Weighted Standard Deviation in R

Last Updated : 26 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

z287

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

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)
R
#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

Example 2: For One Column of Data Frame

Step 1: Install Package

install.packages("Hmisc")
library(Hmisc)

Step 2: Create dataset for For One Column of Data Frame

df <- data.frame(team=c('A', 'A', 'A', 'A', 'A', 'B', 'B', 'C'),
wins=c(2, 9, 11, 12, 15, 17, 18, 19),
points=c(1, 2, 2, 2, 3, 3, 3, 3))

Step 3: Define weights

wt <- c(1, 1, 1.5, 2, 2, 1.5, 1, 2)

Step 4: Calculate weighted variance

weighted_var <- wtd.var(df$points, wt)

Step 5: Calculate weighted standard deviation

sqrt(weighted_var)

Code

R
#Step 1: Install Package
install.packages("Hmisc")
library(Hmisc)

#Step 2: Create dataset for For One Column of Data Frame
df <- data.frame(team=c('A', 'A', 'A', 'A', 'A', 'B', 'B', 'C'),
                 wins=c(2, 9, 11, 12, 15, 17, 18, 19),
                 points=c(1, 2, 2, 2, 3, 3, 3, 3))

#Step 3: Define weights
wt <- c(1, 1, 1.5, 2, 2, 1.5, 1, 2)

#Step 4:  Calculate weighted variance 
weighted_var <- wtd.var(df$points, wt)

#Step 5: Calculate weighted standard deviation 
sqrt(weighted_var)

Output

0.6727938

Example 3: For Multiple Columns of Data Frame

Step 1: Install Package

install.packages("Hmisc")
library(Hmisc)

Step 2: Create dataset for For Multiple Columns of Data Frame

df <- data.frame(team=c('A', 'A', 'A', 'A', 'A', 'B', 'B', 'C'),
wins=c(2, 9, 11, 12, 15, 17, 18, 19),
points=c(1, 2, 2, 2, 3, 3, 3, 3))

Step 3: Define weights

wt <- c(1, 1, 1.5, 2, 2, 1.5, 1, 2)

Step 4: Calculate weighted standard deviation of points and wins

sapply(df[c('wins', 'points')], function(x) sqrt(wtd.var(x, wt)))

Code

R
#Step 1: Install Package
install.packages("Hmisc")
library(Hmisc)

#Step 2: Create dataset for For Multiple Columns of Data Frame
df <- data.frame(team=c('A', 'A', 'A', 'A', 'A', 'B', 'B', 'C'),
                 wins=c(2, 9, 11, 12, 15, 17, 18, 19),
                 points=c(1, 2, 2, 2, 3, 3, 3, 3))

#Step 3: Define weights
wt <- c(1, 1, 1.5, 2, 2, 1.5, 1, 2)

#Step 4: Calculate weighted standard deviation of points and wins
sapply(df[c('wins', 'points')], function(x) sqrt(wtd.var(x, wt)))


Output

  wins                        points 
4.9535723 0.6727938

Conclusion

In this article, we learnt about How to Calculate Weighted Standard Deviation in R. We learnt different examples for calculating Weighted Standard Deviation for One Vector, One Column of Data Frame and for Multiple Columns of Data Frame.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads