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

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

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个观测值时失败。

  1. library(BSDA)
  2. s <- 0.2
  3. x <- c(1, 1.5, 1)
  4. y <- c(2, 2.5, 2)
  5. z.test(x, y, sigma.x = s, sigma.y = s)
  6. #>
  7. #> Two-sample z-Test
  8. #>
  9. #> data: x and y
  10. #> z = -6.1237, p-value = 9.141e-10
  11. #> alternative hypothesis: true difference in means is not equal to 0
  12. #> 95 percent confidence interval:
  13. #> -1.3200608 -0.6799392
  14. #> sample estimates:
  15. #> mean of x mean of y
  16. #> 1.166667 2.166667
  17. x <- 1
  18. y <- 2
  19. z.test(x, y, sigma.x = s, sigma.y = s)
  20. #> 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.

  1. library(BSDA)
  2. s &lt;- 0.2
  3. x &lt;- c(1, 1.5, 1)
  4. y &lt;- c(2, 2.5, 2)
  5. z.test(x, y, sigma.x = s, sigma.y = s)
  6. #&gt;
  7. #&gt; Two-sample z-Test
  8. #&gt;
  9. #&gt; data: x and y
  10. #&gt; z = -6.1237, p-value = 9.141e-10
  11. #&gt; alternative hypothesis: true difference in means is not equal to 0
  12. #&gt; 95 percent confidence interval:
  13. #&gt; -1.3200608 -0.6799392
  14. #&gt; sample estimates:
  15. #&gt; mean of x mean of y
  16. #&gt; 1.166667 2.166667
  17. x &lt;- 1
  18. y &lt;- 2
  19. z.test(x, y, sigma.x = s, sigma.y = s)
  20. #&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的维护者相同!)似乎可以正常工作:

  1. PASWR::z.test(x, y, sigma.x = s, sigma.y = s)
  2. 双样本z检验
  3. 数据:x y
  4. z = -3.5355p = 0.000407
  5. 备择假设:均值的真实差异不等于0
  6. 95%置信区间:
  7. -1.5543615 -0.4456385
  8. 样本估计:
  9. x的平均值 y的平均值
  10. 1 2

p值与直接计算一致:

  1. 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:

  1. PASWR::z.test(x, y, sigma.x = s, sigma.y = s)
  2. Two Sample z-test
  3. data: x and y
  4. z = -3.5355, p-value = 0.000407
  5. alternative hypothesis: true difference in means is not equal to 0
  6. 95 percent confidence interval:
  7. -1.5543615 -0.4456385
  8. sample estimates:
  9. mean of x mean of y
  10. 1 2

The p-value agrees with the direct calculation:

  1. 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:

确定