g07 Chapter Contents
g07 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_2_sample_t_test (g07cac)

## 1  Purpose

nag_2_sample_t_test (g07cac) computes a $t$-test statistic to test for a difference in means between two Normal populations, together with a confidence interval for the difference between the means.

## 2  Specification

 #include #include
 void nag_2_sample_t_test (Nag_TailProbability tail, Nag_PopVar equal, Integer nx, Integer ny, double xmean, double ymean, double xstd, double ystd, double clevel, double *t, double *df, double *prob, double *dl, double *du, NagError *fail)

## 3  Description

Consider two independent samples, denoted by $X$ and $Y$, of size ${n}_{x}$ and ${n}_{y}$ drawn from two Normal populations with means ${\mu }_{x}$ and ${\mu }_{y}$, and variances ${\sigma }_{x}^{2}$ and ${\sigma }_{y}^{2}$ respectively. Denote the sample means by $\stackrel{-}{x}$ and $\stackrel{-}{y}$ and the sample variances by ${s}_{x}^{2}$ and ${s}_{y}^{2}$ respectively.
nag_2_sample_t_test (g07cac) calculates a test statistic and its significance level to test the null hypothesis ${H}_{0}:{\mu }_{x}={\mu }_{y}$, together with upper and lower confidence limits for ${\mu }_{x}-{\mu }_{y}$. The test used depends on whether or not the two population variances are assumed to be equal.
1. It is assumed that the two variances are equal, that is ${\sigma }_{x}^{2}={\sigma }_{y}^{2}$.
The test used is the two sample $t$-test. The test statistic $t$ is defined by;
 $t obs = x - - y - s 1 / n x + 1 / n y$
where ${s}^{2}=\frac{\left({n}_{x}-1\right){s}_{x}^{2}+\left({n}_{y}-1\right){s}_{y}^{2}}{{n}_{x}+{n}_{y}-2}$ is the pooled variance of the two samples.
Under the null hypothesis ${H}_{0}$ this test statistic has a $t$-distribution with $\left({n}_{x}+{n}_{y}-2\right)$ degrees of freedom.
The test of ${H}_{0}$ is carried out against one of three possible alternatives:
1. ${H}_{1}:{\mu }_{x}\ne {\mu }_{y}$; the significance level, $p=P\left(t\ge \left|{t}_{\mathrm{obs}}\right|\right)$, i.e., a two tailed probability.
2. ${H}_{1}:{\mu }_{x}>{\mu }_{y}$; the significance level, $p=P\left(t\ge {t}_{\mathrm{obs}}\right)$, i.e., an upper tail probability.
3. ${H}_{1}:{\mu }_{x}<{\mu }_{y}$; the significance level, $p=P\left(t\le {t}_{\mathrm{obs}}\right)$, i.e., a lower tail probability.
Upper and lower $100\left(1-\alpha \right)$% confidence limits for ${\mu }_{x}-{\mu }_{y}$ are calculated as:
 $x - - y - ± t 1 - α / 2 s 1 / n x + 1 / n y ,$
where ${t}_{1-\alpha /2}$ is the $100\left(1-\alpha /2\right)$ percentage point of the $t$-distribution with $\left({n}_{x}+{n}_{y}-2\right)$ degrees of freedom.
2. It is not assumed that the two variances are equal.
If the population variances are not equal the usual two sample $t$-statistic no longer has a $t$-distribution and an approximate test is used.
This problem is often referred to as the Behrens–Fisher problem, see Kendall and Stuart (1979). The test used here is based on Satterthwaites procedure. To test the null hypothesis the test statistic ${t}^{\prime }$ is used where
 $t obs ′ = x - - y - se x - - y -$
where $\mathrm{se}\left(\stackrel{-}{x}-\stackrel{-}{y}\right)\left(\stackrel{-}{x}-\stackrel{-}{y}\right)=\sqrt{\frac{{s}_{x}^{2}}{{n}_{x}}+\frac{{s}_{y}^{2}}{{n}_{y}}}$.
A $t$-distribution with $f$ degrees of freedom is used to approximate the distribution of ${t}^{\prime }$ where
 $f = se⁡ x - - y - 4 s x 2 / n x 2 n x - 1 + s y 2 / n y 2 n y - 1 .$
The test of ${H}_{0}$ is carried out against one of the three alternative hypotheses described above, replacing $t$ by ${t}^{\prime }$ and ${t}_{\mathrm{obs}}$ by ${t}_{\mathrm{obs}}^{\prime }$.
Upper and lower $100\left(1-\alpha \right)$% confidence limits for ${\mu }_{x}-{\mu }_{y}$ are calculated as:
 $x - - y - ± t 1 - α / 2 se x - y - .$
where ${t}_{1-\alpha /2}$ is the $100\left(1-\alpha /2\right)$ percentage point of the $t$-distribution with $f$ degrees of freedom.

## 4  References

Johnson M G and Kotz A (1969) The Encyclopedia of Statistics 2 Griffin
Kendall M G and Stuart A (1979) The Advanced Theory of Statistics (3 Volumes) (4th Edition) Griffin
Snedecor G W and Cochran W G (1967) Statistical Methods Iowa State University Press

## 5  Arguments

1:     tailNag_TailProbabilityInput
On entry: indicates which tail probability is to be calculated, and thus which alternative hypothesis is to be used.
${\mathbf{tail}}=\mathrm{Nag_TwoTail}$
The two tail probability, i.e., ${H}_{1}:{\mu }_{x}\ne {\mu }_{y}$.
${\mathbf{tail}}=\mathrm{Nag_UpperTail}$
The upper tail probability, i.e., ${H}_{1}:{\mu }_{x}>{\mu }_{y}$.
${\mathbf{tail}}=\mathrm{Nag_LowerTail}$
The lower tail probability, i.e., ${H}_{1}:{\mu }_{x}<{\mu }_{y}$.
Constraint: ${\mathbf{tail}}=\mathrm{Nag_UpperTail}$, $\mathrm{Nag_LowerTail}$ or $\mathrm{Nag_TwoTail}$.
2:     equalNag_PopVarInput
On entry: indicates whether the population variances are assumed to be equal or not.
${\mathbf{equal}}=\mathrm{Nag_PopVarEqual}$
The population variances are assumed to be equal, that is ${\sigma }_{x}^{2}={\sigma }_{y}^{2}$.
${\mathbf{equal}}=\mathrm{Nag_PopVarNotEqual}$
The population variances are not assumed to be equal.
Constraint: ${\mathbf{equal}}=\mathrm{Nag_PopVarEqual}$ or $\mathrm{Nag_PopVarNotEqual}$.
3:     nxIntegerInput
On entry: the size of the $X$ sample, ${n}_{x}$.
Constraint: ${\mathbf{nx}}\ge 2$.
4:     nyIntegerInput
On entry: the size of the $Y$ sample, ${n}_{y}$.
Constraint: ${\mathbf{ny}}\ge 2$.
5:     xmeandoubleInput
On entry: the mean of the $X$ sample, $\stackrel{-}{x}$.
6:     ymeandoubleInput
On entry: the mean of the $Y$ sample, $\stackrel{-}{y}$.
7:     xstddoubleInput
On entry: the standard deviation of the $X$ sample, ${s}_{x}$.
Constraint: ${\mathbf{xstd}}>0.0$.
8:     ystddoubleInput
On entry: the standard deviation of the $Y$ sample, ${s}_{y}$.
Constraint: ${\mathbf{ystd}}>0.0$.
9:     cleveldoubleInput
On entry: the confidence level, $1-\alpha$, for the specified tail. For example ${\mathbf{clevel}}=0.95$ will give a 95% confidence interval.
Constraint: $0.0<{\mathbf{clevel}}<1.0$.
10:   tdouble *Output
On exit: contains the test statistic, ${t}_{\mathrm{obs}}$ or ${t}_{\mathrm{obs}}^{\prime }$.
11:   dfdouble *Output
On exit: contains the degrees of freedom for the test statistic.
12:   probdouble *Output
On exit: contains the significance level, that is the tail probability, $p$, as defined by tail.
13:   dldouble *Output
On exit: contains the lower confidence limit for ${\mu }_{x}-{\mu }_{y}$.
14:   dudouble *Output
On exit: contains the upper confidence limit for ${\mu }_{x}-{\mu }_{y}$.
15:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

On entry, argument equal had an illegal value.
On entry, argument tail had an illegal value.
NE_INT_ARG_LT
On entry, ${\mathbf{nx}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nx}}\ge 2$.
On entry, ${\mathbf{ny}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ny}}\ge 2$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
NE_REAL_ARG_GE
On entry, clevel must not be greater than or equal to 1.0: ${\mathbf{clevel}}=〈\mathit{\text{value}}〉$.
NE_REAL_ARG_LE
On entry, clevel must not be less than or equal to 0.0: ${\mathbf{clevel}}=〈\mathit{\text{value}}〉$.
On entry, xstd must not be less than or equal to 0.0: ${\mathbf{xstd}}=〈\mathit{\text{value}}〉$.
On entry, ystd must not be less than or equal to 0.0: ${\mathbf{ystd}}=〈\mathit{\text{value}}〉$.

## 7  Accuracy

The computed probability and the confidence limits should be accurate to approximately five significant figures.

The sample means and standard deviations can be computed using nag_summary_stats_1var (g01aac).

## 9  Example

The following example program reads the two sample sizes and the sample means and standard deviations for two independent samples. The data is taken from page 116 of Snedecor and Cochran (1967) from a test to compare two methods of estimating the concentration of a chemical in a vat. A test of the equality of the means is carried out first assuming that the two population variances are equal and then making no assumption about the equality of the population variances.

### 9.1  Program Text

Program Text (g07cace.c)

### 9.2  Program Data

Program Data (g07cace.d)

### 9.3  Program Results

Program Results (g07cace.r)