Fast standard deviation with weights

I wanted to use a function that would quickly give me the standard deviation of a vector declaration so that I could include weights for the elements in the vector. i.e.

sd(c(1,2,3)) #weights all equal 1 #[1] 1 sd(c(1,2,3,3,3)) #weights equal 1,1,3 respectively #[1] 0.8944272 

For weighted values, I can use wt.mean() from library(SDMTools) , for example.

 > mean(c(1,2,3)) [1] 2 > wt.mean(c(1,2,3),c(1,1,1)) [1] 2 > > mean(c(1,2,3,3,3)) [1] 2.4 > wt.mean(c(1,2,3),c(1,1,3)) [1] 2.4 

but the wt.sd function does not seem to provide what I thought I wanted:

 > sd(c(1,2,3)) [1] 1 > wt.sd(c(1,2,3),c(1,1,1)) [1] 1 > sd(c(1,2,3,3,3)) [1] 0.8944272 > wt.sd(c(1,2,3),c(1,1,3)) [1] 1.069045 

I expect a function that returns 0.8944272 from me with a weighted sd . Preferably, I would use this on data.frame, for example:

 data.frame(x=c(1,2,3),w=c(1,1,3)) 
+6
source share
2 answers
 library(Hmisc) sqrt(wtd.var(1:3,c(1,1,3))) #[1] 0.8944272 
+9
source

You can use rep to replicate values ​​according to their weights. Then sd can be calculated for the resulting vector.

 x <- c(1, 2, 3) # values w <- c(1, 1, 3) # weights sd(rep(x, w)) [1] 0.8944272 
+3
source

Source: https://habr.com/ru/post/951388/


All Articles