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_anova_dummyvars (g04ea)

## Purpose

nag_anova_dummyvars (g04ea) computes orthogonal polynomial or dummy variables for a factor or classification variable.

## Syntax

[x, rep, ifail] = g04ea(typ, levels, ifact, v, 'n', n)
[x, rep, ifail] = nag_anova_dummyvars(typ, levels, ifact, v, 'n', n)

## Description

In the analysis of an experimental design using a general linear model the factors or classification variables that specify the design have to be coded as dummy variables. nag_anova_dummyvars (g04ea) computes dummy variables that can then be used in the fitting of the general linear model using nag_correg_linregm_fit (g02da).
If the factor of length n$n$ has k$k$ levels then the simplest representation is to define k$k$ dummy variables, Xj${X}_{j}$ such that Xj = 1${X}_{j}=1$ if the factor is at level j$j$ and 0$0$ otherwise for j = 1,2,,k$j=1,2,\dots ,k$. However, there is usually a mean included in the model and the sum of the dummy variables will be aliased with the mean. To avoid the extra redundant parameter k1$k-1$ dummy variables can be defined as the contrasts between one level of the factor, the reference level, and the remaining levels. If the reference level is the first level then the dummy variables can be defined as Xj = 1${X}_{\mathit{j}}=1$ if the factor is at level j$\mathit{j}$ and 0$0$ otherwise, for j = 2,3,,k$\mathit{j}=2,3,\dots ,k$. Alternatively, the last level can be used as the reference level.
A second way of defining the k1$k-1$ dummy variables is to use a Helmert matrix in which levels 2,3,,k$2,3,\dots ,k$ are compared with the average effect of the previous levels. For example if k = 4$k=4$ then the contrasts would be:
 1 − 1 − 1 − 1 2 1 − 1 − 1 3 0 2 − 1 4 0 0 3
$1 -1 -1 -1 2 1 -1 -1 3 0 2 -1 4 0 0 3$
Thus variable j$\mathit{j}$, for j = 1,2,,k1$\mathit{j}=1,2,\dots ,k-1$ is given by
• Xj = 1${X}_{j}=-1$ if factor is at level less than j + 1$j+1$
• Xj = i = 1jri / rj + 1${X}_{j}=\sum _{i=1}^{j}{r}_{i}/{r}_{j+1}$ if factor is at level j + 1$j+1$
• Xj = 0${X}_{j}=0$ if factor is at level greater than j + 1$j+1$
where rj${r}_{j}$ is the number of replicates of level j$j$
If the factor can be considered as a set of values from an underlying continuous variable then the factor can be represented by a set of k1$k-1$ orthogonal polynomials representing the linear, quadratic etc. effects of the underlying variable. The orthogonal polynomial is computed using Forsythe's algorithm (Forsythe (1957), see also Cooper (1968)). The values of the underlying continuous variable represented by the factor levels have to be supplied to the function.
The orthogonal polynomials are standardized so that the sum of squares for each dummy variable is one. For the other methods integer ( ± 1$±1$) representations are retained except that in the Helmert representation the code of level j + 1$j+1$ in dummy variable j$j$ will be a fraction.

## References

Cooper B E (1968) Algorithm AS 10. The use of orthogonal polynomials Appl. Statist. 17 283–287
Forsythe G E (1957) Generation and use of orthogonal polynomials for data fitting with a digital computer J. Soc. Indust. Appl. Math. 5 74–88

## Parameters

### Compulsory Input Parameters

