# NAG Library Function Document

## nag_blgm_lm_design_matrix (g22ycc)

Note: please be advised that this function is classed as ‘experimental’ and its interface may be developed further in the future. Please see Section 3.1.1 in How to Use the NAG Library and its Documentation for further information.

## 1Purpose

nag_blgm_lm_design_matrix (g22ycc) generates a design matrix from a data matrix and model description.

## 2Specification

 #include #include
 void nag_blgm_lm_design_matrix (void *hform, void *hddesc, const double dat[], Integer pddat, Integer sddat, void **hxdesc, double x[], Integer pdx, Integer sdx, Integer *mx, NagError *fail)

## 3Description

nag_blgm_lm_design_matrix (g22ycc) generates a design matrix from a data matrix and a model description. Design matrices encapsulate the observed values of the independent variables and the required model in a form that can be used by many of the model fitting functions available in the NAG Library, for example those in Chapter g02.

### 3.1Notation

Let $D$ denote a data matrix with $n$ observations on ${m}_{d}$ independent variables, denoted by ${V}_{j}$, for $\mathit{j}=1,2,\dots ,{m}_{d}$. If ${V}_{j}$ is a categorical variable, let ${L}_{j}$ denote the number of levels associated with it. If ${V}_{j}$ is a binary, ordinal or continuous variable, let ${L}_{j}=1$.
Let ${V}_{ji}$ denote the $i$th value of ${V}_{j}$.
Let $\mathcal{M}$ denote a model made up of one or more terms, denoted by ${T}_{i}$. Each term consists of either a main effect or an interaction and hence can be described using one or more variable names ${V}_{j}$ and the interaction operator ‘$.$’. The operator ‘$+$’ is used to denote the addition of a term to the model. Therefore, $\mathcal{M}={T}_{1}+{T}_{2}+{T}_{3}={V}_{1}+{V}_{2}+{V}_{1}.{V}_{2}$ denotes a model with three terms, the first two terms being the main effects for variables ${V}_{1}$ and ${V}_{2}$ and the last term the interaction between them. For simplicity we reorder the terms of the model by the number of variables in them, so main effects come first, then two-way interactions, then three-way interactions etc. By default it is assumed that the model $\mathcal{M}$ contains a mean effect (or intercept term), if the mean effect is excluded, this will be denoted by ‘$-1$’, so $\mathcal{M}={T}_{1}$ is a model with one term and a mean effect and $\mathcal{M}={T}_{1}-1$ is the same model with the mean effect dropped.
nag_blgm_lm_design_matrix (g22ycc) generates an $n$ by ${m}_{x}$ design matrix, $X$, from $D$ and $\mathcal{M}$.

### 3.2Dummy Variables

When constructing a design matrix, we cannot work directly with categorical variables. Categorical variables must first be recoded into dummy variables. A categorical variable ${V}_{j}$ requires ${L}_{j}$ dummy variables. Let ${\mathcal{D}}^{j}$ denote an $n×{L}_{j}$ matrix of dummy variables for ${V}_{j}$ defined as
 $Dlij = 1 ; if ​Vji=l, 0 ; otherwise$
where ${\mathcal{D}}_{l}^{j}$ is the $l$th column of ${\mathcal{D}}^{j}$ and ${\mathcal{D}}_{li}^{j}$ is the $i$th element of ${\mathcal{D}}_{l}^{j}$.
For a binary, ordinal or continuous variable, ${\mathcal{D}}_{1i}^{j}={V}_{ji}$.

### 3.3Full Design Matrix

Given a model, $\mathcal{M}$, and the matrices of dummy variables constructing the full design matrix ${X}_{F}$ is trivial. Each term is processed in order and
1. If term $i$ is a main effect, that is ${T}_{i}={V}_{j}$ for some $j$, ${\mathcal{D}}^{j}$ is copied into ${X}_{F}$.
2. If term $i$ is a two-way interaction, that is ${T}_{i}={V}_{j}.{V}_{k}$, for some $j\ne k$, then
 (i) Loop over ${l}_{j}=1,2,\dots {L}_{j}$. (ii) Loop over ${l}_{k}=1,2,\dots {L}_{k}$. (iii) Add a column to ${X}_{F}$ corresponding to the element-wise product of ${\mathcal{D}}_{{l}_{j}}^{j}$ and ${\mathcal{D}}_{{l}_{k}}^{k}$.
