tTest

fun tTest(sample: DoubleArray, mu: Double = 0.0, alternative: Alternative = Alternative.TWO_SIDED, confidenceLevel: Double = 0.95): TestResult(source)

Performs a one-sample t-test for whether the population mean equals mu.

The null hypothesis is that the true mean of the population from which sample was drawn equals mu. The test uses the Student's t-distribution with n-1 degrees of freedom, where n is the sample size.

Example:

val sample = doubleArrayOf(5.0, 6.0, 7.0, 5.5, 6.5)
val result = tTest(sample, mu = 5.0)
result.statistic // t-statistic
result.pValue // p-value
result.confidenceInterval // 95% CI for the sample mean
result.isSignificant() // true if p < 0.05

Return

a TestResult containing the t-statistic, p-value, degrees of freedom (n-1), a confidence interval for the mean, and additional info with "mean" and "standardError".

Parameters

sample

the observed values. Must contain at least 2 elements.

mu

the hypothesized population mean. Defaults to 0.0.

alternative

the direction of the alternative hypothesis. Defaults to Alternative.TWO_SIDED, which tests whether the true mean differs from mu in either direction.

confidenceLevel

the confidence level for the confidence interval. Defaults to 0.95 (95%).


fun tTest(sample1: DoubleArray, sample2: DoubleArray, alternative: Alternative = Alternative.TWO_SIDED, equalVariances: Boolean = false, confidenceLevel: Double = 0.95): TestResult(source)

Performs a two-sample t-test for whether two populations have the same mean.

The null hypothesis is that the two populations from which sample1 and sample2 were drawn have equal means. By default, uses Welch's t-test which does not assume equal variances. Set equalVariances to true for the pooled (Student's) variant when the populations are known to have similar variances.

Example:

val control = doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0)
val treatment = doubleArrayOf(6.0, 7.0, 8.0, 9.0, 10.0)
val result = tTest(control, treatment)
result.statistic // t-statistic
result.pValue // p-value
result.confidenceInterval // 95% CI for the difference in means

Return

a TestResult containing the t-statistic, p-value, degrees of freedom, a confidence interval for the difference in means, and additional info with "mean1", "mean2", and "meanDifference".

Parameters

sample1

the first sample. Must contain at least 2 elements.

sample2

the second sample. Must contain at least 2 elements.

alternative

the direction of the alternative hypothesis. Defaults to Alternative.TWO_SIDED, which tests whether the means differ in either direction.

equalVariances

whether to assume equal variances in both populations. Defaults to false (Welch's t-test). Set to true for the pooled (Student's) t-test.

confidenceLevel

the confidence level for the confidence interval. Defaults to 0.95 (95%).