R population variance & population standard deviation

Sep 3rd, 2014
295
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. # AFAIK dalam standar, R hanya menghitung SD dari vektor sebagai SD dari sample, SD(x).
2. # Berikut untuk menghitung SD dari data dan memperlakukannya sebagai populasi.
3. #
4. #
6. # Samarinda, Indonesia, Agustus 2014
7. #
8. # Creative Commons Attribution-ShareAlike 4.0
9. #
10. # Usage (contoh penggunaan):
11. # popVarSd(x) #x adalah vektor
12.
13. popVarSd <- function(x){
14.
15. populationVariance <- sum((x - mean(x))^2)/length(x)
16. populationStandardDeviation <- sqrt(sum((x - mean(x))^2)/length(x))
17. nSample <- length(x)
18.
19. cat("\n")
20. cat( sprintf("%-31s %1s % 15.4f %10s % 10.2f ","Jumlah sample",                ":", nSample,                    "[trunc2:]", round(nSample,2)),                     fill = TRUE )
21. cat( sprintf("%-31s %1s % 15.4f %10s % 10.2f ","Population mean",              ":", mean(x),                    "[trunc2:]", round(mean(x),2)),                     fill = TRUE )
22. cat( sprintf("%-31s %1s % 15.4f %10s % 10.2f ","Population Variance          ",":", populationVariance,         "[trunc2:]", round(populationVariance,2)),          fill = TRUE )
23. cat( sprintf("%-31s %1s % 15.4f %10s % 10.2f ","Population Standard Deviation",":", populationStandardDeviation,"[trunc2:]", round(populationStandardDeviation,2)), fill = TRUE )
24. cat( sprintf("%-31s %1s % 15.4f %10s % 10.2f ","as Sample Standard Deviation", ":", sd(x)                      ,"[trunc2:]", round(sd(x),2)),                       fill = TRUE )
25.
26. cat("\n")
27. }