var()
function. But which variance does it give you? The one with N in the denominator or the one with N-1? Time to find out:heights <- c(50, 47, 52, 46, 45)
> var(heights)
[1] 8.5
It calculates the estimated variance (with N–1 in the denominator). To calculate that first variance with N in the denominator, you have to multiply this number by (N–1)/N. Using length()
to calculate N, that's
var(heights)*(length(heights)-1)/length(heights)
[1] 6.8
If you were going to work with this kind of variance frequently, define a function var.p()
:
var.p = function(x){var(x)*(length(x)-1)/length(x)}
And here's how to use it:
> var.p(heights)
[1] 6.8
Think of the denominator of a variance estimate (like N–1) as degrees of freedom.