2017-06-03 13 views
1

Suche nach einer Methode zur Berechnung der Standardabweichung der Population in R - mit mehr als 10 Proben. Der Quell-C-Code konnte nicht in R extrahiert werden, um die Berechnungsmethode zu finden.Berechnung der Standardabweichung der Bevölkerung in R

# 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 

Jetzt muss die Standardabweichung der Population sd (x) mit 100 zählen.

# 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 

Antwort

3

Bitte überprüfen Sie die Frage. Das erste Argument von rnorm sollte n sein.

Die Bevölkerung und Probenstandardabweichungen sind:

sqrt((n-1)/n) * sd(x) # pop 
## [1] 0.8936971 

sd(x) # sample 
## [1] 0.8981994 

Sie können auch wie folgt berechnet werden:

library(sqldf) 
library(RH2) 

sqldf("select stddev_pop(x), stddev_samp(x) from X") 
## STDDEV_POP("x") STDDEV_SAMP("x") 
## 1  0.8936971  0.8981994 

Hinweis: Wir werden diese Testdaten verwendet:

set.seed(1) 
n <- 100 
x <- rnorm(n) 
X <- data.frame(x) 
+0

Th Anks für die Hilfe. Es sieht so aus, als wäre es möglich, ohne Mittelwert/var/sd zu berechnen - siehe unten. – eyeOfTheStorm

+2

aber warum möchten Sie ...? –

+0

Danke für das RH2-Beispiel – eyeOfTheStorm

0
## Sample Standard Deviation 
n <- 10 # Sample count 
set.seed(1) 
x <- rnorm(n, 10) 

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


## Population Standard Deviation 
n <- 100 # Note: 10 or greater biases var() and sd() 
set.seed(1) 
x <- rnorm(n, 10) 

sd(x) # Incorrect Population Standard Deviation!! 
# [1] 0.8981994 
sqrt(sum((x - mean(x))^2)/(n)) # Correct 
# [1] 0.8936971 
sqrt(sum(x^2 - 2*mean(x)*x + mean(x)^2)/(n)) # Correct 
# [1] 0.8936971 
sqrt((sum(x^2) - (sum(x)^2/n))/(n)) # Correct 
# [1] 0.8936971 
sqrt((n/(n)) * ((sum(x^2)/(n)) - (sum(x)/n) ^2)) # Correct 
# [1] 0.8936971 
Verwandte Themen