如何计算R中多个标准偏差的平均值?
当我有几个标准偏差时,我正在尝试弄清楚如何计算数据集的标准偏差。让我们看一下此MWE:
set.seed(1234)
dummy_data <- data.frame(
"col_1" = sample(1:7, size = 10, replace = TRUE),
"col_2" = sample(1:7, size = 10, replace = TRUE),
"col_3" = sample(1:7, size = 10, replace = TRUE),
"col_4" = sample(1:7, size = 10, replace = TRUE)
)
现在,由于我知道所有数据点,所以我可以计算总标准偏差,如下所示:
> sd(as.matrix(dummy_data))
[1] 1.727604
但是我手头的真实数据如下:
> dplyr::summarise_all(dummy_data, sd)
col_1 col_2 col_3 col_4
1 1.837873 1.873796 1.37032 1.888562
如果我遵循通常的方法来计算多个的平均值标准偏差具有相似的样本尺寸,我将应用以下内容:
sds <- dplyr::summarise_all(dummy_data, sd)
vars <- sds^2
mean_sd <- sqrt(sum(vars) / (length(vars) - 1))
> mean_sd
[1] 2.027588
这是不一样的!现在,我已经尝试了没有减:
> sqrt(sum(vars) / (length(vars)))
[1] 1.755942
这无法解决问题。我尝试过这样的定义自己的标准偏差函数:
own_sd <- function(x) {
sqrt(sum((x - mean(x))^2) / length(x))
}
在x -1
中消除dplyr :: summarise_all()
步骤,然后根据上述步骤平均:
> sqrt(sum(dplyr::summarise_all(dummy_data, own_sd)^2) / 3)
[1] 1.923538
> sqrt(sum(dplyr::summarise_all(dummy_data, own_sd)^2) / 4)
[1] 1.665833
但是,所有这些都与sd(as.matrix())
方法给出了不同的结果。这里出了什么问题?
I am trying to figure out how to calculate the standard deviation of a dataset when I have a couple of standard deviations. Let's just look at this MWE:
set.seed(1234)
dummy_data <- data.frame(
"col_1" = sample(1:7, size = 10, replace = TRUE),
"col_2" = sample(1:7, size = 10, replace = TRUE),
"col_3" = sample(1:7, size = 10, replace = TRUE),
"col_4" = sample(1:7, size = 10, replace = TRUE)
)
Now since I know all the data points I can calculate the total standard deviation as follows:
> sd(as.matrix(dummy_data))
[1] 1.727604
But the real data that I have at hand is the following:
> dplyr::summarise_all(dummy_data, sd)
col_1 col_2 col_3 col_4
1 1.837873 1.873796 1.37032 1.888562
If I follow the usual method of calculating the average of multiple standard deviations with similar sample sizes, I would apply the following:
sds <- dplyr::summarise_all(dummy_data, sd)
vars <- sds^2
mean_sd <- sqrt(sum(vars) / (length(vars) - 1))
> mean_sd
[1] 2.027588
which is not the same! Now I have tried without the minus one:
> sqrt(sum(vars) / (length(vars)))
[1] 1.755942
which does not solve the problem. I have tried defining an own standard deviation function like this:
own_sd <- function(x) {
sqrt(sum((x - mean(x))^2) / length(x))
}
to eliminate the x - 1
in the dplyr::summarise_all()
step, and then average according to the step above:
> sqrt(sum(dplyr::summarise_all(dummy_data, own_sd)^2) / 3)
[1] 1.923538
> sqrt(sum(dplyr::summarise_all(dummy_data, own_sd)^2) / 4)
[1] 1.665833
But all seem to give a different result than the sd(as.matrix())
method. What is going wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法仅了解组SD的全局SD。例如:
这表明,即使样本大小相同,也知道两组的标准偏差也不能帮助您计算这些组的标准偏差。
根据Merijn van Tilborg的评论,如果您知道小组的大小和小组的含义,那么计算是可能的如下所示。
You can't calculate a global SD from only knowing group SDs. For example:
This demonstrates that even if the sample sizes are identical, knowing the standard deviation of two groups does not help you calculate the standard deviation of those groups combined.
As per Merijn van Tilborg's comment, if you know the group sizes and the group means, the calculation is possible as shown here.