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$-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$ 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_univar_ttest_2normal (g07ca) 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.

## 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:     $\mathrm{tail}$ – string (length ≥ 1)
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:     $\mathrm{equal}$ – string (length ≥ 1)
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:     $\mathrm{nx}$int64int32nag_int scalar
${n}_{x}$, the size of the $X$ sample.
Constraint: ${\mathbf{nx}}\ge 2$.
4:     $\mathrm{ny}$int64int32nag_int scalar
${n}_{y}$, the size of the $Y$ sample.
Constraint: ${\mathbf{ny}}\ge 2$.
5:     $\mathrm{xmean}$ – double scalar
$\stackrel{-}{x}$, the mean of the $X$ sample.
6:     $\mathrm{ymean}$ – double scalar
$\stackrel{-}{y}$, the mean of the $Y$ sample.
7:     $\mathrm{xstd}$ – double scalar
${s}_{x}$, the standard deviation of the $X$ sample.
Constraint: ${\mathbf{xstd}}>0.0$.
8:     $\mathrm{ystd}$ – double scalar
${s}_{y}$, the standard deviation of the $Y$ sample.
Constraint: ${\mathbf{ystd}}>0.0$.
9:     $\mathrm{clevel}$ – double scalar
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$.

None.

### Output Parameters

1:     $\mathrm{t}$ – double scalar
Contains the test statistic, ${t}_{\mathrm{obs}}$ or ${t}_{\mathrm{obs}}^{\prime }$.
2:     $\mathrm{df}$ – double scalar
Contains the degrees of freedom for the test statistic.
3:     $\mathrm{prob}$ – double scalar
Contains the significance level, that is the tail probability, $p$, as defined by tail.
4:     $\mathrm{dl}$ – double scalar
Contains the lower confidence limit for ${\mu }_{x}-{\mu }_{y}$.
5:     $\mathrm{du}$ – double scalar
Contains the upper confidence limit for ${\mu }_{x}-{\mu }_{y}$.
6:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{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:
${\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$.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

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

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.
```function g07ca_example

fprintf('g07ca example results\n\n');

nx = int64(4);
ny = int64(8);
xmean  = 25;
ymean  = 21;
xstd   = 0.8185;
ystd   = 4.2083;

% Display data
fprintf('Sample X\n');
fprintf('  Sample size        = %5d\n', nx);
fprintf('  Mean               = %10.4e\n', xmean);
fprintf('  Standard deviation = %10.4e\n\n', xstd);
fprintf('Sample Y\n');
fprintf('  Sample size        = %5d\n', ny);
fprintf('  Mean               = %10.4e\n', ymean);
fprintf('  Standard deviation = %10.4e\n', ystd);

% Loop over different assumptions and requests
clevel = [0.95,    0.95];
tail   = {'Two';   'Two'};
equal  = {'Equal'; 'Unequal'};

for j = 1:numel(clevel)
% Calculate statistic
[t, df, prob, dl, du, ifail] = ...
g07ca( ...
tail{j}, equal{j}, nx, ny, xmean, ymean, xstd, ystd, clevel(j));

if equal{1}=='Equal'
fprintf('\nAssuming population variances are equal.\n\n');
else
end
fprintf('t test statistic         = %11.4f\n', t);
fprintf('Degrees of freedom       = %8.1f\n', df);
fprintf('Significance level       = %11.4f\n\n', prob);
fprintf('Difference in means\n');
fprintf('  Value                  = %10.4f\n', xmean - ymean);
fprintf('  Lower confidence limit = %10.4f\n', dl);
fprintf('  Upper confidence limit = %10.4f\n', du);
fprintf('  Confidence level       = %10.4f\n', clevel(j));
end

```
```g07ca example results

Sample X
Sample size        =     4
Mean               = 2.5000e+01
Standard deviation = 8.1850e-01

Sample Y
Sample size        =     8
Mean               = 2.1000e+01
Standard deviation = 4.2083e+00

Assuming population variances are equal.

t test statistic         =      1.8403
Degrees of freedom       =     10.0
Significance level       =      0.0955

Difference in means
Value                  =     4.0000
Lower confidence limit =    -0.8429
Upper confidence limit =     8.8429
Confidence level       =     0.9500

Assuming population variances are equal.

t test statistic         =      2.5922
Degrees of freedom       =      8.0
Significance level       =      0.0320

Difference in means
Value                  =     4.0000
Lower confidence limit =     0.4410
Upper confidence limit =     7.5590
Confidence level       =     0.9500
```