合并R中标准偏差的现有功能?

合并R中标准偏差的现有功能?,r,aggregate,standard-deviation,R,Aggregate,Standard Deviation,我有4个已知均值和标准差的总体。我想知道grand mean和grand sd。grand mean显然很容易计算,但R有一个方便的实用函数,weighted.mean()。是否存在用于组合标准偏差的类似函数 ,但现有的函数将使我的代码更干净、更容易理解 还有一个问题,你用什么工具来搜索这样的函数?我知道它一定在那里,但我做了很多搜索,却找不到它。谢谢 种群是否不重叠 library(fishmethods) combinevar 例如,维基百科中的示例如下: xbar <- c(70,

我有4个已知均值和标准差的总体。我想知道grand mean和grand sd。grand mean显然很容易计算,但R有一个方便的实用函数,weighted.mean()。是否存在用于组合标准偏差的类似函数

,但现有的函数将使我的代码更干净、更容易理解


还有一个问题,你用什么工具来搜索这样的函数?我知道它一定在那里,但我做了很多搜索,却找不到它。谢谢

种群是否不重叠

library(fishmethods)
combinevar
例如,维基百科中的示例如下:

xbar <- c(70,65)
s<-c(3,2)
n <- c(1,1)
combinevar(xbar,s,n)
combinevar <- 
function (xbar = NULL, s_squared = NULL, n = NULL) 
{
    if (length(xbar) != length(s_squared) | length(xbar) != length(n) | 
        length(s_squared) != length(n)) 
        stop("Vector lengths are different.")
    sum_of_squares <- sum((n - 1) * s_squared + n * xbar^2)
    grand_mean <- sum(n * xbar)/sum(n)
    combined_var <- (sum_of_squares - sum(n) * grand_mean^2)/(sum(n) - 
        1)
    return(c(grand_mean, combined_var))
}

xbar我不知道具体的包或函数名,但从维基百科的页面上滚动您自己的函数似乎很容易。假设人口没有重叠:

## N: vector of sizes
## M: vector of means
## S: vector of standard deviations

grand.mean <- function(M, N) {weighted.mean(M, N)}
grand.sd   <- function(S, M, N) {sqrt(weighted.mean(S^2 + M^2, N) -
                                      weighted.mean(M, N)^2)}
##N:大小向量
##M:平均向量
##S:标准偏差向量
grand.mean使用
utilities
包中的
sample.decomp
函数 这类统计问题现在已在中的
sample.decomp
函数中实现自动化。此函数可以从子组矩计算合并样本矩,或从其他子组矩和合并矩计算缺少的子组矩。它适用于高达四阶的分解,即样本大小、样本均值、样本方差/标准差、样本偏度和样本峰度的分解


如何使用该函数:这里我们给出一个示例,使用该函数计算由四个子组组成的集合样本的样本矩。为此,我们首先生成一个模拟数据集
DATA
,其中包含四个大小不等的子组,并将这些子组作为单个数据集
pool
。使用同一软件包中的
函数可以获得子组和合并样本的矩

#Create some subgroups of mock data and a pooled dataset
set.seed(1)
N    <- c(28, 44, 51, 102)
SUB1 <- rnorm(N[1])
SUB2 <- rnorm(N[2])
SUB3 <- rnorm(N[3])
SUB4 <- rnorm(N[4])
DATA <- list(SUB1 = SUB1, SUB2 = SUB2, SUB3 = SUB3, SUB4 = SUB4)
POOL <- c(SUB1, SUB2, SUB3, SUB4)

#Show sample statistics for the subgroups
library(utilities)
moments(DATA)

       n sample.mean sample.var sample.skew sample.kurt NAs
SUB1  28  0.09049834  0.9013829  -0.7648008    3.174128   0
SUB2  44  0.18637936  0.8246700   0.3653918    3.112901   0
SUB3  51  0.05986594  0.6856030   0.3076281    2.306243   0
SUB4 102 -0.05135660  1.0526184   0.3348429    2.741974   0

#Show sample statistics for the pooled sample
moments(POOL)

       n sample.mean sample.var sample.skew sample.kurt NAs
POOL 225  0.03799749  0.9030244   0.1705622    2.828833   0

如您所见,
sample.decomp
函数允许计算合并样本方差。您可以在中阅读有关此功能的信息。

关于您的搜索问题,sos软件包中的
findFn
是一个方便的工具。@AndresT,是的,总体是不重叠的。@joran谢谢,我不知道,我将以这种方式开始搜索。我猜“标准偏差”将成为R手册中的一个流行术语,非常感谢这个答案。当我在维基百科上看到这个公式时,我不认为我可以让计算看起来像你那样简单。事实上,我可能只是用这个,但对于其他发现这个问题的人来说,安德烈的回答更全面一些。谢谢
#Compute sample statistics for subgroups
library(utilities)
MEAN   <- c(mean(SUB1), mean(SUB2), mean(SUB3), mean(SUB4))
VAR    <- c( var(SUB1),  var(SUB2),  var(SUB3),  var(SUB4))

#Compute sample decomposition
sample.decomp(n = N, sample.mean = MEAN, sample.var  = VAR, names = names(DATA))

             n sample.mean sample.var
SUB1        28  0.09049834  0.9013829
SUB2        44  0.18637936  0.8246700
SUB3        51  0.05986594  0.6856030
SUB4       102 -0.05135660  1.0526184
--pooled-- 225  0.03799749  0.9030244