# Factor Analysis

## Data

Access the lessR data set called datMach4 for the analysis of 351 people to the Mach IV scale. Read the optional variable labels. Including the item contents as variable labels means that the output of the confirmatory factor analysis contains the item content grouped by factor.

d <- Read("Mach4", quiet=TRUE)
l <- Read("Mach4_lbl", var_labels=TRUE)
##
## >>> Suggestions
##
## Data Types
## ------------------------------------------------------------
## character: Non-numeric data values
## ------------------------------------------------------------
##
##     Variable                  Missing  Unique
##         Name     Type  Values  Values  Values   First and last values
## ------------------------------------------------------------------------------------------
##  1     label character     20       0      20   Never tell anyone the real reason you did something unless it is useful to do so ... Most people forget more easily the death of a parent than the loss of their property
## ------------------------------------------------------------------------------------------

Calculate the correlations and store in here in mycor, a data structure that contains the computed correlation matrix with the name R. Extract R from mycor.

mycor <- cr(m01:m20)

R <- mycor$R The correlation matrix for analysis is named R. The item (observed variable) correlation matrix is the numerical input into the confirmatory factor analysis. ## Exploratory Factor Analysis Here do the default two-factor solution with "promax" rotation. The default correlation matrix is mycor. The abbreviation for corEFA() is efa(). efa(R, n_factors=4) ## EXPLORATORY FACTOR ANALYSIS ## ## Loadings (except -0.2 to 0.2) ## ------------------------------------- ## Factor1 Factor2 Factor3 Factor4 ## m06 0.828 -0.290 ## m07 0.712 ## m10 0.539 ## m03 0.422 0.318 ## m09 0.323 ## m05 0.649 ## m18 0.555 0.253 ## m13 0.543 0.226 ## m01 0.490 ## m12 0.434 -0.230 ## m08 0.236 -0.202 ## m14 0.402 0.991 -0.401 ## m04 0.426 ## m20 0.237 -0.282 ## m17 0.267 ## m19 ## m11 -0.299 0.309 -0.609 ## m16 0.274 -0.455 ## m02 -0.319 ## m15 -0.207 0.203 -0.214 ## ## Sum of Squares ## ------------------------------------------------ ## Factor1 Factor2 Factor3 Factor4 ## SS loadings 1.933 2.038 1.825 1.099 ## Proportion Var 0.097 0.102 0.091 0.055 ## Cumulative Var 0.097 0.199 0.290 0.345 ## ## CONFIRMATORY FACTOR ANALYSIS CODE ## ## MeasModel <- ## " F1 =~ m01 + m02 + m03 + m04 + m05 ## F2 =~ m06 + m07 + m08 + m09 + m10 + m11 ## F3 =~ m12 + m13 + m14 + m15 ## F4 =~ m17 + m18 + m19 + m20 ## " ## ## fit <- lessR::cfa(MeasModel) ## ## library(lavaan) ## fit <- lavaan::cfa(MeasModel, data=d) ## summary(fit, fit.measures=TRUE, standardized=TRUE) ## ## Deletion threshold: min_loading = 0.2 ## Deleted items: m16 ## Confirmatory Factor Analysis The confirmatory factor analysis is of multiple-indicator measurement scales, that is, each item (observed variable) is assigned to only one factor. Solution method is centroid factor analysis. Specify the measurement model for the analysis in Lavaan notation. Define four factors: Deceit, Trust, Cynicism, and Flattery. MeasModel <- " Deceit =~ m07 + m06 + m10 + m09 Trust =~ m12 + m05 + m13 + m01 Cynicism =~ m11 + m16 + m04 Flattery =~ m15 + m02 " Do the confirmatory factor analysis of 4-factor solution of Mach IV scale, Hunter, Gerbing and Boster (1982). By default output goes to the console, or, can store the output into an object, here cfa_out, which can then be displayed as a whole, or piecemeal. The abbreviation for corCFA() is cfa(). cfa_out <- cfa(MeasModel, R) cfa_out ## FACTOR / SCALE COMPOSITION ## ## F1 ## ------------------------------ ## m07: There is no excuse for lying to someone else ## m06: Honesty is the best policy in all cases ## m10: When you ask someone to do something for you, it is best to give the real reasons for wanting ## it rather than giving reasons which carry more weight ## m09: All in all, it is better to be humble and honest than to be important and dishonest ## ## F2 ## ------------------------------ ## m12: Anyone who completely trusts anyone else is asking for trouble ## m05: It is safest to assume that all people have a vicious streak and it will come out when they ## are given a chance ## m13: The biggest difference between most criminals and other people is that the criminals are ## stupid enough to get caught ## m01: Never tell anyone the real reason you did something unless it is useful to do so ## ## F3 ## ------------------------------ ## m11: Most people who get ahead in the world lead clean, moral lives ## m16: It is possible to be good in all respects ## m04: Most people are basically good and kind ## ## F4 ## ------------------------------ ## m15: It is wise to flatter important people ## m02: The best way to handle people is to tell them what they want to hear ## ## RELIABILITY ANALYSIS ## ## Scale Alpha Omega ## ----------------------- ## F1 0.691 0.701 ## F2 0.515 0.517 ## F3 0.402 0.421 ## F4 0.400 0.400 ## ## SOLUTION ## ## Fac Indi Pat Unique Factors with which an indicator correlates too ## tor cator tern ness highly, and other indicator diagnostics. ## --------------------------------------------------------------------------- ## ## F1 m07 0.743 0.448 ## F1 m06 0.682 0.535 ## F1 m10 0.578 0.666 ## F1 m09 0.409 0.832 ## ## F2 m12 0.540 0.708 ## F2 m05 0.446 0.801 ## F2 m13 0.435 0.810 ## F2 m01 0.415 0.828 ## ## F3 m11 0.549 0.698 ## F3 m16 0.510 0.740 ## F3 m04 0.255 0.935 F2 ## ## F4 m15 0.500 0.750 ## F4 m02 0.500 0.750 ## ## Cyn Fla ## Dec Tru ici tte ## m07 m06 m10 m09| m12 m05 m13 m01| m11 m16 m04| m15 m02| eit st sm ry ## ## m07 55 52 40 32| -16 -10 -11 -13| 22 21 14| -17 | 74 -27 43 -26 ## m06 52 47 41 25| -14 -11 -16 -11| 29 24 | -15 | 68 -28 47 -19 ## m10 40 41 33 25| -18 -15| 15 11 11| -18 -22| 58 -23 28 -40 ## m09 32 25 25 17| -14 -13 -14| 16 17 14| -17 -18| 41 -27 36 -35 ## ---------------------------------------------------------------------------------- ## m12 -16 -14 -18 -14| 29 23 22 25| -20 -15 -18| 18| -26 54 -40 26 ## m05 -10 -11 | 23 20 22 17| -13 -19 | 12| -13 45 -31 21 ## m13 -11 -16 -13| 22 22 19 17| -11 -14| 16 13| -20 44 -21 29 ## m01 -13 -11 -15 -14| 25 17 17 17| | 11 | -22 42 -19 18 ## ---------------------------------------------------------------------------------- ## m11 22 29 15 16| -20 -13 -11 | 30 28 14| | 34 -29 55 ## m16 21 24 11 17| -15 -19 | 28 26 13| | 30 -25 51 ## m04 14 11 14| -18 -14 | 14 13 | -15| 20 -27 25 -16 ## ---------------------------------------------------------------------------------- ## m15 -17 -15 -18 -17| 16 11| | 25 25| -28 24 50 ## m02 -22 -18| 18 12 13 | -15| 25 25| -22 27 -13 50 ## ---------------------------------------------------------------------------------- ## Deceit 74 68 58 41| -26 -13 -20 -22| 34 30 20| -28 -22| 100 -44 64 -50 ## Trust -27 -28 -23 -27| 54 45 44 42| -29 -25 -27| 24 27| -44 100 -61 51 ## Cynicism 43 47 28 36| -40 -31 -21 -19| 55 51 25| -13| 64 -61 100 -16 ## Flattery -26 -19 -40 -35| 26 21 29 18| -16| 50 50| -50 51 -16 100 ## ## RESIDUALS ## ## m07 m06 m10 m09| m12 m05 m13 m01| m11 m16 m04| m15 m02| ## ## m07 | | | 09| ## m06 | | 05 | 13| ## m10 | 09 | -05 -08 | -08| ## m09 | -05 -07| 07| -07 -08| ## ------------------------------------------------------------- ## m12 | | -10| -06 | ## m05 09 | | -05 | | ## m13 -05| | 11 -07| | ## m01 -07| | | | ## ------------------------------------------------------------- ## m11 05 -05 | | | 05| ## m16 -08 | -05 11 | | | ## m04 07| -10 -07 | | -13| ## ------------------------------------------------------------- ## m15 -07| -06 | | | ## m02 09 13 -08 -08| | 05 -13| | ## ------------------------------------------------------------- ## ## Sum of Average ## Squares Abs Value ## ------- --------- ## m07 0.017 0.030 ## m06 0.024 0.032 ## m10 0.031 0.044 ## m09 0.028 0.042 ## m12 0.020 0.034 ## m05 0.017 0.029 ## m13 0.027 0.040 ## m01 0.014 0.028 ## m11 0.015 0.030 ## m16 0.026 0.036 ## m04 0.038 0.039 ## m15 0.015 0.028 ## m02 0.061 0.056 ## ## Total sum of squares for all items: 0.333 ## ## Root mean square residual for all items: 0.577 ## ## Average absolute residual w/o the diagonal: 0.036 ## ## ## ## LAVAAN SPECIFICATION ## ## MeasModel <- ## " ## Deceit =~ m07 + m06 + m10 + m09 ## Trust =~ m12 + m05 + m13 + m01 ## Cynicism =~ m11 + m16 + m04 ## Flattery =~ m15 + m02 ## " ## ## library(lavaan) ## fit <- lavaan::cfa(MeasModel, data=d, std.ov=TRUE, std.lv=TRUE) ## summary(fit, fit.measures=TRUE) ## ## -------- ## The preceding code fits the model from data frame: d ## To access the correlation matrix directly without the data ## replace the parameter data with the parameters ## sample.cov and sample.nobs ## These names refer to the name of the input correlation matrix and sample size ##  Or, view pieces. See the names of all the output components. The output includes: • Heat map: Ordered correlation matrix, items within factors • Item content: Items within factors • Reliability analysis: Coefficient alpha and omega • Solution: Factor pattern coefficients, indicator diagnostics • Residual matrix: Items within factors • Item Diagnostics: Sum of squares and average sum of squares by item • Lavaan code: Run the model in Lavaan for maximum likelihood solution names(cfa_out) ## [1] "call" "out_title_scales" "out_labels" "out_title_rel" "out_reliability" ## [6] "out_title_solution" "out_indicators" "out_solution" "out_title_residuals" "out_residuals" ## [11] "out_res_stats" "out_title_lavaan" "out_lavaan" "out_Rmd" "ff.cor" ## [16] "if.cor" "diag.cor" "alpha" "omega" "pred" ## [21] "resid" Here just view just the scale reliabilities. cfa_out$out_reliability
##  Scale   Alpha    Omega
## -----------------------
##    F1    0.691    0.701
##    F2    0.515    0.517
##    F3    0.402    0.421
##    F4    0.400    0.400

