Home

Variance in R

|
|  Updated:  
2017-07-04 2:42:11
Statistical Analysis with R Essentials For Dummies
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, is a cognitive scientist and statistical analyst. He creates online learning tools and writes books on the technology of data science. His books include R All-in-One For Dummies and R Projects For Dummies.