sunu icon

R population variance & population standard deviation

sunu | PRO | 09/03/14 05:28:35 PM UTC | 0 ⭐ | 724 👁️ | Never ⏰ | []
R |

1.43 KB

|

None

|

0 👍

/

0 👎

# AFAIK dalam standar, R hanya menghitung SD dari vektor sebagai SD dari sample, SD(x).
# Berikut untuk menghitung SD dari data dan memperlakukannya sebagai populasi.
# 
#
# Sunu Pradana
# Samarinda, Indonesia, Agustus 2014
#          
# Creative Commons Attribution-ShareAlike 4.0
#
# Usage (contoh penggunaan):
# popVarSd(x) #x adalah vektor
 
popVarSd <- function(x){
 
populationVariance <- sum((x - mean(x))^2)/length(x)
populationStandardDeviation <- sqrt(sum((x - mean(x))^2)/length(x))
nSample <- length(x)
 
cat("\n")
cat( sprintf("%-31s %1s % 15.4f %10s % 10.2f ","Jumlah sample",                ":", nSample,                    "[trunc2:]", round(nSample,2)),                     fill = TRUE )
cat( sprintf("%-31s %1s % 15.4f %10s % 10.2f ","Population mean",              ":", mean(x),                    "[trunc2:]", round(mean(x),2)),                     fill = TRUE )
cat( sprintf("%-31s %1s % 15.4f %10s % 10.2f ","Population Variance          ",":", populationVariance,         "[trunc2:]", round(populationVariance,2)),          fill = TRUE )    
cat( sprintf("%-31s %1s % 15.4f %10s % 10.2f ","Population Standard Deviation",":", populationStandardDeviation,"[trunc2:]", round(populationStandardDeviation,2)), fill = TRUE )    
cat( sprintf("%-31s %1s % 15.4f %10s % 10.2f ","as Sample Standard Deviation", ":", sd(x)                      ,"[trunc2:]", round(sd(x),2)),                       fill = TRUE )    
 
cat("\n")
}

Comments