# Factor Analysis Introduction with the Principal Component Method and R

**R – Aaron Schlegel**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Factor analysis is a controversial technique that represents the variables of a dataset [latex]y_1, y_2, \cdots, y_p[/latex] as linearly related to random, unobservable variables called factors, denoted [latex]f_1, f_2, \cdots, f_m[/latex] where [latex]m \lt p[/latex]. The factors are representative of ‘latent variables’ underlying the original variables. The existence of the factors is hypothetical as they cannot be measured or observed. Thus factor analysis remains controversial among statisticians (Rencher, 2002, pp. 443) and continues to be heavily researched.

The goal of factor analysis, similar to principal component analysis, is to reduce the original variables into a smaller number of factors that allows for easier interpretation. PCA and factor analysis still defer in several respects. One difference is principal components are defined as linear combinations of the variables while factors are defined as linear combinations of the underlying latent variables.

###### Factor Analysis

As mentioned, the factor analysis model is a linear combination of the underlying latent variables, [latex]f_1, f_2, \cdots, f_m[/latex], that are hypothetical in nature and may not actually exist. For the variables in any of the observation vectors in a sample, the model is defined as:

[latex display=”true”] y_1 – \mu_1 = \lambda_{11} f_1 + \lambda_{12} f_2 + \cdots + \lambda_{1m} f_m + \epsilon_1 [/latex]

[latex display=”true”] y_2 – \mu_2 = \lambda_{21} f_1 + \lambda_{22} f_2 + \cdots + \lambda_{2m} f_m + \epsilon_2 [/latex]

[latex display=”true”] \vdots [/latex]

[latex display=”true”] y_p – \mu_p = \lambda_{p1} f_1 + \lambda_{p2} f_2 + \cdots + \lambda_{pm} f_m + \epsilon_p [/latex]

Where [latex]\mu[/latex] is the mean vector and [latex]\epsilon[/latex] is a random error term to show the relationship between the factors is not exact. There are several assumptions that must be made regarding the relationships of the factor model described above.

- Assume the unobservable factors (latent variables) are independent of each other and of the error terms. For the factors [latex]j = 1, 2, \cdots, m[/latex], the expected value of the [latex]j[/latex]th factor is [latex]0[/latex], [latex]E(f_j) = 0[/latex]. The variance of the factor model is [latex]1[/latex], [latex]var(f_j) = 1[/latex], and the covariance of two factor models [latex]f_j[/latex] and [latex]f_k[/latex] is [latex]0[/latex], [latex]cov(f_j, f_k) = 0[/latex] where [latex]j \neq k[/latex].
- Assume the error terms [latex]\epsilon_i[/latex] are independent of each other. Thus, [latex]E(\epsilon) = 0[/latex], [latex]var(\epsilon_i) = \psi_i[/latex], and [latex]cov(\epsilon_i, \epsilon_j) = 0[/latex].
- The covariance of the error terms [latex]\epsilon_i[/latex] and the factor [latex]f_j[/latex] is [latex]0[/latex], [latex]cov(\epsilon_i, f_j) = 0[/latex].

Note the assumption [latex]cov(\epsilon_i, \epsilon_j) = 0[/latex] implies the factors represent all correlations among the observation vectors [latex]y[/latex]. Thus another difference that separates PCA and factor analysis is that factor analysis accounts for the covariances of correlations among the variables while PCA explains the total variance. With the assumptions made above, the variance of [latex]y_i[/latex] can be expressed as:

[latex display=”true”] var(y_i) = \lambda^2_{i1} + \lambda^2_{i2} + \cdots + \lambda^2_{im} + \psi_i [/latex]

Which can be expressed more compactly in matrix notation:

[latex display=”true”] y – \mu = \Lambda f + \epsilon [/latex]

We therefore have a partitioning of the variance of the observation vector [latex]y_i[/latex] into a component due to the common factors, which is called the communality and another called the specific variance. Communality is also referred to as common variance and [latex]\psi_i[/latex] is also known as specificity, unique or residual variance. The factors are grouped into a new term denoting the communality, [latex]h^2_i[/latex], with the error term [latex]\psi_i[/latex] representing the specific variance:

[latex display=”true”] var(y_i) = (\lambda^2_{i1} + \lambda^2_{i2} + \cdots + \lambda^2_{im}) + \psi_i [/latex][latex display=”true”] = h^2_i + \psi_i [/latex]

Which is the communality plus the specific variance.

It must be noted that factor analysis can fail to fit the data; however, a failed fit can indicate that it is not known how many factors there should be and what the factors are.

###### Estimation of Factor Loadings and Communalities with the Principal Component Method

