G03 Chapter Contents
G03 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG03CAF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G03CAF computes the maximum likelihood estimates of the parameters of a factor analysis model. Either the data matrix or a correlation/covariance matrix may be input. Factor loadings, communalities and residual correlations are returned.

## 2  Specification

 SUBROUTINE G03CAF ( MATRIX, WEIGHT, N, M, X, LDX, NVAR, ISX, NFAC, WT, E, STAT, COM, PSI, RES, FL, LDFL, IOP, IWK, WK, LWK, IFAIL)
 INTEGER N, M, LDX, NVAR, ISX(M), NFAC, LDFL, IOP(5), IWK(4*NVAR+2), LWK, IFAIL REAL (KIND=nag_wp) X(LDX,M), WT(*), E(NVAR), STAT(4), COM(NVAR), PSI(NVAR), RES(NVAR*(NVAR-1)/2), FL(LDFL,NFAC), WK(LWK) CHARACTER(1) MATRIX, WEIGHT

## 3  Description

Let $p$ variables, ${x}_{1},{x}_{2},\dots ,{x}_{p}$, with variance-covariance matrix $\Sigma$ be observed. The aim of factor analysis is to account for the covariances in these $p$ variables in terms of a smaller number, $k$, of hypothetical variables, or factors, ${f}_{1},{f}_{2},\dots ,{f}_{k}$. These are assumed to be independent and to have unit variance. The relationship between the observed variables and the factors is given by the model:
 $xi=∑j=1kλijfj+ei, i=1,2,…,p$
where ${\lambda }_{\mathit{i}\mathit{j}}$, for $\mathit{i}=1,2,\dots ,p$ and $\mathit{j}=1,2,\dots ,k$, are the factor loadings and ${e}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,p$, are independent random variables with variances ${\psi }_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,p$. The ${\psi }_{i}$ represent the unique component of the variation of each observed variable. The proportion of variation for each variable accounted for by the factors is known as the communality. For this routine it is assumed that both the $k$ factors and the ${e}_{i}$'s follow independent Normal distributions.
The model for the variance-covariance matrix, $\Sigma$, can be written as:
 $Σ=ΛΛT+Ψ$ (1)
where $\Lambda$ is the matrix of the factor loadings, ${\lambda }_{ij}$, and $\Psi$ is a diagonal matrix of unique variances, ${\psi }_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,p$.
The estimation of the parameters of the model, $\Lambda$ and $\Psi$, by maximum likelihood is described by Lawley and Maxwell (1971). The log-likelihood is:
 $-12n-1logΣ-12n-1traceS,Σ-1+constant,$
where $n$ is the number of observations, $S$ is the sample variance-covariance matrix or, if weights are used, $S$ is the weighted sample variance-covariance matrix and $n$ is the effective number of observations, that is, the sum of the weights. The constant is independent of the parameters of the model. A two stage maximization is employed. It makes use of the function $F\left(\Psi \right)$, which is, up to a constant, $-2/\left(n-1\right)$ times the log-likelihood maximized over $\Lambda$. This is then minimized with respect to $\Psi$ to give the estimates, $\stackrel{^}{\Psi }$, of $\Psi$. The function $F\left(\Psi \right)$ can be written as:
 $FΨ=∑j=k+1pθj-log⁡θj-p-k$
where values ${\theta }_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,p$ are the eigenvalues of the matrix:
 $S*=Ψ-1/2SΨ-1/2.$
The estimates $\stackrel{^}{\Lambda }$, of $\Lambda$, are then given by scaling the eigenvectors of ${S}^{*}$, which are denoted by $V$:
 $Λ^=Ψ1/2VΘ-I1/2.$