Can also just analysis item content, without the factor analysis.

cfa(MeasModel, labels="only")
##
## F1
## ------------------------------
## m07: There is no excuse for lying to someone else
## m06: Honesty is the best policy in all cases
## m10: When you ask someone to do something for you, it is best to give the real reasons for wanting it rather than giving reasons which carry more weight
## m09: All in all, it is better to be humble and honest than to be important and dishonest
##
## F2
## ------------------------------
## m12: Anyone who completely trusts anyone else is asking for trouble
## m05: It is safest to assume that all people have a vicious streak and it will come out when they are given a chance
## m13: The biggest difference between most criminals and other people is that the criminals are stupid enough to get caught
## m01: Never tell anyone the real reason you did something unless it is useful to do so
##
## F3
## ------------------------------
## m11: Most people who get ahead in the world lead clean, moral lives
## m16: It is possible to be good in all respects
## m04: Most people are basically good and kind
##
## F4
## ------------------------------
## m15: It is wise to flatter important people
## m02: The best way to handle people is to tell them what they want to hear
## 

Generate R Markdown instructions of the confirmation factor analysis with the option: Rmd. Output file in this example will be m4.Rmd, a simple text file that can be edited with any text editor including RStudio, from which it can be knit to generate dynamic output such as to a Word document.

