Notebook of Reading Books: R in Action_Chapter 14.

This chapter covers

  • Principal components analysis

  • Exploratory factor analysis

  • Other latent variable models

    fig141

14.1. Principal components and factor analysis in R

14.2. Principal components

fun1

14.2.1. Selecting the number of components to extract

  • Figure 14.2. Assessing the number of principal components to retain for the US Judge Rating example.

    • A scree plot (the line with x’s), eigenvalues greater than 1 criteria (horizontal line),
    • and parallel analysis with 100 simulations (dashed line) suggest retaining a single component.

    fig142

14.2.2. Extracting principal components

  • Figure 14.3. Assessing the number of principal components to retain for the Body Measurements example.

    • The scree plot (line with x’s), eigenvalues greater than 1 criteria (horizontal line),
    • and parallel analysis with 100 simulations (dashed line) suggest retaining two components.

    fig143

14.2.3. Rotating principal components

14.2.4. Obtaining principal components scores

14.3. Exploratory factor analysis

14.3.1. Deciding how many common factors to extract

  • fa.parallel()

  • Figure 14.4. Assessing the number of factors to retain for the psychological tests example.

    • Results for both PCA and EFA are present.
    • The PCA results suggest one or two components.
    • The EFA results suggest two factors.

    fig144

14.3.2. Extracting common factors

  • fa()

  • fm="pa"

14.3.3. Rotating factors

  • rotate = "varimax"

  • rotate="promax"

  • install.packages(“GPArotation”)

  • Figure 14.5. Two factor plot for the psychological tests in ability.cov.

    • Vocab and reading load on the first factor (PA1).
    • while blocks, picture, and maze load on the second factor (PA2).
    • The general intelligence test loads on both.

    fig145

  • Figure 14.6. Diagram of the oblique two factor solution for the psychological test data in ability.cov

    fig146

14.3.4. Factor scores

14.4. Other latent variable models

  • Methods CFA and SEM with the sem(), openMx() and lavaan() packages.

  • Method MDS in the vagan() package.

14.5 Summary

  • Figure 14.7. A principal components/exploratory factor analysis decision chart

    fig147

Attach is the Script of chapter14.

Show me the code

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# Remove most objects from the working environment
rm(list = ls())
options(stringsAsFactors = F)

# 14.2.1. Selecting the number of components to extract
library(psych)
fa.parallel(USJudgeRatings[,-1], fa="pc", n.iter=100,
            show.legend=FALSE, main="Scree plot with parallel analysis")  # figure 14.2

# 14.2.2. Extracting principal components
# code listing 14.1. Principal components analysis of US Judge Ratings
library(psych)
pc <- principal(USJudgeRatings[, -1], nfactors = 1)
pc
# Principal Components Analysis
# Call: principal(r = USJudgeRatings[, -1], nfactors = 1)
# Standardized loadings (pattern matrix) based upon correlation matrix
#       PC1   h2     u2 com
# INTG 0.92 0.84 0.1565   1
# DMNR 0.91 0.83 0.1663   1
# DILG 0.97 0.94 0.0613   1
# CFMG 0.96 0.93 0.0720   1
# DECI 0.96 0.92 0.0763   1
# PREP 0.98 0.97 0.0299   1
# FAMI 0.98 0.95 0.0469   1
# ORAL 1.00 0.99 0.0091   1
# WRIT 0.99 0.98 0.0196   1
# PHYS 0.89 0.80 0.2013   1
# RTEN 0.99 0.97 0.0275   1
# 
# PC1
# SS loadings    10.13
# Proportion Var  0.92
# 
# Mean item complexity =  1
# Test of the hypothesis that 1 component is sufficient.
# 
# The root mean square of the residuals (RMSR) is  0.04 
# with the empirical chi square  6.21  with prob <  1 
# 
# Fit based upon off diagonal values = 1


library(psych)
fa.parallel(Harman23.cor$cov, n.obs=302, fa="pc", n.iter =100,
            show.legend=FALSE, main="Scree plot with parallel analysis")  # figure 14.3


