Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_univar_ttest_2normal (g07ca)

## Purpose

nag_univar_ttest_2normal (g07ca) computes a t$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.

## Syntax

[t, df, prob, dl, du, ifail] = g07ca(tail, equal, nx, ny, xmean, ymean, xstd, ystd, clevel)
[t, df, prob, dl, du, ifail] = nag_univar_ttest_2normal(tail, equal, nx, ny, xmean, ymean, xstd, ystd, clevel)

## Description

Consider two independent samples, denoted by X$X$ and Y$Y$, of size nx${n}_{x}$ and ny${n}_{y}$ drawn from two Normal populations with means μx${\mu }_{x}$ and μy${\mu }_{y}$, and variances σx2${\sigma }_{x}^{2}$ and σy2${\sigma }_{y}^{2}$ respectively. Denote the sample means by x$\stackrel{-}{x}$ and y$\stackrel{-}{y}$ and the sample variances by sx2${s}_{x}^{2}$ and sy2${s}_{y}^{2}$ respectively.
nag_univar_ttest_2normal (g07ca) calculates a test statistic and its significance level to test the null hypothesis H0 : μx = μy${H}_{0}:{\mu }_{x}={\mu }_{y}$, together with upper and lower confidence limits for μxμy${\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 σx2 = σy2${\sigma }_{x}^{2}={\sigma }_{y}^{2}$.
The test used is the two sample t$t$-test. The test statistic t$t$ is defined by;
 tobs = (x − y)/(s×sqrt((1 / nx) + (1 / ny))) $tobs=x--y- s⁢(1/nx)+(1/ny)$
where
 s2 = ( (nx − 1) sx2 + (ny − 1) sy2 )/( nx + ny − 2 ) $s2 = (nx-1) sx2 + (ny-1) sy2 nx + ny - 2$
is the pooled variance of the two samples.
Under the null hypothesis H0${H}_{0}$ this test statistic has a t$t$-distribution with (nx + ny2)$\left({n}_{x}+{n}_{y}-2\right)$ degrees of freedom.
The test of H0${H}_{0}$ is carried out against one of three possible alternatives;
• H1 : μxμy${H}_{1}:{\mu }_{x}\ne {\mu }_{y}$; the significance level, p = P(t|tobs|)$p=P\left(t\ge |{t}_{\mathrm{obs}}|\right)$, i.e., a two tailed probability.
• H1 : μx > μy${H}_{1}:{\mu }_{x}>{\mu }_{y}$; the significance level, p = P(ttobs)$p=P\left(t\ge {t}_{\mathrm{obs}}\right)$, i.e., an upper tail probability.
• H1 : μx < μy${H}_{1}:{\mu }_{x}<{\mu }_{y}$; the significance level, p = P(ttobs)$p=P\left(t\le {t}_{\mathrm{obs}}\right)$, i.e., a lower tail probability.
Upper and lower 100(1α)%$100\left(1-\alpha \right)%$ confidence limits for μxμy${\mu }_{x}-{\mu }_{y}$ are calculated as:
 (x − y) ± t1 − α / 2s×sqrt((1 / nx) + (1 / ny)). $(x--y-)±t1-α/2s⁢(1/nx)+(1/ny).$
where t1α / 2${t}_{1-\alpha /2}$ is the 100(1α / 2)$100\left(1-\alpha /2\right)$ percentage point of the t$t$-distribution with (nx + ny2${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$t$-statistic no longer has a t$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${t}^{\prime }$ is used where
 tobs ′ = (x − y)/(se(x − y)) $tobs′=x--y- se(x--y-)$
where se(xy) = sqrt((sx2)/(nx) + (sy2)/(ny))$\mathrm{se}\left(\stackrel{-}{x}-\stackrel{-}{y}\right)=\sqrt{\frac{{s}_{x}^{2}}{{n}_{x}}+\frac{{s}_{y}^{2}}{{n}_{y}}}$.
A t$t$-distribution with f$f$ degrees of freedom is used to approximate the distribution of t${t}^{\prime }$ where
 f = ( se(x − y)4 )/( ((sx2 / nx)2)/((nx − 1)) + ((sy2 / ny)2)/((ny − 1)) ) . $f = se⁡ ( x- - y- ) 4 ( sx2 / nx ) 2 (nx-1) + ( sy2 / ny ) 2 (ny-1) .$
The test of H0${H}_{0}$ is carried out against one of the three alternative hypotheses described above, replacing t$t$ by t${t}^{\prime }$ and tobs${t}_{\mathrm{obs}}$ by tobs${t}_{\mathrm{obs}}^{\prime }$.
Upper and lower 100(1α)%$100\left(1-\alpha \right)%$ confidence limits for μxμy${\mu }_{x}-{\mu }_{y}$ are calculated as:
 (x − y) ± t1 − α / 2se(x − y). $(x--y-)±t1-α/2se(x-y-).$
where t1α / 2${t}_{1-\alpha /2}$ is the 100(1α / 2)$100\left(1-\alpha /2\right)$ percentage point of the t$t$-distribution with f$f$ degrees of freedom.

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

## Parameters

### Compulsory Input Parameters

1:     tail – string (length ≥ 1)
Indicates which tail probability is to be calculated, and thus which alternative hypothesis is to be used.
tail = 'T'${\mathbf{tail}}=\text{'T'}$
The two tail probability, i.e., H1 : μxμy${H}_{1}:{\mu }_{x}\ne {\mu }_{y}$.
tail = 'U'${\mathbf{tail}}=\text{'U'}$
The upper tail probability, i.e., H1 : μx > μy${H}_{1}:{\mu }_{x}>{\mu }_{y}$.
tail = 'L'${\mathbf{tail}}=\text{'L'}$
The lower tail probability, i.e., H1 : μx < μy${H}_{1}:{\mu }_{x}<{\mu }_{y}$.
Constraint: tail = 'T'${\mathbf{tail}}=\text{'T'}$, 'U'$\text{'U'}$ or 'L'$\text{'L'}$.
2:     equal – string (length ≥ 1)
Indicates whether the population variances are assumed to be equal or not.
equal = 'E'${\mathbf{equal}}=\text{'E'}$
The population variances are assumed to be equal, that is σx2 = σy2${\sigma }_{x}^{2}={\sigma }_{y}^{2}$.
equal = 'U'${\mathbf{equal}}=\text{'U'}$
The population variances are not assumed to be equal.
Constraint: equal = 'E'${\mathbf{equal}}=\text{'E'}$ or 'U'$\text{'U'}$.
3:     nx – int64int32nag_int scalar
nx${n}_{x}$, the size of the X$X$ sample.
Constraint: nx2${\mathbf{nx}}\ge 2$.
4:     ny – int64int32nag_int scalar
ny${n}_{y}$, the size of the Y$Y$ sample.
Constraint: ny2${\mathbf{ny}}\ge 2$.
5:     xmean – double scalar
x$\stackrel{-}{x}$, the mean of the X$X$ sample.
6:     ymean – double scalar
y$\stackrel{-}{y}$, the mean of the Y$Y$ sample.
7:     xstd – double scalar
sx${s}_{x}$, the standard deviation of the X$X$ sample.
Constraint: xstd > 0.0${\mathbf{xstd}}>0.0$.
8:     ystd – double scalar
sy${s}_{y}$, the standard deviation of the Y$Y$ sample.
Constraint: ystd > 0.0${\mathbf{ystd}}>0.0$.
9:     clevel – double scalar
The confidence level, 1α$1-\alpha$, for the specified tail. For example clevel = 0.95${\mathbf{clevel}}=0.95$ will give a 95%$95%$ confidence interval.
Constraint: 0.0 < clevel < 1.0$0.0<{\mathbf{clevel}}<1.0$.

None.

None.

### Output Parameters

1:     t – double scalar
Contains the test statistic, tobs${t}_{\mathrm{obs}}$ or tobs${t}_{\mathrm{obs}}^{\prime }$.
2:     df – double scalar
Contains the degrees of freedom for the test statistic.
3:     prob – double scalar
Contains the significance level, that is the tail probability, p$p$, as defined by tail.
4:     dl – double scalar
Contains the lower confidence limit for μxμy${\mu }_{x}-{\mu }_{y}$.
5:     du – double scalar
Contains the upper confidence limit for μxμy${\mu }_{x}-{\mu }_{y}$.
6:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:
ifail = 1${\mathbf{ifail}}=1$
 On entry, tail ≠ 'T'${\mathbf{tail}}\ne \text{'T'}$, 'U'$\text{'U'}$ or 'L'$\text{'L'}$, or equal ≠ 'E'${\mathbf{equal}}\ne \text{'E'}$ or 'U'$\text{'U'}$, or nx < 2${\mathbf{nx}}<2$, or ny < 2${\mathbf{ny}}<2$, or xstd ≤ 0.0${\mathbf{xstd}}\le 0.0$, or ystd ≤ 0.0${\mathbf{ystd}}\le 0.0$, or clevel ≤ 0.0${\mathbf{clevel}}\le 0.0$, or clevel ≥ 1.0${\mathbf{clevel}}\ge 1.0$.

## 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_stat_summary_onevar (g01at).

## Example

```function nag_univar_ttest_2normal_example
tail = 'Two';
equal = 'Equal';
nx = int64(4);
ny = int64(8);
xmean = 25;
ymean = 21;
xstd = 0.8185;
ystd = 4.2083;
clevel = 0.95;
[t, df, prob, dl, du, ifail] = ...
nag_univar_ttest_2normal(tail, equal, nx, ny, xmean, ymean, xstd, ystd, clevel)
```
```

t =

1.8403

df =

10

prob =

0.0955

dl =

-0.8429

du =

8.8429

ifail =

0

```
```function g07ca_example
tail = 'Two';
equal = 'Equal';
nx = int64(4);
ny = int64(8);
xmean = 25;
ymean = 21;
xstd = 0.8185;
ystd = 4.2083;
clevel = 0.95;
[t, df, prob, dl, du, ifail] = ...
g07ca(tail, equal, nx, ny, xmean, ymean, xstd, ystd, clevel)
```
```

t =

1.8403

df =

10

prob =

0.0955

dl =

-0.8429

du =

8.8429

ifail =

0

```