Statistical Analysis with R For Dummies
Book image
Explore Book Buy On Amazon
Calculating variance in R is simplicity itself. You use the 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.

About This Article

This article is from the book:

About the book author:

Joseph Schmuller, PhD, has taught undergraduate and graduate statistics, and has 25 years of IT experience. The author of four editions of Statistical Analysis with Excel For Dummies and three editions of Teach Yourself UML in 24 Hours (SAMS), he has created online coursework for and is a former Editor in Chief of PC AI magazine. He is a Research Scholar at the University of North Florida.

This article can be found in the category: