G07 Chapter Contents
G07 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG07CAF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G07CAF 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

 SUBROUTINE G07CAF ( TAIL, EQUAL, NX, NY, XMEAN, YMEAN, XSTD, YSTD, CLEVEL, T, DF, PROB, DL, DU, IFAIL)
 INTEGER NX, NY, IFAIL REAL (KIND=nag_wp) XMEAN, YMEAN, XSTD, YSTD, CLEVEL, T, DF, PROB, DL, DU CHARACTER(1) TAIL, EQUAL

## 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.
G07CAF 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;
 $tobs=x--y- s⁢1/nx+1/ny$
where
 $s2 = nx-1 sx2 + ny-1 sy2 nx + ny - 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;
• ${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.
• ${H}_{1}:{\mu }_{x}>{\mu }_{y}$; the significance level, $p=P\left(t\ge {t}_{\mathrm{obs}}\right)$, i.e., an upper tail probability.
• ${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-±t1-α/2s⁢1/nx+1/ny.$
where ${t}_{1-\alpha /2}$ is the $100\left(1-\alpha /2\right)$ percentage point of the $t$-distribution with (${n}_{x}+{n}_{y}-2$) 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 (1969). The test used here is based on Satterthwaites procedure. To test the null hypothesis the test statistic ${t}^{\prime }$ is used where
 $tobs′=x--y- sex--y-$
where $\mathrm{se}\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 sx2 / nx 2 nx-1 + sy2 / ny 2 ny-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-±t1-α/2sex-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 (1969) The Advanced Theory of Statistics (Volume 1) (3rd Edition) Griffin
Snedecor G W and Cochran W G (1967) Statistical Methods Iowa State University Press

## 5  Parameters

1:     TAIL – CHARACTER(1)Input
On entry: indicates which tail probability is to be calculated, and thus which alternative hypothesis is to be used.
${\mathbf{TAIL}}=\text{'T'}$
The two tail probability, i.e., ${H}_{1}:{\mu }_{x}\ne {\mu }_{y}$.
${\mathbf{TAIL}}=\text{'U'}$
The upper tail probability, i.e., ${H}_{1}:{\mu }_{x}>{\mu }_{y}$.
${\mathbf{TAIL}}=\text{'L'}$
The lower tail probability, i.e., ${H}_{1}:{\mu }_{x}<{\mu }_{y}$.
Constraint: ${\mathbf{TAIL}}=\text{'T'}$, $\text{'U'}$ or $\text{'L'}$.
2:     EQUAL – CHARACTER(1)Input
On entry: indicates whether the population variances are assumed to be equal or not.
${\mathbf{EQUAL}}=\text{'E'}$
The population variances are assumed to be equal, that is ${\sigma }_{x}^{2}={\sigma }_{y}^{2}$.
${\mathbf{EQUAL}}=\text{'U'}$
The population variances are not assumed to be equal.
Constraint: ${\mathbf{EQUAL}}=\text{'E'}$ or $\text{'U'}$.
3:     NX – INTEGERInput
On entry: ${n}_{x}$, the size of the $X$ sample.
Constraint: ${\mathbf{NX}}\ge 2$.
4:     NY – INTEGERInput
On entry: ${n}_{y}$, the size of the $Y$ sample.
Constraint: ${\mathbf{NY}}\ge 2$.
5:     XMEAN – REAL (KIND=nag_wp)Input
On entry: $\stackrel{-}{x}$, the mean of the $X$ sample.
6:     YMEAN – REAL (KIND=nag_wp)Input
On entry: $\stackrel{-}{y}$, the mean of the $Y$ sample.
7:     XSTD – REAL (KIND=nag_wp)Input
On entry: ${s}_{x}$, the standard deviation of the $X$ sample.
Constraint: ${\mathbf{XSTD}}>0.0$.
8:     YSTD – REAL (KIND=nag_wp)Input
On entry: ${s}_{y}$, the standard deviation of the $Y$ sample.
Constraint: ${\mathbf{YSTD}}>0.0$.
9:     CLEVEL – REAL (KIND=nag_wp)Input
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:   T – REAL (KIND=nag_wp)Output
On exit: contains the test statistic, ${t}_{\mathrm{obs}}$ or ${t}_{\mathrm{obs}}^{\prime }$.
11:   DF – REAL (KIND=nag_wp)Output
On exit: contains the degrees of freedom for the test statistic.
12:   PROB – REAL (KIND=nag_wp)Output
On exit: contains the significance level, that is the tail probability, $p$, as defined by TAIL.
13:   DL – REAL (KIND=nag_wp)Output
On exit: contains the lower confidence limit for ${\mu }_{x}-{\mu }_{y}$.
14:   DU – REAL (KIND=nag_wp)Output
On exit: contains the upper confidence limit for ${\mu }_{x}-{\mu }_{y}$.
15:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{TAIL}}\ne \text{'T'}$, $\text{'U'}$ or $\text{'L'}$, or ${\mathbf{EQUAL}}\ne \text{'E'}$ or $\text{'U'}$, or ${\mathbf{NX}}<2$, or ${\mathbf{NY}}<2$, or ${\mathbf{XSTD}}\le 0.0$, or ${\mathbf{YSTD}}\le 0.0$, or ${\mathbf{CLEVEL}}\le 0.0$, or ${\mathbf{CLEVEL}}\ge 1.0$.

## 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 G01ATF.

## 9  Example

This example 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 (g07cafe.f90)

### 9.2  Program Data

Program Data (g07cafe.d)

### 9.3  Program Results

Program Results (g07cafe.r)