Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compute weighted mean in R?

Tags:

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.

like image 541
Frank Avatar asked Jun 12 '12 00:06

Frank


People also ask

How do you calculate weighted mean?

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.

What is weighted mean in central tendency?

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.

What is weighted mean in data analysis?

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.

Can you calculate a weighted median?

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.


2 Answers

Use weighted.mean:

> weighted.mean(z$size, z$count) [1] 4 
like image 70
Frank Avatar answered Oct 09 '22 00:10

Frank


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 
like image 30
Chase Avatar answered Oct 08 '22 23:10

Chase