|
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:
We can use wt.var() function from the Hmisc package to Calculate Weighted Standard Deviation in R Example 1: For One VectorStep 1: Install Package install.packages("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 FrameStep 1: Install Package install.packages("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'), 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
Output 0.6727938 Example 3: For Multiple Columns of Data FrameStep 1: Install Package install.packages("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'), 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
Output wins points ConclusionIn 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. |
Reffered: https://www.geeksforgeeks.org
AI ML DS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |