r - 计算R中的人口标准差

标签 r statistics variance

寻找一种方法来计算R中的人口标准差-使用10个以上的样本。无法提取R中的源C代码以找到计算方法。

# 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

现在,“人口标准偏差”需要将sd(x)与100计数匹配。
# 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

最佳答案

请检查问题。 rnorm的第一个参数应为n。

总体和样本标准差为:

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

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

它们也可以这样计算:
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

注意:我们使用了以下测试数据:
set.seed(1)
n <- 100
x <- rnorm(n)
X <- data.frame(x)

关于r - 计算R中的人口标准差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44339070/

相关文章:

r - 提高 RODBC-Postgres 写入性能

从 data.table 中删除重复出现的数据

r - R中将多列切割成大致相等的有序组的函数

mysql - 在 MySQL GROUP BY 中计数 "repeats"

python - 属性错误 : '_process_plot_var_args' object has no attribute 'get_next_color'

c# - 无法从 Derived<Derived 2T> 转换为 BaseT<base 2T>

r - 在 data.table 上按周期分组重复

iphone - iphone 有什么好的统计可视化库吗?

java - 计算方差/标准差 - JAVA

r - 如何使用 R 处理 OLS 中的异方差性