3. Higher interaction terms are handled in a similar manner as the two-way interactions by adding columns constructed from multiplying all combinations of the columns of the corresponding $\mathcal{D}$s that correspond to the variables involved. In all cases, the variables towards the right hand side of a term are iterated over the quickest.

### 3.4Contrasts

Using the full design matrix ${X}_{F}$ in an analysis can result in an overparameterized model. This is due to ${X}_{F}$ often not being of full rank as the sum of all the dummy variables for a particular variable is a vector of ones. This source of overparameterization can be alleviated by using a design matrix $X$ where (some) dummy variables are replaced by contrasts. For a categorical variable ${V}_{j}$ the contrasts are a set of ${L}_{j}-1$ functionally independent linear combinations of the dummy variables.
Whilst the choice of contrasts used in term ${T}_{i}$ will affect the individual model coefficients (parameters), it has no effect on the overall contribution of ${T}_{i}$.
For a given variable ${V}_{j}$, the contrasts can be represented by an ${L}_{j}$ by ${L}_{j}-1$ matrix, ${C}_{j}$. The rows of ${C}_{j}$ correspond to a particular value of ${V}_{j}$ and the columns correspond to the values to use in the design matrix.
Six types of contrast are available in nag_blgm_lm_design_matrix (g22ycc); two types of treatment contrasts, two types of sum contrasts, Helmert contrasts and polynomial contrasts. Unless specified otherwise, the contrasts used by nag_blgm_lm_design_matrix (g22ycc) are treatment contrasts relative to the first level. See the description of the optional parameter ${\mathbf{Contrast}}$ in nag_blgm_lm_formula (g22yac) for ways of changing the contrasts used.

#### 3.4.1Treatment Contrasts

Treatment contrasts are taken relative to either the first or last level of the variable. For example, if ${L}_{j}=4$,
 $Cj= 0 0 0 1 0 0 0 1 0 0 0 1$
would be the contrast matrix for ${V}_{j}$ using treatment contrasts relative to the first level. The contrast matrix obtained when using treatment contrasts relative to the last level is similar, but the row of zeros appears at the bottom and all other rows are shifted up one.
Strictly speaking, the term contrast implies that each row in the contrast matrix sums to zero. That is not the case for treatment contrasts, however they are included as this coding is commonly used in practice.

#### 3.4.2Sum Contrasts

Sum contrasts are similar to treatment contrasts and again can be taken relative to the first or last level of the variable. Unlike treatment contrasts, sum contrasts effectively constrain the coefficients related to the variable to sum to zero. For example, if ${L}_{j}=4$,
 $Cj= 1 0 0 0 1 0 0 0 1 -1 -1 -1$
would be the contrast matrix for ${V}_{j}$ using treatment contrasts relative to the last level. The contrast matrix obtained when using treatment contrasts relative to the first level is similar, but the row of $-1$s appears at the top and all other rows are shifted down one.

#### 3.4.3Helmert Contrasts

With Helmert contrasts level $l$ of the variable is compared with the average effect of all previous levels. For example, if ${L}_{j}=4$,
 $Cj= -1 -1 -1 1 -1 -1 0 2 -1 0 0 3$
would be the contrast matrix for ${V}_{j}$ using Helmert contrasts.

#### 3.4.4Polynomial Contrasts

With polynomial contrasts the entries in the columns of ${C}_{j}$ correspond in linear, quadratic, cubic, quartic, etc. terms to a hypothetical underlying numeric variable that takes equally spaced values at each level. For example, if ${L}_{j}=4$,
 $Cj= -0.67 0.50 -0.22 -0.22 -0.50 0.67 0.22 -0.50 -0.67 0.67 0.50 0.22$
