Calculating Population Standard Deviation in R

Viewed 25761

Looking for a way to calculate Population Standard Deviation in R -- using greater than 10 samples. Unable to extract the source C code in R to find the method of calculation.

# Sample Standard Deviation 
# Note: All the below match with 10 or less samples
n <- 10 # 10 or greater it shifts calculation
set.seed(1)
x <- rnorm(n, 10)

# Sample Standard Deviation
sd(x)
# [1] 0.780586
sqrt(sum((x - mean(x))^2)/(n - 1))
# [1] 0.780586
sqrt(sum(x^2 - 2*mean(x)*x + mean(x)^2)/(n - 1)) # # Would like the Population Standard Deviation equivalent using this.
# [1] 0.780586
sqrt( (n/(n-1)) * ( ( (sum(x^2)/(n)) ) - (sum(x)/n) ^2 ) )
# [1] 0.780586

Now, the Population Standard Deviation needs to match sd(x) with 100 count.

# Population Standard Deviation 
n <- 100 
set.seed(1)
x <- rnorm(x, 10)

sd(x)
# [1] 0.780586

sqrt(sum((x - mean(x))^2)/(n))
# [1] 0.2341758

sqrt(sum(x^2 - 2*mean(x)*x + mean(x)^2)/(n)) 
# [1] 0.2341758

# Got this to work above using (eventual goal, to fix the below):
# https://en.wikipedia.org/wiki/Algebraic_formula_for_the_variance
sqrt( (n/(n-1)) * ( ( (sum(x^2)/(n)) ) - (sum(x)/n) ^2 ) )  # Would like the Population Standard Deviation equivalent using this.
# [1] 3.064027
4 Answers

I think that the easiest way is to just define it quickly from sd:

sd.p=function(x){sd(x)*sqrt((length(x)-1)/length(x))}

I have just spent considerable amount of time looking for a package with a ready function for population standard deviation. These are the results:

1) radiant.data::sdpop should be a good function (see documentation)

2) multicon::popsd also works well, but check the documentation to understand what the second argument is

3) muStat::stdev with the unbiased=FALSE does not work properly. On the github page it seems that in 2012 someone set it to be sd(x)*(1-1/length(x)) instead of sd(x)*sqrt(1-1/length(x))...

4) rfml::sd.pop will not work without ml.data.frame (MarkLogic Server)

I hope this helps.

Related