如何计算R中多个标准偏差的平均值?

发布于 2025-01-27 19:59:46 字数 1378 浏览 1 评论 0原文

当我有几个标准偏差时,我正在尝试弄清楚如何计算数据集的标准偏差。让我们看一下此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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是伱的 2025-02-03 19:59:46

您无法仅了解组SD的全局SD。例如:

x1 = 1:5
x2 = 11:15
x3 = 101:105

## all the SDs are equal
(sd1 = sd(x1))
#[1] 1.581139
(sd2 = sd(x2))
#[1] 1.581139
(sd3 = sd(x3))
#[1] 1.581139

## however, combining the groups in pairs give very different results
sd(c(x1, x2))
# [1] 5.477226

sd(c(x1, x3))
# [1] 52.72571

这表明,即使样本大小相同,也知道两组的标准偏差也不能帮助您计算这些组的标准偏差。

根据Merijn van Tilborg的评论,如果您知道小组的大小和小组的含义,那么计算是可能的如下所示

You can't calculate a global SD from only knowing group SDs. For example:

x1 = 1:5
x2 = 11:15
x3 = 101:105

## all the SDs are equal
(sd1 = sd(x1))
#[1] 1.581139
(sd2 = sd(x2))
#[1] 1.581139
(sd3 = sd(x3))
#[1] 1.581139

## however, combining the groups in pairs give very different results
sd(c(x1, x2))
# [1] 5.477226

sd(c(x1, x3))
# [1] 52.72571

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文