where $\Theta$ is the diagonal matrix with elements ${\theta }_{i}$, and $I$ is the identity matrix.
The minimization of $F\left(\Psi \right)$ is performed using E04LBF which uses a modified Newton algorithm. The computation of the Hessian matrix is described by Clark (1970). However, instead of using the eigenvalue decomposition of the matrix ${S}^{*}$ as described above, the singular value decomposition of the matrix $R{\Psi }^{-1/2}$ is used, where $R$ is obtained either from the $QR$ decomposition of the (scaled) mean centred data matrix or from the Cholesky decomposition of the correlation/covariance matrix. The routine E04LBF ensures that the values of ${\psi }_{i}$ are greater than a given small positive quantity, $\delta$, so that the communality is always less than one. This avoids the so called Heywood cases.
In addition to the values of $\Lambda$, $\Psi$ and the communalities, G03CAF returns the residual correlations, i.e., the off-diagonal elements of $C-\left(\Lambda {\Lambda }^{\mathrm{T}}+\Psi \right)$ where $C$ is the sample correlation matrix. G03CAF also returns the test statistic:
 $χ2=n-1-2p+5/6-2k/3FΨ^$
which can be used to test the goodness-of-fit of the model (1), see Lawley and Maxwell (1971) and Morrison (1967).

## 4  References

Clark M R B (1970) A rapidly convergent method for maximum likelihood factor analysis British J. Math. Statist. Psych.
Hammarling S (1985) The singular value decomposition in multivariate statistics SIGNUM Newsl. 20(3) 2–25
Lawley D N and Maxwell A E (1971) Factor Analysis as a Statistical Method (2nd Edition) Butterworths
Morrison D F (1967) Multivariate Statistical Methods McGraw–Hill

## 5  Parameters

