我正在使用 Apache Math3 对网站项目进行学生 t 测试。假设我有两个样本:
double[] sampleOne = new double[] {134 ,146 ,104 ,119 ,124 ,161 ,107 ,83 ,113 ,129 ,97 ,123};
double[] sampleTwo = new double[] { 70, 118, 101, 85, 107, 132, 94};
此数据复制自 https://www.statsdirect.com/help/parametric_methods/unpaired_t.htm
我希望计算出如上页所示的置信区间。例如:
Assuming equal variances
95% confidence interval for difference between means = -2.193679 to 40.193679
我找到了这个链接:
Using Apache Commons Math to determine confidence intervals ,它显示了这个方法:
private double getConfidenceIntervalWidth(StatisticalSummary statistics, double significance) {
TDistribution tDist = new TDistribution(statistics.getN() - 1);
double a = tDist.inverseCumulativeProbability(1.0 - significance / 2);
return a * statistics.getStandardDeviation() / Math.sqrt(statistics.getN());
}
这似乎不适用于 t 检验中的两个样本。我做了很多研究,但没能找到如何使用 Apache Math3 来做到这一点。
最佳答案
我知道这可能是一个非常晚的回复,但我会尽力回答您的问题。假设您有两个未配对的样本 sampleOne
和 sampleTwo
(它们因大小不同而未配对),您可以使用以下方法来计算 t 统计量:
DescriptiveStatistics one = new DescriptiveStatistics();
for (double d : sampleOne)
one.addValue(d);
DescriptiveStatistics two = new DescriptiveStatistics();
for (double d : sampleTwo)
two.addValue(d);
double tStat = TestUtils.t(one, two);
请注意,除了 DescriptiveStatistics
,您也可以使用 SummaryStatistics
。相反,如果您想要 p 值,您可以执行以下操作:
double pVal = TestUtils.tTest(sampleOne, sampleTwo);
最后,如果您想以给定的置信度运行完整测试(我们称之为 double conf = 0.95
),那么您可以执行:
TestUtils.tTest(sampleOne, sampleTwo, 1.0 - conf)
谈到获取下边距和上边距,Apache Commons Math 没有直接支持这一点。该公式似乎适用于未配对的 t 检验,但请记住,您的样本方差需要相等(如您提供的网站所示)。
关于java - Apache Math : calculate the confidence interval in a t-test, 有两个样本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47002374/