c <- cfa(MeasModel, R, Rmd="m4")

## Re-order the Correlation Matrix

### Hierarchical Cluster Analysis

Re-order the correlation matrix and display its heat map according to a hierarchical cluster analysis, indicated by the default parameter of the order parameter, hclust. Default input correlation matrix is the mycor structure, from which R will be extracted automatically.

The optional n_clusters parameter indicates to display corresponding cluster membership according to the specified number of clusters from the hierarchical tree. Although a four-factor solution, choose five clusters to allow for a “garbage” cluster.

corReorder(n_clusters=5)
##
##  5 Cluster Solution
## -------------------
## m01 m05 m08 m12 m13 m18 m20 m02 m15 m03 m06 m07 m09 m10 m04 m11 m14 m16 m17 m19
##   1   1   1   1   1   1   1   2   2   3   3   3   3   3   4   4   4   4   5   5

Use the hclust_type parameter to specify the type of cluster analysis provided by base R beyond the default of "complete". Other possibilities include "ward.D", "ward.D2", "single", "average".

### Manual Reorder

The variables in the correlation matrix can be re-ordered manually, including sub-setting the matrix. Do so with the vars parameter, specifying the index (ordinal positioning) of each variable in the re-ordered matrix.

Here, subset six variables from the original correlation matrix. Specifying the vars parameter automatically sets order to manual, so no need to separately specify.

R2 <- corReorder(vars=c(6,7,9,10,15,2))

R2
##       m06   m07   m09   m10   m15   m02
## m06  1.00  0.52  0.25  0.41 -0.15 -0.04
## m07  0.52  1.00  0.32  0.40 -0.17 -0.09
## m09  0.25  0.32  1.00  0.25 -0.17 -0.18
## m10  0.41  0.40  0.25  1.00 -0.18 -0.22
## m15 -0.15 -0.17 -0.17 -0.18  1.00  0.25
## m02 -0.04 -0.09 -0.18 -0.22  0.25  1.00

## Full Manual

Use the base R help() function to view the full manual for the factor analysis and correlation matrix functions. Simply enter a question mark followed by the name of the function.

?corCFA
?corEFA
?corScree
?corProp
?Correlation