# NAG CL Interfaceg07cac (ttest_​2normal)

## 1Purpose

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.

## 2Specification

 #include
 void g07cac (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)
The function may be called by the names: g07cac, nag_univar_ttest_2normal or nag_2_sample_t_test.

## 3Description

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 $\overline{x}$ and $\overline{y}$ and the sample variances by ${s}_{x}^{2}$ and ${s}_{y}^{2}$ respectively.
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. 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. (i)${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. (ii)${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. (iii)${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. 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(\overline{x}-\overline{y}\right)\left(\overline{x}-\overline{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.
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

## 5Arguments

1: $\mathbf{tail}$Nag_TailProbability Input
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: $\mathbf{equal}$Nag_PopVar Input
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: $\mathbf{nx}$Integer Input
On entry: the size of the $X$ sample, ${n}_{x}$.
Constraint: ${\mathbf{nx}}\ge 2$.
4: $\mathbf{ny}$Integer Input
On entry: the size of the $Y$ sample, ${n}_{y}$.
Constraint: ${\mathbf{ny}}\ge 2$.
5: $\mathbf{xmean}$double Input
On entry: the mean of the $X$ sample, $\overline{x}$.
6: $\mathbf{ymean}$double Input
On entry: the mean of the $Y$ sample, $\overline{y}$.
7: $\mathbf{xstd}$double Input
On entry: the standard deviation of the $X$ sample, ${s}_{x}$.
Constraint: ${\mathbf{xstd}}>0.0$.
8: $\mathbf{ystd}$double Input
On entry: the standard deviation of the $Y$ sample, ${s}_{y}$.
Constraint: ${\mathbf{ystd}}>0.0$.
9: $\mathbf{clevel}$double 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: $\mathbf{t}$double * Output
On exit: contains the test statistic, ${t}_{\mathrm{obs}}$ or ${t}_{\mathrm{obs}}^{\prime }$.
11: $\mathbf{df}$double * Output
On exit: contains the degrees of freedom for the test statistic.
12: $\mathbf{prob}$double * Output
On exit: contains the significance level, that is the tail probability, $p$, as defined by tail.
13: $\mathbf{dl}$double * Output
On exit: contains the lower confidence limit for ${\mu }_{x}-{\mu }_{y}$.
14: $\mathbf{du}$double * Output
On exit: contains the upper confidence limit for ${\mu }_{x}-{\mu }_{y}$.
15: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

## 6Error 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}}〉$.

## 7Accuracy

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

## 8Parallelism and Performance

g07cac is not threaded in any implementation.

The sample means and standard deviations can be computed using g01atc.

## 10Example

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.

### 10.1Program Text

Program Text (g07cace.c)

### 10.2Program Data

Program Data (g07cace.d)

### 10.3Program Results

Program Results (g07cace.r)