NAG Library Function Document
nag_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 <nag.h> 
#include <nagg07.h> 
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;
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:
(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. 
(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. 
(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:
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
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
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:
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:
$\mathbf{tail}$ – Nag_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:
$\mathbf{equal}$ – Nag_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:
$\mathbf{nx}$ – IntegerInput

On entry: the size of the $X$ sample, ${n}_{x}$.
Constraint:
${\mathbf{nx}}\ge 2$.
 4:
$\mathbf{ny}$ – IntegerInput

On entry: the size of the $Y$ sample, ${n}_{y}$.
Constraint:
${\mathbf{ny}}\ge 2$.
 5:
$\mathbf{xmean}$ – doubleInput

On entry: the mean of the $X$ sample, $\stackrel{}{x}$.
 6:
$\mathbf{ymean}$ – doubleInput

On entry: the mean of the $Y$ sample, $\stackrel{}{y}$.
 7:
$\mathbf{xstd}$ – doubleInput

On entry: the standard deviation of the $X$ sample, ${s}_{x}$.
Constraint:
${\mathbf{xstd}}>0.0$.
 8:
$\mathbf{ystd}$ – doubleInput

On entry: the standard deviation of the $Y$ sample, ${s}_{y}$.
Constraint:
${\mathbf{ystd}}>0.0$.
 9:
$\mathbf{clevel}$ – doubleInput

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 3.7 in How to Use the NAG Library and its Documentation).
6
Error Indicators and Warnings
 NE_BAD_PARAM

On entry, argument
equal had an illegal value.
On entry, argument
tail had an illegal value.
 NE_INT_ARG_LT

On entry, ${\mathbf{nx}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{nx}}\ge 2$.
On entry, ${\mathbf{ny}}=\u2329\mathit{\text{value}}\u232a$.
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}}=\u2329\mathit{\text{value}}\u232a$.
 NE_REAL_ARG_LE

On entry,
clevel must not be less than or equal to 0.0:
${\mathbf{clevel}}=\u2329\mathit{\text{value}}\u232a$.
On entry,
xstd must not be less than or equal to 0.0:
${\mathbf{xstd}}=\u2329\mathit{\text{value}}\u232a$.
On entry,
ystd must not be less than or equal to 0.0:
${\mathbf{ystd}}=\u2329\mathit{\text{value}}\u232a$.
7
Accuracy
The computed probability and the confidence limits should be accurate to approximately five significant figures.
8
Parallelism and Performance
nag_2_sample_t_test (g07cac) is not threaded in any implementation.
The sample means and standard deviations can be computed using
nag_summary_stats_onevar (g01atc).
10
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.
10.1
Program Text
Program Text (g07cace.c)
10.2
Program Data
Program Data (g07cace.d)
10.3
Program Results
Program Results (g07cace.r)