# code listing 14.2. Principal components analysis of body measurements
library(psych)
PC <- principal(Harman23.cor$cov, nfactors = 2, rotate = "none")
PC
# Principal Components Analysis
# Call: principal(r = Harman23.cor$cov, nfactors = 2, rotate = "none")
# Standardized loadings (pattern matrix) based upon correlation matrix
#                 PC1   PC2   h2    u2 com
# height         0.86 -0.37 0.88 0.123 1.4
# arm.span       0.84 -0.44 0.90 0.097 1.5
# forearm        0.81 -0.46 0.87 0.128 1.6
# lower.leg      0.84 -0.40 0.86 0.139 1.4
# weight         0.76  0.52 0.85 0.150 1.8
# bitro.diameter 0.67  0.53 0.74 0.261 1.9
# chest.girth    0.62  0.58 0.72 0.283 2.0
# chest.width    0.67  0.42 0.62 0.375 1.7
# 
#                        PC1  PC2
# SS loadings           4.67 1.77
# Proportion Var        0.58 0.22
# Cumulative Var        0.58 0.81
# Proportion Explained  0.73 0.27
# Cumulative Proportion 0.73 1.00
# 
# Mean item complexity =  1.7
# Test of the hypothesis that 2 components are sufficient.
# 
# The root mean square of the residuals (RMSR) is  0.05 
# 
# Fit based upon off diagonal values = 0.99


#======================================================
# 14.2.3. Rotating principal components
# code lisitng 14.3. Principal components analysis with varimax rotation
rc <- principal(Harman23.cor$cov, nfactors = 2, rotate = "varimax")
rc
# Principal Components Analysis
# Call: principal(r = Harman23.cor$cov, nfactors = 2, rotate = "varimax")
# Standardized loadings (pattern matrix) based upon correlation matrix
#                 RC1  RC2   h2    u2 com
# height         0.90 0.25 0.88 0.123 1.2
# arm.span       0.93 0.19 0.90 0.097 1.1
# forearm        0.92 0.16 0.87 0.128 1.1
# lower.leg      0.90 0.22 0.86 0.139 1.1
# weight         0.26 0.88 0.85 0.150 1.2
# bitro.diameter 0.19 0.84 0.74 0.261 1.1
# chest.girth    0.11 0.84 0.72 0.283 1.0
# chest.width    0.26 0.75 0.62 0.375 1.2
# 
#                        RC1  RC2
# SS loadings           3.52 2.92
# Proportion Var        0.44 0.37
# Cumulative Var        0.44 0.81
# Proportion Explained  0.55 0.45
# Cumulative Proportion 0.55 1.00
# 
# Mean item complexity =  1.1
# Test of the hypothesis that 2 components are sufficient.
# 
# The root mean square of the residuals (RMSR) is  0.05 
# 
# Fit based upon off diagonal values = 0.99


#=======================================================
# 14.2.4. Obtaining principal components scores

# code listing 14.4. Obtaining component scores from raw data
library(psych)
pc <- principal(USJudgeRatings[,-1], nfactors=1, score=TRUE)
head(pc$scores)
#                       PC1
# AARONSON,L.H.  -0.1857981
# ALEXANDER,J.M.  0.7469865
# ARMENTANO,A.J.  0.0704772
# BERDON,R.I.     1.1358765
# BRACKEN,J.J.   -2.1586211
# BURNS,E.B.      0.7669406

cor(USJudgeRatings$CONT, pc$score)
#               PC1
# [1,] -0.008815895

# code listing 14.5. Obtaining principal component scoring coefficients
library(psych)
rc <- principal(Harman23.cor$cov, nfactors = 2, rotate = "varimax")
round(unclass(rc$weights), 2)
#                  RC1   RC2
# height          0.28 -0.05
# arm.span        0.30 -0.08
# forearm         0.30 -0.09
# lower.leg       0.28 -0.06
# weight         -0.06  0.33
# bitro.diameter -0.08  0.32
# chest.girth    -0.10  0.34
# chest.width    -0.04  0.27