would be the contrast matrix for ${V}_{j}$ using polynomial contrasts.

#### 3.4.5When Contrasts Can Be Used

Depending on the specifics of the model, $\mathcal{M}$, it may not be possible to always replace the ${L}_{j}$ dummy variables with ${L}_{j}-1$ contrasts for all variables in all terms and retain the same model. A simple example of this is a data matrix, $D$, with four observations and two variables which have two and three levels respectively. This data matrix might look something like:
 $D= 1 1 2 3 1 2 2 2$
For the sake of argument, assume that our model contains the main effect for each variable, but does not contain a mean effect (or intercept term). So using the notation established earlier, $\mathcal{M}={V}_{1}+{V}_{2}-1$. The full design matrix, ${X}_{F}$, for this data matrix and model would be
 $XF = 1 0 1 0 0 0 1 0 0 1 1 0 0 1 0 0 1 0 1 0$
However, ${X}_{F}$ is not of full rank (and hence $\mathcal{M}$ is overparameterized) because the sum of the first two columns is a vector of ones as is the sum of the last three columns.
In order to alleviate this we might try constructing ${X}_{C}$ where the dummy variables have been replaced by contrasts. Assuming treatment contrasts, relative to the first level, we would have
 $XC = 0 0 0 1 0 1 0 1 0 1 1 0$
However, using ${X}_{C}$ makes an implicit assumption that the expected value of the dependent variable (the quantity being modelled) is zero when ${V}_{1}=1$ and ${V}_{2}=1$. This assumption was not made when we used ${X}_{F}$ and hence the two design matrices are not equivalent. One solution would be to use dummy variables for ${V}_{1}$ and contrasts for ${V}_{2}$, which would result in a design matrix, $X$ of
 $X = 1 0 0 0 0 1 0 1 1 0 1 0 0 1 1 0$
Using $X$ would give an equivalent model to using ${X}_{F}$.
The algorithm used by nag_blgm_lm_design_matrix (g22ycc) to decide which variables, in which terms, can be coded as contrasts and which need to be coded as dummy variables is described below.
Suppose ${V}_{j}$ is any variable that appears in term ${T}_{i}$, let ${T}_{i\left(j\right)}$ denote the term obtained by dropping ${V}_{j}$ from ${T}_{i}$. For example, if ${T}_{3}={V}_{1}.{V}_{2}.{V}_{3}$, ${T}_{3\left(2\right)}={V}_{1}.{V}_{3}$. In this context, the empty term is taken to be the mean effect (or intercept term). We say that ${T}_{i\left(j\right)}$ appears in $\mathcal{M}$ if there exists a term ${T}_{k}$, $k, that contains all of the variables appearing in ${T}_{i\left(j\right)}$. In most cases ${T}_{k}={T}_{i\left(j\right)}$, but this is not required. Note, as stated earlier, the terms in $\mathcal{M}$ are ordered by the number of variables in them.
A variable, ${V}_{j}$ in term ${T}_{i}$ is coded by contrasts if ${T}_{i\left(j\right)}$ appears in $\mathcal{M}$ and by dummy variables otherwise. It is therefore possible for variable ${V}_{j}$ to be coded by contrasts in some terms and dummy variables in others within the same $X$.
The above rule assumes the presence of a mean effect. If no such effect is present in the model, the main effect of the first categorical variable is coded by dummy variables to compensate. If no main effects appear in the model, the warning ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NW_POTENTIAL_PROBLEM is returned.
A longer description and informal proof that the resulting $X$ is a suitable design matrix for the model of interest can be found in chapter two of Chambers and Hastie (1992).

### 3.5Mean Effect

The mean effect (or intercept term) is included in a design matrix by adding a column of ones as the first column of $X$. However, many model fitting functions in the NAG Library handle the mean effect as a special case and do not require it to be explicitly added to the design matrix. Therefore, by default, nag_blgm_lm_design_matrix (g22ycc) does not explicitly add the mean effect to the design matrix. This behaviour can be changed via the optional parameter ${\mathbf{Explicit Mean}}$ in nag_blgm_lm_formula (g22yac).