1:     MATRIX – CHARACTER(1)Input
On entry: selects the type of matrix on which factor analysis is to be performed.
${\mathbf{MATRIX}}=\text{'D'}$
The data matrix will be input in X and factor analysis will be computed for the correlation matrix.
${\mathbf{MATRIX}}=\text{'S'}$
The data matrix will be input in X and factor analysis will be computed for the covariance matrix, i.e., the results are scaled as described in Section 8.
${\mathbf{MATRIX}}=\text{'C'}$
The correlation/variance-covariance matrix will be input in X and factor analysis computed for this matrix.
Constraint: ${\mathbf{MATRIX}}=\text{'D'}$, $\text{'S'}$ or $\text{'C'}$.
2:     WEIGHT – CHARACTER(1)Input
On entry: if ${\mathbf{MATRIX}}=\text{'D'}$ or $\text{'S'}$, WEIGHT indicates if weights are to be used.
${\mathbf{WEIGHT}}=\text{'U'}$
No weights are used.
${\mathbf{WEIGHT}}=\text{'W'}$
Weights are used and must be supplied in WT.
Note:  if ${\mathbf{MATRIX}}=\text{'C'}$, WEIGHT is not referenced.
Constraint: if ${\mathbf{MATRIX}}=\text{'D'}$ or $\text{'S'}$, ${\mathbf{WEIGHT}}=\text{'U'}$ or $\text{'W'}$.
3:     N – INTEGERInput
On entry: if ${\mathbf{MATRIX}}=\text{'D'}$ or $\text{'S'}$ the number of observations in the data array X.
If ${\mathbf{MATRIX}}=\text{'C'}$ the (effective) number of observations used in computing the (possibly weighted) correlation/variance-covariance matrix input in X.
Constraint: ${\mathbf{N}}>{\mathbf{NVAR}}$.
4:     M – INTEGERInput
On entry: the number of variables in the data/correlation/variance-covariance matrix.
Constraint: ${\mathbf{M}}\ge {\mathbf{NVAR}}$.
5:     X(LDX,M) – REAL (KIND=nag_wp) arrayInput
On entry: the input matrix.
If ${\mathbf{MATRIX}}=\text{'D'}$ or $\text{'S'}$, X must contain the data matrix, i.e., ${\mathbf{X}}\left(\mathit{i},\mathit{j}\right)$ must contain the $\mathit{i}$th observation for the $\mathit{j}$th variable, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,{\mathbf{M}}$.
If ${\mathbf{MATRIX}}=\text{'C'}$, X must contain the correlation or variance-covariance matrix. Only the upper triangular part is required.
6:     LDX – INTEGERInput
On entry: the first dimension of the array X as declared in the (sub)program from which G03CAF is called.
Constraints:
• if ${\mathbf{MATRIX}}=\text{'D'}$ or $\text{'S'}$, ${\mathbf{LDX}}\ge {\mathbf{N}}$;
• if ${\mathbf{MATRIX}}=\text{'C'}$, ${\mathbf{LDX}}\ge {\mathbf{M}}$.
7:     NVAR – INTEGERInput
On entry: $p$, the number of variables in the factor analysis.
Constraint: ${\mathbf{NVAR}}\ge 2$.
8:     ISX(M) – INTEGER arrayInput
On entry: ${\mathbf{ISX}}\left(\mathit{j}\right)$ indicates whether or not the $\mathit{j}$th variable is included in the factor analysis. If ${\mathbf{ISX}}\left(\mathit{j}\right)\ge 1$, the variable represented by the $\mathit{j}$th column of X is included in the analysis; otherwise it is excluded, for $\mathit{j}=1,2,\dots ,{\mathbf{M}}$.
Constraint: ${\mathbf{ISX}}\left(j\right)>0$ for NVAR values of $j$.
9:     NFAC – INTEGERInput
On entry: $k$, the number of factors.
Constraint: $1\le {\mathbf{NFAC}}\le {\mathbf{NVAR}}$.
10:   WT($*$) – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array WT must be at least ${\mathbf{N}}$ if ${\mathbf{WEIGHT}}=\text{'W'}$ and ${\mathbf{MATRIX}}=\text{'D'}$ or $\text{'S'}$, and at least $1$ otherwise.
On entry: if ${\mathbf{WEIGHT}}=\text{'W'}$ and ${\mathbf{MATRIX}}=\text{'D'}$ or $\text{'S'}$, WT must contain the weights to be used in the factor analysis. The effective number of observations in the analysis will then be the sum of weights. If ${\mathbf{WT}}\left(i\right)=0.0$, the $i$th observation is not included in the analysis.
If ${\mathbf{WEIGHT}}=\text{'U'}$ or ${\mathbf{MATRIX}}=\text{'C'}$, WT is not referenced and the effective number of observations is $n$.
Constraint: if ${\mathbf{WEIGHT}}=\text{'W'}$, $\text{the sum of weights}>{\mathbf{NVAR}}$, ${\mathbf{WT}}\left(\mathit{i}\right)\ge 0.0$, for $\mathit{i}=1,2,\dots ,n$.
11:   E(NVAR) – REAL (KIND=nag_wp) arrayOutput
On exit: the eigenvalues ${\theta }_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,p$.
12:   STAT($4$) – REAL (KIND=nag_wp) arrayOutput
On exit: the test statistics.
${\mathbf{STAT}}\left(1\right)$
Contains the value $F\left(\stackrel{^}{\Psi }\right)$.
${\mathbf{STAT}}\left(2\right)$
Contains the test statistic, ${\chi }^{2}$.
${\mathbf{STAT}}\left(3\right)$
Contains the degrees of freedom associated with the test statistic.
${\mathbf{STAT}}\left(4\right)$
Contains the significance level.
13:   COM(NVAR) – REAL (KIND=nag_wp) arrayOutput
On exit: the communalities.
14:   PSI(NVAR) – REAL (KIND=nag_wp) arrayOutput
On exit: the estimates of ${\psi }_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,p$.
15:   RES(${\mathbf{NVAR}}×\left({\mathbf{NVAR}}-1\right)/2$) – REAL (KIND=nag_wp) arrayOutput
On exit: the residual correlations. The residual correlation for the $i$th and $j$th variables is stored in ${\mathbf{RES}}\left(\left(j-1\right)\left(j-2\right)/2+i\right)$, $i.
16:   FL(LDFL,NFAC) – REAL (KIND=nag_wp) arrayOutput
On exit: the factor loadings. ${\mathbf{FL}}\left(\mathit{i},\mathit{j}\right)$ contains ${\lambda }_{\mathit{i}\mathit{j}}$, for $\mathit{i}=1,2,\dots ,p$ and $\mathit{j}=1,2,\dots ,k$.
17:   LDFL – INTEGERInput
On entry: the first dimension of the array FL as declared in the (sub)program from which G03CAF is called.
Constraint: ${\mathbf{LDFL}}\ge {\mathbf{NVAR}}$.
18:   IOP($5$) – INTEGER arrayInput
On entry: options for the optimization. There are four options to be set:
 $\mathit{iprint}$ controls iteration monitoring; if $\mathit{iprint}\le 0$, then there is no printing of information else if $\mathit{iprint}>0$, then information is printed at every iprint iterations. The information printed consists of the value of $F\left(\Psi \right)$ at that iteration, the number of evaluations of $F\left(\Psi \right)$, the current estimates of the communalities and an indication of whether or not they are at the boundary. $\mathit{maxfun}$ the maximum number of function evaluations. $\mathit{acc}$ the required accuracy for the estimates of ${\psi }_{i}$. $\mathit{eps}$ a lower bound for the values of $\psi$, see Section 3.