1:     typ – string (length ≥ 1)
The type of dummy variable to be computed.
• If typ = 'P'${\mathbf{typ}}=\text{'P'}$, an orthogonal Polynomial representation is computed.
• If typ = 'H'${\mathbf{typ}}=\text{'H'}$, a Helmert matrix representation is computed.
• If typ = 'F'${\mathbf{typ}}=\text{'F'}$, the contrasts relative to the First level are computed.
• If typ = 'L'${\mathbf{typ}}=\text{'L'}$, the contrasts relative to the Last level are computed.
• If typ = 'C'${\mathbf{typ}}=\text{'C'}$, a Complete set of dummy variables is computed.
Constraint: typ = 'P'${\mathbf{typ}}=\text{'P'}$, 'H'$\text{'H'}$, 'F'$\text{'F'}$, 'L'$\text{'L'}$ or 'C'$\text{'C'}$.
2:     levels – int64int32nag_int scalar
k$k$, the number of levels of the factor.
Constraint: levels2${\mathbf{levels}}\ge 2$.
3:     ifact(n) – int64int32nag_int array
n, the dimension of the array, must satisfy the constraint ${\mathbf{n}}\ge {\mathbf{levels}}$.
The n$n$ values of the factor.
Constraint: 1ifact(i)levels$1\le {\mathbf{ifact}}\left(\mathit{i}\right)\le {\mathbf{levels}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
4:     v( : $:$) – double array
Note: the dimension of the array v must be at least levels${\mathbf{levels}}$ if typ = 'P'${\mathbf{typ}}=\text{'P'}$, and at least 1$1$ otherwise.
If typ = 'P'${\mathbf{typ}}=\text{'P'}$, the k$k$ distinct values of the underlying variable for which the orthogonal polynomial is to be computed.
If typ'P'${\mathbf{typ}}\ne \text{'P'}$, v is not referenced.
Constraint: if typ = 'P'${\mathbf{typ}}=\text{'P'}$, the k$k$ values of v must be distinct.

### Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The dimension of the array ifact.
n$n$, the number of observations for which the dummy variables are to be computed.
Constraint: ${\mathbf{n}}\ge {\mathbf{levels}}$.

ldx

### Output Parameters

1:     x(ldx, : $:$) – double array
The first dimension of the array x will be n${\mathbf{n}}$
The second dimension of the array will be levels1${\mathbf{levels}}-1$ if typ = 'P'${\mathbf{typ}}=\text{'P'}$, 'H'$\text{'H'}$, 'F'$\text{'F'}$ or 'L'$\text{'L'}$ and at least levels${\mathbf{levels}}$ if typ = 'C'${\mathbf{typ}}=\text{'C'}$
ldxn$\mathit{ldx}\ge {\mathbf{n}}$.
The n$n$ by k*${k}^{*}$ matrix of dummy variables, where k* = k1${k}^{*}=k-1$ if typ = 'P'${\mathbf{typ}}=\text{'P'}$, 'H'$\text{'H'}$, 'F'$\text{'F'}$ or 'L'$\text{'L'}$ and k* = k${k}^{*}=k$ if typ = 'C'${\mathbf{typ}}=\text{'C'}$.
2:     rep(levels) – double array
The number of replications for each level of the factor, ri${r}_{\mathit{i}}$, for i = 1,2,,k$\mathit{i}=1,2,\dots ,k$.
3:     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:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

ifail = 1${\mathbf{ifail}}=1$
 On entry, levels < 2${\mathbf{levels}}<2$, or ${\mathbf{n}}<{\mathbf{levels}}$, or ldx < n$\mathit{ldx}<{\mathbf{n}}$, or typ ≠ 'P'${\mathbf{typ}}\ne \text{'P'}$, 'H'$\text{'H'}$, 'F'$\text{'F'}$, 'L'$\text{'L'}$ or 'C'$\text{'C'}$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, a value of ifact is not in the range 1 ≤ ifact(i) ≤ levels$1\le {\mathbf{ifact}}\left(\mathit{i}\right)\le {\mathbf{levels}}$, for i = 1,2, … ,n$\mathit{i}=1,2,\dots ,n$, or typ = 'P'${\mathbf{typ}}=\text{'P'}$ and not all values of v are distinct, or not all levels are represented in ifact.
W ifail = 3${\mathbf{ifail}}=3$
An orthogonal polynomial has all values zero. This will be due to some values of v being very close together. Note this can only occur if typ = 'P'${\mathbf{typ}}=\text{'P'}$.

## Accuracy

The computations are stable.

Other functions for fitting polynomials can be found in Chapter E02.

## Example

```function nag_anova_dummyvars_example
typ = 'C';
levels = int64(4);
ifact = [int64(1);4;2;3;4;2;4;1;3;1;3;2];
v = [0];
[x, rep, ifail] = nag_anova_dummyvars(typ, levels, ifact, v)
```
```

x =

1     0     0     0
0     0     0     1
0     1     0     0
0     0     1     0
0     0     0     1
0     1     0     0
0     0     0     1
1     0     0     0
0     0     1     0
1     0     0     0
0     0     1     0
0     1     0     0

rep =

3
3
3
3

ifail =

0

```
```function g04ea_example
typ = 'C';
levels = int64(4);
ifact = [int64(1);4;2;3;4;2;4;1;3;1;3;2];
v = [0];
[x, rep, ifail] = g04ea(typ, levels, ifact, v)
```
```

x =

1     0     0     0
0     0     0     1
0     1     0     0
0     0     1     0
0     0     0     1
0     1     0     0
0     0     0     1
1     0     0     0
0     0     1     0
1     0     0     0
0     0     1     0
0     1     0     0

rep =

3
3
3
3

ifail =

0

```