How do I compute the weighted mean in R
?
For example, I have 4 elements of which 1 element is of size (or: length, width, etc.) 10 and 3 elements are of size 2.
> z = data.frame(count=c(1,3), size=c(10,2)) > z count size 1 1 10 2 3 2
The weighted average is (10 * 1 + 2 * 3) / 4 = 4
.
The weighted mean is a type of mean that is calculated by multiplying the weight (or probability) associated with a particular event or outcome with its associated quantitative outcome and then summing all the products together.
Weighted mean is calculated when certain values in a data set are more important than the others. [9] A weight wi is attached to each of the values xi to reflect this importance.
The weighted mean involves multiplying each data point in a set by a value which is determined by some characteristic of whatever contributed to the data point.
If the total number of occurrences (let's call it 'n', i.e. the sum of the frequencies / the total number of students) is odd, then the median is the ((n+1) / 2)-th value. If n is even, then the median is the average of the (n/2)-th and the ((n/2) + 1)-th value.
Use weighted.mean
:
> weighted.mean(z$size, z$count) [1] 4
Seems like you already know how to calculate this, just need a nudge in the right direction to implement it. Since R is vectorized, this is pretty simple:
with(z, sum(count*size)/sum(count))
The with
bit just saves on typing and is equivalent to sum(z$count*z$size)/sum(z$count)
Or use the built in function weighted.mean()
as you also pointed out. Using your own function can prove faster, though will not do the same amount of error checking that the builtin function does.
builtin <- function() with(z, weighted.mean(count, size)) rollyourown <- function() with(z, sum(count*size)/sum(count)) require(rbenchmark) benchmark(builtin(), rollyourown(), replications = 1000000, columns = c("test", "elapsed", "relative"), order = "relative") #----- test elapsed relative 2 rollyourown() 13.26 1.000000 1 builtin() 22.84 1.722474
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With