英文:
How to do a two-sample z-test with <3 observations?
问题
在R中执行z-tests的一种通用方式似乎是使用BSDA
包中的z.test
函数。然而,它似乎不适用于少于3个观测值的情况。这让我感到困惑,因为z-test不应该需要超过一个观测值。它不应该使用样本来估计方差 - 它应该使用输入的sigma。
我是否做错了什么?在R中是否有更好的执行z-tests的方法?
在这里,您可以看到在每个组中有3个观测值时z-test成功,而在只有1个观测值时失败。
library(BSDA)
s <- 0.2
x <- c(1, 1.5, 1)
y <- c(2, 2.5, 2)
z.test(x, y, sigma.x = s, sigma.y = s)
#>
#> Two-sample z-Test
#>
#> data: x and y
#> z = -6.1237, p-value = 9.141e-10
#> alternative hypothesis: true difference in means is not equal to 0
#> 95 percent confidence interval:
#> -1.3200608 -0.6799392
#> sample estimates:
#> mean of x mean of y
#> 1.166667 2.166667
x <- 1
y <- 2
z.test(x, y, sigma.x = s, sigma.y = s)
#> Error in z.test(x, y, sigma.x = s, sigma.y = s): not enough x observations
创建于2023-06-27,使用reprex v2.0.2
英文:
The accepted way to do z-tests in R seems to be the z.test
function from BSDA
package. However, it doesn't seem to work with less than 3 observations. This is confusing to me, because the z-test shouldn't require more than one observation. It shouldn't use the sample to estimate the variance - it should use the inputted sigma.
Am I doing something wrong? Is there a better way to do z-tests in R?
Here you can see the z-test succeed with 3 observations in each group and fail with 1.
library(BSDA)
s <- 0.2
x <- c(1, 1.5, 1)
y <- c(2, 2.5, 2)
z.test(x, y, sigma.x = s, sigma.y = s)
#>
#> Two-sample z-Test
#>
#> data: x and y
#> z = -6.1237, p-value = 9.141e-10
#> alternative hypothesis: true difference in means is not equal to 0
#> 95 percent confidence interval:
#> -1.3200608 -0.6799392
#> sample estimates:
#> mean of x mean of y
#> 1.166667 2.166667
x <- 1
y <- 2
z.test(x, y, sigma.x = s, sigma.y = s)
#> Error in z.test(x, y, sigma.x = s, sigma.y = s): not enough x observations
<sup>Created on 2023-06-27 with reprex v2.0.2</sup>
答案1
得分: 2
我同意评论者的观点,似乎没有什么好理由BSDA::z.test
不能处理每组n为1的两样本Z检验。
我用library(sos); findFn("z test")
查找,找到了misty::test.z
,但它似乎也有问题。
然而,PASWR
包(与BSDA
的维护者相同!)似乎可以正常工作:
PASWR::z.test(x, y, sigma.x = s, sigma.y = s)
双样本z检验
数据:x 和 y
z = -3.5355,p值 = 0.000407
备择假设:均值的真实差异不等于0
95%置信区间:
-1.5543615 -0.4456385
样本估计:
x的平均值 y的平均值
1 2
p值与直接计算一致:
2*pnorm(abs(x-y)/sqrt(s^2+s^2), lower.tail = FALSE)
英文:
I agree with the commenters that there doesn't seem to be any good reason that BSDA::z.test
can't handle a two-sample Z test with an n (per group) of 1.
I looked around with library(sos); findFn("z test")
and found misty::test.z
, but that also seems to have issues.
However, the PASWR
package (which has the same maintainer as BSDA
!) appears to work OK:
PASWR::z.test(x, y, sigma.x = s, sigma.y = s)
Two Sample z-test
data: x and y
z = -3.5355, p-value = 0.000407
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.5543615 -0.4456385
sample estimates:
mean of x mean of y
1 2
The p-value agrees with the direct calculation:
2*pnorm(abs(x-y)/sqrt(s^2+s^2), lower.tail = FALSE)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论