## 4References

Chambers J M and Hastie T J (1992) Statistical Models in S Wadsworth and Brooks/Cole Computer Science Series

## 5Arguments

1:    $\mathbf{hform}$void *Input
On entry: a G22 handle to the internal data structure containing a description of the model $\mathcal{M}$ as returned in hform by nag_blgm_lm_formula (g22yac).
2:    $\mathbf{hddesc}$void *Input
On entry: a G22 handle to the internal data structure containing a description of the data matrix, $D$ as returned in hddesc by nag_blgm_lm_describe_data (g22ybc).
3:    $\mathbf{dat}\left[{\mathbf{pddat}}×{\mathbf{sddat}}\right]$const doubleInput
Note: the $\left(i,j\right)$th element of the matrix is stored in ${\mathbf{dat}}\left[\left(j-1\right)×{\mathbf{pddat}}+i-1\right]$.
On entry: the data matrix, $D$. By default ${D}_{ij}$, the $\mathit{i}$th value for the $\mathit{j}$th variable, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,{m}_{d}$, should be supplied in ${\mathbf{dat}}\left[\left(j-1\right)×{\mathbf{pddat}}+i-1\right]$.
If the optional parameter ${\mathbf{Storage Order}}$, described in nag_blgm_lm_describe_data (g22ybc), is set to $\mathrm{VAROBS}$, ${D}_{ij}$ should be supplied in ${\mathbf{dat}}\left[\left(i-1\right)×{\mathbf{pddat}}+j-1\right]$.
4:    $\mathbf{pddat}$IntegerInput
On entry: the stride separating matrix row elements in the array dat.
Constraints:
• if the optional parameter ${\mathbf{Storage Order}}$, described in nag_blgm_lm_describe_data (g22ybc), is set to $\mathrm{VAROBS}$, ${\mathbf{pddat}}\ge {m}_{d}$;
• otherwise ${\mathbf{pddat}}\ge n$.
5:    $\mathbf{sddat}$IntegerInput
On entry: the secondary dimension of dat.
Constraints:
• if the optional parameter ${\mathbf{Storage Order}}$, described in nag_blgm_lm_describe_data (g22ybc), is set to $\mathrm{VAROBS}$, ${\mathbf{sddat}}\ge n$;
• otherwise ${\mathbf{sddat}}\ge {m}_{d}$.
6:    $\mathbf{hxdesc}$void **Input/Output
On entry: must be set to NULL.
As an alternative an existing G22 handle may be supplied in which case this function will destroy the supplied G22 handle as if nag_blgm_handle_free (g22zac) had been called.
On exit: holds a G22 handle to the internal data structure containing a description of the design matrix, $X$. You must not change the G22 handle other than through the functions in Chapter g22.
7:    $\mathbf{x}\left[{\mathbf{pdx}}×{\mathbf{sdx}}\right]$doubleOutput
Note: the $\left(i,j\right)$th element of the matrix $X$ is stored in ${\mathbf{x}}\left[\left(j-1\right)×{\mathbf{pdx}}+i-1\right]$.
On exit: the design matrix, $X$. By default ${X}_{ij}$, the $\mathit{i}$th value for the $\mathit{j}$th column, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,{m}_{x}$, is returned in ${\mathbf{x}}\left[\left(j-1\right)×{\mathbf{pdx}}+i-1\right]$
If the optional parameter ${\mathbf{Storage Order}}$, described in nag_blgm_lm_formula (g22yac), is set to $\mathrm{VAROBS}$, ${X}_{ij}$ is returned in ${\mathbf{x}}\left[\left(i-1\right)×{\mathbf{pdx}}+j-1\right]$.
If pdx or sdx are too small to hold x, the number of columns required to hold the design matrix is returned in mx.
Under some conditions it is possible to use the data matrix in place of the design matrix. Specifically, if $D$ has no categorical variables, $\mathcal{M}$ has only main effects and either has no mean effect or the mean effect does not need to be explicitly added to the design matrix. If pdx or sdx are too small under such circumstances, ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NW_ALTERNATIVE is returned and hxdesc is set up in such a way as to allow dat to be used as the design matrix.
If pdx and sdx are both zero, x is not referenced and may be NULL.
8:    $\mathbf{pdx}$IntegerInput
On entry: the stride separating matrix row elements in the array x.
Constraints:
• if the optional parameter ${\mathbf{Storage Order}}$, described in nag_blgm_lm_formula (g22yac), is set to $\mathrm{VAROBS}$, ${\mathbf{pdx}}\ge {m}_{x}$;
• otherwise ${\mathbf{pdx}}\ge n$.
9:    $\mathbf{sdx}$IntegerInput
On entry: the secondary dimension of x.
Constraints:
• if the optional parameter ${\mathbf{Storage Order}}$, described in nag_blgm_lm_formula (g22yac), is set to $\mathrm{VAROBS}$, ${\mathbf{sdx}}\ge n$;
• otherwise ${\mathbf{sdx}}\ge {m}_{x}$.
10:  $\mathbf{mx}$Integer *Output
On exit: the minimum number of columns required to hold the design matrix.
In most cases ${\mathbf{mx}}={m}_{x}$. The one exception is when ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NW_ALTERNATIVE, that is the size of x was too small but the data matrix given in dat can be used as the design matrix. In this case mx holds the number of columns that would be required if only the relevant parts of dat were copied into a new array.
11:  $\mathbf{fail}$NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 2.3.1.2 in How to Use the NAG Library and its Documentation for further information.
NE_ARRAY_SIZE
On entry, ${m}_{d}=〈\mathit{\text{value}}〉$ and ${\mathbf{pddat}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pddat}}\ge {m}_{d}$.
On entry, ${m}_{d}=〈\mathit{\text{value}}〉$ and ${\mathbf{sddat}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{sddat}}\ge {m}_{d}$.
On entry, $n=〈\mathit{\text{value}}〉$ and ${\mathbf{pddat}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pddat}}\ge n$.
On entry, $n=〈\mathit{\text{value}}〉$ and ${\mathbf{sddat}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{sddat}}\ge n$.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_FIELD_UNKNOWN
A variable name used when creating hform is not present in hddesc.
Variable name: $〈\mathit{\text{value}}〉$.
NE_HANDLE
hddesc has not been initialized or is corrupt.
hddesc is not a G22 handle as generated by nag_blgm_lm_describe_data (g22ybc).
hform has not been initialized or is corrupt.
hform is not a G22 handle as generated by nag_blgm_lm_formula (g22yac).
On entry, hxdesc is not NULL or a recognised G22 handle.
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.
See Section 2.7.6 in How to Use the NAG Library and its Documentation for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 2.7.5 in How to Use the NAG Library and its Documentation for further information.
NE_REAL_ARRAY
On entry, column $j$ of the data matrix, $D$, is not consistent with information supplied in hddesc, $j=〈\mathit{\text{value}}〉$.
NW_ALTERNATIVE
On entry, the size of x is too small to hold the design matrix. dat can be used instead.
NW_ARRAY_SIZE
On entry, ${m}_{x}=〈\mathit{\text{value}}〉$ and ${\mathbf{pdx}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdx}}\ge {m}_{x}$.
On entry, ${m}_{x}=〈\mathit{\text{value}}〉$ and ${\mathbf{sdx}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{sdx}}\ge {m}_{x}$.
On entry, $n=〈\mathit{\text{value}}〉$ and ${\mathbf{pdx}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdx}}\ge n$.
On entry, $n=〈\mathit{\text{value}}〉$ and ${\mathbf{sdx}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{sdx}}\ge n$.
NW_POTENTIAL_PROBLEM
The model contains categorical variables, but no intercept or main effects terms have been requested.
Please check the design matrix returned matches the model you require.