Let  then if ${\mathbf{IOP}}\left(1\right)=0$, then the following default values are used:
• $\mathit{iprint}=-1$
• $\mathit{maxfun}=100p$
• $\mathit{acc}=10\sqrt{\epsilon }$
• $\mathit{eps}=\epsilon$
If ${\mathbf{IOP}}\left(1\right)\ne 0$, then
• $\mathit{iprint}={\mathbf{IOP}}\left(2\right)$
• $\mathit{maxfun}={\mathbf{IOP}}\left(3\right)$
• $\mathit{acc}={10}^{-l}$ where $l={\mathbf{IOP}}\left(4\right)$
• $\mathit{eps}={10}^{-l}$ where $l={\mathbf{IOP}}\left(5\right)$
Constraint: if ${\mathbf{IOP}}\left(1\right)\ne 0$, ${\mathbf{IOP}}\left(\mathit{i}\right)$ must be such that $\mathit{maxfun}\ge 1$, $\epsilon \le \mathit{acc}<1$ and $\epsilon \le \mathit{eps}<1$, for $\mathit{i}=3,4,5$.
19:   IWK($4×{\mathbf{NVAR}}+2$) – INTEGER arrayWorkspace
20:   WK(LWK) – REAL (KIND=nag_wp) arrayWorkspace
21:   LWK – INTEGERInput
On entry: the dimension of the array WK as declared in the (sub)program from which G03CAF is called. The length of the workspace.
Constraints:
• if ${\mathbf{MATRIX}}=\text{'D'}$ or $\text{'S'}$, ${\mathbf{LWK}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(\left(5×{\mathbf{NVAR}}×{\mathbf{NVAR}}+33×{\mathbf{NVAR}}-4\right)/2,\text{}{\mathbf{N}}×{\mathbf{NVAR}}+7×{\mathbf{NVAR}}+{\mathbf{NVAR}}×\left({\mathbf{NVAR}}-1\right)/2\right)$;
• if ${\mathbf{MATRIX}}=\text{'C'}$, ${\mathbf{LWK}}\ge \left(5×{\mathbf{NVAR}}×{\mathbf{NVAR}}+33×{\mathbf{NVAR}}-4\right)/2$.
22:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, because for this routine the values of the output parameters may be useful even if ${\mathbf{IFAIL}}\ne {\mathbf{0}}$ on exit, the recommended value is $-1$. When the value $-\mathbf{1}\text{​ or ​}1$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Note: G03CAF may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{LDFL}}<{\mathbf{NVAR}}$, or ${\mathbf{NVAR}}<2$, or ${\mathbf{N}}\le {\mathbf{NVAR}}$, or ${\mathbf{NFAC}}<1$, or ${\mathbf{NVAR}}<{\mathbf{NFAC}}$, or ${\mathbf{M}}<{\mathbf{NVAR}}$, or ${\mathbf{MATRIX}}=\text{'D'}$ or $\text{'S'}$ and ${\mathbf{LDX}}<{\mathbf{N}}$, or ${\mathbf{MATRIX}}=\text{'C'}$ and ${\mathbf{LDX}}<{\mathbf{M}}$, or ${\mathbf{MATRIX}}\ne \text{'D'}$, $\text{'S'}$ or $\text{'C'}$, or ${\mathbf{MATRIX}}=\text{'D'}$ or $\text{'S'}$ and ${\mathbf{WEIGHT}}\ne \text{'U'}$ or $\text{'W'}$, or ${\mathbf{IOP}}\left(1\right)\ne 0$ and ${\mathbf{IOP}}\left(3\right)$ is such that $\mathit{maxfun}<1$, or ${\mathbf{IOP}}\left(1\right)\ne 0$ and ${\mathbf{IOP}}\left(4\right)$ is such that $\mathit{acc}\ge 1.0$, or ${\mathbf{IOP}}\left(1\right)\ne 0$ and ${\mathbf{IOP}}\left(4\right)$ is such that , or ${\mathbf{IOP}}\left(1\right)\ne 0$ and ${\mathbf{IOP}}\left(5\right)$ is such that $\mathit{eps}\ge 1.0$, or ${\mathbf{IOP}}\left(1\right)\ne 0$ and ${\mathbf{IOP}}\left(5\right)$ is such that , or ${\mathbf{MATRIX}}=\text{'C'}$ and ${\mathbf{LWK}}<\left(5×{\mathbf{NVAR}}×{\mathbf{NVAR}}+33×{\mathbf{NVAR}}-4\right)/2$, or ${\mathbf{MATRIX}}=\text{'D'}$ or $\text{'S'}$ and ${\mathbf{LWK}}<\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(\left(5×{\mathbf{NVAR}}×{\mathbf{NVAR}}+33×{\mathbf{NVAR}}-4\right)/2,{\mathbf{N}}×{\mathbf{NVAR}}+7×{\mathbf{NVAR}}+\phantom{\rule{0ex}{0ex}}{\mathbf{NVAR}}×\left({\mathbf{NVAR}}-1\right)/2\right)$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{WEIGHT}}=\text{'W'}$ and a value of ${\mathbf{WT}}<0.0$.
${\mathbf{IFAIL}}=3$
On entry, there are not exactly NVAR elements of ${\mathbf{ISX}}>0$, or the effective number of observations $\text{}\le {\mathbf{NVAR}}$.
${\mathbf{IFAIL}}=4$
On entry, ${\mathbf{MATRIX}}=\text{'D'}$ or $\text{'S'}$ and the data matrix is not of full column rank, or ${\mathbf{MATRIX}}=\text{'C'}$ and the input correlation/variance-covariance matrix is not positive definite.
This exit may also be caused by two of the eigenvalues of ${S}^{*}$ being equal; this is rare (see Lawley and Maxwell (1971)), and may be due to the data/correlation matrix being almost singular.
${\mathbf{IFAIL}}=5$
A singular value decomposition has failed to converge. This is a very unlikely error exit.
${\mathbf{IFAIL}}=6$
The estimation procedure has failed to converge in the given number of iterations. Change IOP to either increase number of iterations $\mathit{maxfun}$ or increase the value of $\mathit{acc}$.
${\mathbf{IFAIL}}=7$
The convergence is not certain but a lower point could not be found. See E04LBF for further details. In this case all results are computed.

## 7  Accuracy

The accuracy achieved is discussed in E04LBF with the value of the parameter XTOL given by $\mathit{acc}$ as described in parameter IOP.

The factor loadings may be orthogonally rotated by using G03BAF and factor score coefficients can be computed using G03CCF. The maximum likelihood estimators are invariant to a change in scale. This means that the results obtained will be the same (up to a scaling factor) if either the correlation matrix or the variance-covariance matrix is used. As the correlation matrix ensures that all values of ${\psi }_{i}$ are between $0$ and $1$ it will lead to a more efficient optimization. In the situation when the data matrix is input the results are always computed for the correlation matrix and then scaled if the results for the covariance matrix are required. When you input the covariance/correlation matrix the input matrix itself is used and you are advised to input the correlation matrix rather than the covariance matrix.

## 9  Example

This example is taken from Lawley and Maxwell (1971). The correlation matrix for nine variables is input and the parameters of a factor analysis model with three factors are estimated and printed.

### 9.1  Program Text

Program Text (g03cafe.f90)

### 9.2  Program Data

Program Data (g03cafe.d)

### 9.3  Program Results

Program Results (g03cafe.r)