如何使用小于3个观察值进行双样本 z 检验?

huangapple go评论96阅读模式
英文:

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 &lt;- 0.2
x &lt;- c(1, 1.5, 1)
y &lt;- c(2, 2.5, 2)

z.test(x, y, sigma.x = s, sigma.y = s)
#&gt; 
#&gt;  Two-sample z-Test
#&gt; 
#&gt; data:  x and y
#&gt; z = -6.1237, p-value = 9.141e-10
#&gt; alternative hypothesis: true difference in means is not equal to 0
#&gt; 95 percent confidence interval:
#&gt;  -1.3200608 -0.6799392
#&gt; sample estimates:
#&gt; mean of x mean of y 
#&gt;  1.166667  2.166667

x &lt;- 1
y &lt;- 2

z.test(x, y, sigma.x = s, sigma.y = s)
#&gt; 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(&quot;z test&quot;)查找,找到了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(&quot;z test&quot;) 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)

huangapple
  • 本文由 发表于 2023年6月27日 21:13:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76565263.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定