Not applicable.

## 8Parallelism and Performance

nag_blgm_lm_design_matrix (g22ycc) is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
nag_blgm_lm_design_matrix (g22ycc) makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the x06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this function. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

nag_blgm_lm_submodel (g22ydc) can be used to obtain labels for the columns of the design matrix $X$.
Many of the analysis functions that require a design matrix to be supplied allow submodels to be defined through the use of a vector of ones or zeros indicating whether a column of $X$ should be included or excluded from the analyses (see for example sx in nag_regsn_mult_linear (g02dac) or nag_glm_normal (g02gac)). This allows nested models to be fit without having to reconstruct the design matrix for each analysis. nag_blgm_lm_submodel (g22ydc) offers a mechanism for constructing these vectors using submodels specified using nag_blgm_lm_formula (g22yac).

## 10Example

This example creates and outputs two design matrices for a simple linear regression model. The first design matrix uses sum contrasts for all variables and the second uses a combination of polynomial and Helmert contrasts. Column labels are generated using nag_blgm_lm_submodel (g22ydc).
See also the examples for nag_blgm_lm_formula (g22yac), nag_blgm_lm_describe_data (g22ybc) and nag_blgm_lm_submodel (g22ydc).

### 10.1Program Text

Program Text (g22ycce.c)