There are several methods for estimating the factor loadings and communalities, including the principal component method, principal factor method, the iterated principal factor method and maximum likelihood estimation. The principal component method is one of the most common approaches to estimation and will be employed on the rootstock data seen in previous posts.

The principal component method is rather misleading in its naming it that no principal components are calculated. The approach of the principal component method is to calculate the sample covariance matrix [latex]S[/latex] from a sample of data and then find an estimator, denoted [latex]\hat{\Lambda}[/latex] that can be used to factor [latex]S[/latex].

[latex display=”true”] S = \hat{\Lambda} \hat{\Lambda}’ [/latex]

Another term, [latex]\Psi[/latex], is added to the estimate of [latex]S[/latex], making the above [latex]S = \hat{\Lambda} \hat{\Lambda}’ + \hat{\Psi}[/latex]. [latex]\hat{\Psi}[/latex] is a diagonal matrix of the specific variances [latex](\hat{\psi_1}, \hat{\psi_2}, \cdots, \hat{\psi_p})[/latex]. [latex]\Psi[/latex] is estimated in other approaches to factor analysis such as the principal factor method and its iterated version but is excluded in the principal component method of factor analysis. The reason for the term’s exclusion is since [latex]\hat{\Psi}[/latex] equals the specific variances of the variables, it models the diagonal of [latex]S[/latex] exactly.

Spectral decomposition is employed To factor [latex]S[/latex] into:

[latex display=”true”] S = CDC’ [/latex]

Where [latex]C[/latex] is an orthogonal matrix of the normalized eigenvectors of [latex]S[/latex] as columns and [latex]D[/latex] is a diagonal matrix with the diagonal equaling the eigenvalues of [latex]S[/latex]. Recall that all covariance matrices are positive semidefinite. Thus the eigenvalues must be either positive or zero which allows us to factor the diagonal matrix [latex]D[/latex] into:

[latex display=”true”] D = D^{1/2} D^{1/2} [/latex]

The above factor of [latex]D[/latex] is substituted into the decomposition of [latex]S[/latex].

[latex display=”true”] S = CDC’ = C D^{1/2} D^{1/2} C’ [/latex]

Then rearranging:

[latex display=”true”] S = (CD^{1/2})(CD^{1/2})’ [/latex]

Which yields the form [latex]S = \hat{\Lambda} \hat{\Lambda}'[/latex]. Since we are interested in finding [latex]m[/latex] factors in the data, we want to find a [latex]\hat{\Lambda}[/latex] that is [latex]p \times m[/latex] with [latex]m[/latex] smaller than [latex]p[/latex]. Thus [latex]D[/latex] can be defined as a diagonal matrix with [latex]m[/latex] eigenvalues (making it [latex]m \times m[/latex]) on the diagonal and [latex]C[/latex] is therefore [latex]p \times m[/latex] with the corresponding eigenvectors, which makes [latex]\hat{\Lambda}[/latex] [latex]p \times m[/latex]. There are numerous ways to select the number of factors, some of which include finding the number of eigenvalues greater than the average eigenvalue or plotting a scree plot.

###### Principal Component Method of Factor Analysis in R

The following example demonstrates factor analysis using the covariance matrix using the rootstock data seen in other posts. As mentioned in several of those posts, the measurements of the variables are not commensurate and thus using the covariance matrix for factor analysis (or PCA) does not make intuitive sense. In most cases, factoring the correlation matrix is recommended and is, in fact, more straightforward than using the covariance matrix as [latex]R[/latex] does not need to be decomposed into [latex]CDC'[/latex] beforehand. The correlation matrix [latex]R[/latex] of the data is employed for factor analysis in a follow-up post.

The rootstock data contains growth measurements of six different apple tree rootstocks from 1918 to 1934 (Andrews and Herzberg 1985, pp. 357-360) and were obtained from the companion FTP site of the book Methods of Multivariate Analysis by Alvin Rencher. The data contains four dependent variables as follows:

- trunk girth at four years (mm [latex]\times[/latex] 100)
- extension growth at four years (m)
- trunk girth at 15 years (mm [latex]\times[/latex] 100)
- weight of tree above ground at 15 years (lb [latex]\times[/latex] 1000)

Load the data and name the columns.

root <- read.table('ROOT.DAT', col.names = c('Tree.Number', 'Trunk.Girth.4.Years', 'Ext.Growth.4.Years', 'Trunk.Girth.15.Years', 'Weight.Above.Ground.15.Years'))

Find the covariance matrix [latex]S[/latex] with the `cov()`

function.

