Weighted average designation - Weight adjustment in R

I am trying to calculate the weighted average of a statistical sample (vector) in R using this form:

form

The function takes a vector, and the weight is adjusted in accordance with the second parameter (1 - 3), which:

weights

where sis the standard deviation.

I adjusted the weight accordingly if the parameter is 1 or 3 using else-if, but I am having problems with the second, if you have criteria to satisfy ...

I calculated X - xBaras a vector:m = x-mean(x)

I calculated susing the function R :s = sd(x)

, " " . if , ...

( , ,), x (m/s) 1? 3 ?

. 1, 1, 1 2, 0,5?

, . R , , ... , .

.

+3
1

, , , ifelse, if else, , -

m <- mean(x)
s <- sd(x)
absstandardx <- abs( (x - m) / s )
w2 <- ifelse( absstandardx < 1, 1, ifelse( absstandardx < 2, 0.5, 0 ) ) 
weightedmean2 <- sum(w2 * x) / sum(w2)
+2

All Articles