PC1 = 0.28*height + 0.30*arm.span + 0.30*forearm + 0.28*lower.leg -
  0.06*weight - 0.08*bitro.diameter - 0.10*chest.girth -
  0.04*chest.width

PC2 = -0.05*height - 0.08*arm.span - 0.09*forearm - 0.06*lower.leg +
  0.33*weight + 0.32*bitro.diameter + 0.34*chest.girth +
  0.27*chest.width

# 14.3. Exploratory factor analysis
options(digits = 2)
covariances <- ability.cov$cov
correlations <- cov2cor(covariances)
correlations
# general picture blocks maze reading vocab
# general    1.00    0.47   0.55 0.34    0.58  0.51
# picture    0.47    1.00   0.57 0.19    0.26  0.24
# blocks     0.55    0.57   1.00 0.45    0.35  0.36
# maze       0.34    0.19   0.45 1.00    0.18  0.22
# reading    0.58    0.26   0.35 0.18    1.00  0.79
# vocab      0.51    0.24   0.36 0.22    0.79  1.00

#===================================================
# 14.3.1. Deciding how many common factors to extract
# figure 14.4
library(psych)
covatiances <- ability.cov$cov
correlations <- cov2cor(covariances)
fa.parallel(correlations, n.obs = 112, fa="both",
            n.iter = 100, main = "Scree plots with parallel analysis")


# 14.3.2. Extracting common factors
# code listing 14.6. Principal axis factoring without rotation
fa <- fa(correlations, nfactors = 2, rotate = "none", fm="pa")
fa
# Factor Analysis using method =  pa
# Call: fa(r = correlations, nfactors = 2, rotate = "none", fm = "pa")
# Standardized loadings (pattern matrix) based upon correlation matrix
#          PA1   PA2   h2    u2 com
# general 0.75  0.07 0.57 0.432 1.0
# picture 0.52  0.32 0.38 0.623 1.7
# blocks  0.75  0.52 0.83 0.166 1.8
# maze    0.39  0.22 0.20 0.798 1.6
# reading 0.81 -0.51 0.91 0.089 1.7
# vocab   0.73 -0.39 0.69 0.313 1.5
# 
#                        PA1  PA2
# SS loadings           2.75 0.83
# Proportion Var        0.46 0.14
# Cumulative Var        0.46 0.60
# Proportion Explained  0.77 0.23
# Cumulative Proportion 0.77 1.00
# 
# Mean item complexity =  1.5
# Test of the hypothesis that 2 factors are sufficient.
# 
# The degrees of freedom for the null model are  15  and the objective function was  2.5
# The degrees of freedom for the model are 4  and the objective function was  0.07 
# 
# The root mean square of the residuals (RMSR) is  0.03 
# The df corrected root mean square of the residuals is  0.06 
# 
# Fit based upon off diagonal values = 0.99
# Measures of factor score adequacy             
#                                                    PA1  PA2
# Correlation of (regression) scores with factors   0.96 0.92
# Multiple R square of scores with factors          0.93 0.84
# Minimum correlation of possible factor scores     0.86 0.68


