使用utilities包中的sample.decomp函数
此类统计问题现已在 utilities package 的 sample.decomp 函数中自动完成。该函数可以从子组矩中计算池化样本矩,或者从其他子组矩和池化矩中计算缺失的子组矩。它适用于高达四阶的分解——即样本大小、样本均值、样本方差/标准差、样本偏度和样本峰度的分解。
如何使用函数:这里我们给出一个例子,我们使用该函数计算由四个子组组成的池样本的样本矩。为此,我们首先生成一个模拟数据集DATA,其中包含四个大小不等的子组,并将它们合并为单个数据集POOL。使用同一包中的moments 函数可以获得子组和合并样本的矩。
#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 函数从子组样本矩中获取合并的样本矩。作为此函数的输入,您可以将moments 输出用于子组,也可以将样本大小和样本矩分别作为向量输入(这里我们将使用后者)。如您所见,这为合并样本提供了与从基础数据直接计算相同的样本时刻。
#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
如您所见,sample.decomp 函数允许计算合并样本方差。您可以在package documentation 阅读有关此功能的信息。