S <- cov(root[,2:5]) S ## Trunk.Girth.4.Years Ext.Growth.4.Years ## Trunk.Girth.4.Years 0.008373360 0.04753083 ## Ext.Growth.4.Years 0.047530829 0.34771174 ## Trunk.Girth.15.Years 0.018858555 0.14295747 ## Weight.Above.Ground.15.Years 0.009055532 0.07973026 ## Trunk.Girth.15.Years ## Trunk.Girth.4.Years 0.01885855 ## Ext.Growth.4.Years 0.14295747 ## Trunk.Girth.15.Years 0.22137762 ## Weight.Above.Ground.15.Years 0.13324894 ## Weight.Above.Ground.15.Years ## Trunk.Girth.4.Years 0.009055532 ## Ext.Growth.4.Years 0.079730255 ## Trunk.Girth.15.Years 0.133248936 ## Weight.Above.Ground.15.Years 0.089693957

The eigenvalues and eigenvectors are then computed from the covariance matrix with the `eigen()`

function.

S.eigen <- eigen(S) S.eigen ## $values ## [1] 0.495986813 0.162680761 0.006924035 0.001565068 ## ## $vectors ## [,1] [,2] [,3] [,4] ## [1,] -0.1011191 0.09661363 -0.21551730 0.9664332 ## [2,] -0.7516463 0.64386366 0.06099466 -0.1294103 ## [3,] -0.5600279 -0.62651631 -0.52992316 -0.1141384 ## [4,] -0.3334239 -0.42846553 0.81793239 0.1903481

Before proceeding with factoring [latex]S[/latex] into [latex]CDC'[/latex], the number of factors [latex]m[/latex] must be selected. The last two eigenvalues of [latex]S[/latex] are practically [latex]0[/latex], so [latex]m = 2[/latex] is likely a good choice. Plot a scree plot to confirm that two factors are appropriate.

plot(S.eigen$values, xlab = 'Eigenvalue Number', ylab = 'Eigenvalue Size', main = 'Scree Graph', type = 'b', xaxt = 'n') axis(1, at = seq(1, 4, by = 1))

With [latex]m = 2[/latex] factors, construct the [latex]C[/latex] and [latex]D[/latex] matrices from the covariance matrix with the first (largest) two eigenvalues and corresponding eigenvectors.

C <- as.matrix(S.eigen$vectors[,1:2]) D <- matrix(0, dim(C)[2], dim(C)[2]) diag(D) <- S.eigen$values[1:2]

[latex]\hat{\Lambda}[/latex] is then found from the [latex]C[/latex] and [latex]D[/latex] matrices as in [latex]\hat{\Lambda} = CD^{1/2}[/latex]

S.loadings <- C %*% sqrt(D) S.loadings ## [,1] [,2] ## [1,] -0.07121445 0.03896785 ## [2,] -0.52935694 0.25969406 ## [3,] -0.39440707 -0.25269723 ## [4,] -0.23481824 -0.17281602

Which are the unrotated factor loadings. We can see where the term ‘principal component method’ is derived from as the factors (columns of [latex]\hat{\Lambda}[/latex]) are proportional to the eigenvectors of [latex]S[/latex] which are equal to the corresponding coefficient of the principal components.

root.pca <- prcomp(root[,2:5])$rotation[,1:2] # Perform PCA on the rootstock data and take the resulting first two PCs root.pca ## PC1 PC2 ## Trunk.Girth.4.Years -0.1011191 0.09661363 ## Ext.Growth.4.Years -0.7516463 0.64386366 ## Trunk.Girth.15.Years -0.5600279 -0.62651631 ## Weight.Above.Ground.15.Years -0.3334239 -0.42846553 S.eigen$vectors[,1:2] ## [,1] [,2] ## [1,] -0.1011191 0.09661363 ## [2,] -0.7516463 0.64386366 ## [3,] -0.5600279 -0.62651631 ## [4,] -0.3334239 -0.42846553

The communality, the variance of the variables explained by the common factors, denoted [latex]h^2_i[/latex], as noted previously is the sum of squares of the rows of [latex]\hat{\Lambda}[/latex].

[latex display="true"] \hat{h}^2_i = \sum^m_{j=1} \hat{\lambda}^2_{ij} [/latex]

S.h2 <- rowSums(S.loadings^2) S.h2 ## [1] 0.006589992 0.347659774 0.219412829 0.085004979

The sum of squares of the columns of [latex]\hat{\Lambda}[/latex] are the respective eigenvalues of [latex]S[/latex].

colSums(S.loadings^2) ## [1] 0.4959868 0.1626808 S.eigen$values[1:2] ## [1] 0.4959868 0.1626808

The specific variance, [latex]\psi_i[/latex], is a component unique to the particular variable and is found by subtracting the diagonal of [latex]S[/latex] by the respective communality [latex]\hat{h}^2_i[/latex]:

[latex display="true"] \psi_i = s_{ii} - \hat{h}^2_i [/latex]