#===========================================================
# 14.3.3. Rotating factors
# code listing 14.7. Factor extraction with orthogonal rotation
fa.varimax <- fa(correlations, nfactors = 2, rotate="varimax", fm="pa")
fa.varimax
# Factor Analysis using method =  pa
# Call: fa(r = correlations, nfactors = 2, rotate = "varimax", fm = "pa")
# Standardized loadings (pattern matrix) based upon correlation matrix
#          PA1  PA2   h2    u2 com
# general 0.49 0.57 0.57 0.432 2.0
# picture 0.16 0.59 0.38 0.623 1.1
# blocks  0.18 0.89 0.83 0.166 1.1
# maze    0.13 0.43 0.20 0.798 1.2
# reading 0.93 0.20 0.91 0.089 1.1
# vocab   0.80 0.23 0.69 0.313 1.2
# 
#                        PA1  PA2
# SS loadings           1.83 1.75
# Proportion Var        0.30 0.29
# Cumulative Var        0.30 0.60
# Proportion Explained  0.51 0.49
# Cumulative Proportion 0.51 1.00
# 
# Mean item complexity =  1.3
# Test of the hypothesis that 2 factors are sufficient.
# 
# The degrees of freedom for the null model are  15  and the objective function was  2.5
# The degrees of freedom for the model are 4  and the objective function was  0.07 
# 
# The root mean square of the residuals (RMSR) is  0.03 
# The df corrected root mean square of the residuals is  0.06 
# 
# Fit based upon off diagonal values = 0.99
# Measures of factor score adequacy             
#                                                    PA1  PA2
# Correlation of (regression) scores with factors   0.96 0.92
# Multiple R square of scores with factors          0.91 0.85
# Minimum correlation of possible factor scores     0.82 0.71



#============================================================
# code listing 14.8. Factor extraction with oblique rotation
# install.packages("GPArotation")
fa.promax <- fa(correlations, nfactors = 2, rotate="promax", fm="pa")
fa.promax
# Factor Analysis using method =  pa
# Call: fa(r = correlations, nfactors = 2, rotate = "promax", fm = "pa")
# Standardized loadings (pattern matrix) based upon correlation matrix
#           PA1   PA2   h2    u2 com
# general  0.37  0.48 0.57 0.432 1.9
# picture -0.03  0.63 0.38 0.623 1.0
# blocks  -0.10  0.97 0.83 0.166 1.0
# maze     0.00  0.45 0.20 0.798 1.0
# reading  1.00 -0.09 0.91 0.089 1.0
# vocab    0.84 -0.01 0.69 0.313 1.0
# 
#                        PA1  PA2
# SS loadings           1.83 1.75
# Proportion Var        0.30 0.29
# Cumulative Var        0.30 0.60
# Proportion Explained  0.51 0.49
# Cumulative Proportion 0.51 1.00
# 
# With factor correlations of 
#      PA1  PA2
# PA1 1.00 0.55
# PA2 0.55 1.00
# 
# Mean item complexity =  1.2
# Test of the hypothesis that 2 factors are sufficient.
# 
# The degrees of freedom for the null model are  15  and the objective function was  2.5
# The degrees of freedom for the model are 4  and the objective function was  0.07 
# 
# The root mean square of the residuals (RMSR) is  0.03 
# The df corrected root mean square of the residuals is  0.06 
# 
# Fit based upon off diagonal values = 0.99
# Measures of factor score adequacy             
#                                                    PA1  PA2
# Correlation of (regression) scores with factors   0.97 0.94
# Multiple R square of scores with factors          0.93 0.88
# Minimum correlation of possible factor scores     0.86 0.77

fsm <- function(oblique) {
  if (class(oblique)[2] == "fa" & is.null(oblique$Phi)) {
      warning("Object doesn't look like oblique EFA")
  } else {
      P <- unclass(oblique$loading)
      F <- P %*% oblique$Phi
      colnames(F) <- c("PA1", "PA2")
      return(F)
  }
}

fsm(fa.promax)
#          PA1  PA2
# general 0.64 0.69
# picture 0.32 0.61
# blocks  0.43 0.91
# maze    0.25 0.45
# reading 0.95 0.46
# vocab   0.83 0.45

factor.plot(fa.promax, labels=rownames(fa.promax$loadings)) # figure 14.5

fa.diagram(fa.promax, simple = F)


# 14.3.4. Factor scores
fa.promax$weights
#           PA1   PA2
# general 0.078 0.211
# picture 0.020 0.090
# blocks  0.037 0.702
# maze    0.027 0.035
# reading 0.743 0.030
# vocab   0.177 0.036`