### 10.2Program Data

Program Data (g22ycce.d)

### 10.3Program Results

Program Results (g22ycce.r)

## 11Optional Parameters

As well as the optional parameters common to all G22 handles described in nag_blgm_optset (g22zmc) and nag_blgm_optget (g22znc), a number of additional optional parameters can be specified for a G22 handle holding the description of a design matrix as returned by nag_blgm_lm_design_matrix (g22ycc) in hxdesc.
The value of an optional parameter can be queried using nag_blgm_optget (g22znc).
The remainder of this section can be skipped if you wish to use the default values for all optional parameters.
The following is a list of the optional parameters available. A full description of each optional parameter is provided in Section 11.1.

### 11.1Description of the Optional Parameters

For each option, we give a summary line, a description of the optional parameter and details of constraints.
The summary line contains:
• a parameter value, where the letters $a$, $i$ and $r$ denote options that take character, integer and real values respectively;
Keywords and character values are case and white space insensitive.
 Formula $a$
This optional parameter returns a verbose formula string describing the model, $\mathcal{M}$, used to create the design matrix. This formula will only contain variable names, the operators ‘$+$’ and ‘$.$’ and any contrast identifiers present.
 Min Number of Columns $i$
This optional parameter returns the minimum number of columns required to hold the design matrix, $X$. In most cases ${\mathbf{Min Number of Columns}}={\mathbf{Number of Columns}}$. The one exception is when ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NW_ALTERNATIVE, that is the size of x was too small but the data matrix given in dat can be used as the design matrix. In this case, ${\mathbf{Number of Columns}}={m}_{x}={m}_{d}$ and ${\mathbf{Min Number of Columns}}$ holds the number of columns that would be required if only the relevant parts of dat were copied into a new array.
 Number of Columns $i$
This optional parameter returns ${m}_{x}$, the number of columns in the design matrix.
 Number of Observations $i$
This optional parameter returns $n$, the number of observations in the design matrix.
 Storage Order $a$
This optional parameter returns how the design matrix, $X$, is stored in x.
If ${\mathbf{Storage Order}}=\mathrm{OBSVAR}$, ${X}_{ij}$, the value for the $j$th variable of the $i$th observation of the design matrix is stored in ${\mathbf{x}}\left[\left(j-1\right)×{\mathbf{pdx}}+i-1\right]$.
If ${\mathbf{Storage Order}}=\mathrm{VAROBS}$, ${X}_{ij}$, the value for the $j$th variable of the $i$th observation of the design matrix is stored in ${\mathbf{x}}\left[\left(i-1\right)×{\mathbf{pdx}}+j-1\right]$.
It should be noted that ${\mathbf{Storage Order}}$ is not writeable. If you wish to change the storage order of the design matrix you need to change ${\mathbf{Storage Order}}$ in hform as described in Section 11 in nag_blgm_lm_formula (g22yac) prior to calling nag_blgm_lm_design_matrix (g22ycc).
© The Numerical Algorithms Group Ltd, Oxford, UK. 2017