S.u2 <- diag(S) - S.h2 S.u2 ## Trunk.Girth.4.Years Ext.Growth.4.Years ## 1.783368e-03 5.197004e-05 ## Trunk.Girth.15.Years Weight.Above.Ground.15.Years ## 1.964786e-03 4.688978e-03

The proportion of variance of the loadings is found by dividing the sum of squares of the columns of [latex]\hat{\Lambda}[/latex] (the eigenvalues of [latex]S[/latex]) by the sum of the eigenvalues of [latex]S[/latex].

prop.loadings <- colSums(S.loadings^2) prop.var <- cbind(prop.loadings[1] / sum(S.eigen$values), prop.loadings[2] / sum(S.eigen$values)) prop.var ## [,1] [,2] ## [1,] 0.7434338 0.2438419

The proportion of variance explained by the loadings is computed by dividing the sum of squares of the columns of [latex]\hat{\Lambda}[/latex] by the sum of those squares.

prop.exp <- cbind(prop.loadings[1] / sum(prop.loadings), prop.loadings[2] / sum(prop.loadings)) prop.exp ## [,1] [,2] ## [1,] 0.7530154 0.2469846

Thus the two factor model represents and explains nearly all of the variance of the variables.

###### Factor Analysis with the `psych`

Package

The psych package has many functions available for performing factor analysis.

library(psych)

The `principal()`

function performs factor analysis with the principal component method as explained above. The rotation is set to `none`

for now as we have not yet done any rotation of the factors. The `covar`

argument is set to `TRUE`

so the function factors the covariance matrix [latex]S[/latex] of the data as we did above.

root.fa.covar <- principal(root[,2:5], nfactors = 2, rotate = 'none', covar = TRUE) root.fa.covar ## Principal Components Analysis ## Call: principal(r = root[, 2:5], nfactors = 2, rotate = "none", covar = TRUE) ## Unstandardized loadings (pattern matrix) based upon covariance matrix ## PC1 PC2 h2 u2 H2 U2 ## Trunk.Girth.4.Years 0.07 -0.04 0.0066 1.8e-03 0.79 0.21298 ## Ext.Growth.4.Years 0.53 -0.26 0.3477 5.2e-05 1.00 0.00015 ## Trunk.Girth.15.Years 0.39 0.25 0.2194 2.0e-03 0.99 0.00888 ## Weight.Above.Ground.15.Years 0.23 0.17 0.0850 4.7e-03 0.95 0.05228 ## ## PC1 PC2 ## SS loadings 0.50 0.16 ## Proportion Var 0.74 0.24 ## Cumulative Var 0.74 0.99 ## Proportion Explained 0.75 0.25 ## Cumulative Proportion 0.75 1.00 ## ## Standardized loadings (pattern matrix) ## item PC1 PC2 h2 u2 ## Trunk.Girth.4.Years 1 0.78 -0.43 0.79 0.21298 ## Ext.Growth.4.Years 2 0.90 -0.44 1.00 0.00015 ## Trunk.Girth.15.Years 3 0.84 0.54 0.99 0.00888 ## Weight.Above.Ground.15.Years 4 0.78 0.58 0.95 0.05228 ## ## PC1 PC2 ## SS loadings 2.73 1.00 ## Proportion Var 0.68 0.25 ## Cumulative Var 0.68 0.93 ## Cum. factor Var 0.73 1.00 ## ## Mean item complexity = 1.6 ## Test of the hypothesis that 2 components are sufficient. ## ## The root mean square of the residuals (RMSR) is 0 ## with the empirical chi square 0 with prob > NA ## ## Fit based upon off diagonal values = 1

The function’s output matches our calculations. H2 and U2 are the communality and specific variance, respectively, of the standardized loadings obtained from the correlation matrix [latex]R[/latex]. As the data were not measured on commensurate scales, it is more intuitive to employ the correlation matrix rather than the covariance matrix as the loadings can be dominated by variables with large variances on the diagonal of [latex]S[/latex].

###### Summary

I hope this served as a useful introduction to factor analysis. In the next few posts, we will explore the principal component method of factor analysis with the correlation matrix [latex]R[/latex] as well as rotation of the loadings to help improve interpretation of the factors.

###### References

Rencher, A. (2002). Methods of Multivariate Analysis (2nd ed.). Brigham Young University: John Wiley & Sons, Inc.

http://web.stanford.edu/class/psych253/tutorials/FactorAnalysis.html

http://www.yorku.ca/ptryfos/f1400.pdf

The post Factor Analysis Introduction with the Principal Component Method and R appeared first on Aaron Schlegel.

**leave a comment**for the author, please follow the link and comment on their blog:

**R – Aaron Schlegel**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.