Based on the William Revelle’s comprehensive psych package, qacr provides wrapper functions for simplified input, intuitive output, and easily interpretable graphs, making these techniques more accessible to data analysts new to these forms of analysis.

Principal Components Analysis

Let’s perform a principal components analysis on some ratings data. First we’ll import the data into R.

# input data
ratings <- read.csv("https://www.promptcloud.com/wp-content/uploads/2017/02/EFA.csv")
head(ratings)
#>   Price Safety Exterior_Looks Space_comfort Technology After_Sales_Service
#> 1     4      4              5             4          3                   4
#> 2     3      5              3             3          4                   4
#> 3     4      4              3             4          5                   5
#> 4     4      4              4             3          3                   4
#> 5     5      5              4             4          5                   4
#> 6     4      4              5             3          4                   5
#>   Resale_Value Fuel_Type Fuel_Efficiency Color Maintenance Test_drive
#> 1            5         4               4     2           4          2
#> 2            3         4               3     4           3          2
#> 3            5         4               5     4           5          4
#> 4            5         5               4     4           4          2
#> 5            5         3               4     5           5          5
#> 6            3         4               3     2           3          2
#>   Product_reviews Testimonials
#> 1               4            3
#> 2               2            2
#> 3               4            3
#> 4               5            3
#> 5               5            2
#> 6               2            3

Next, well create a scree plot of the data.


# scree plot
scree_plot(ratings)
#> 
#> Note: parallel analysis suggests 2 components.

The scree plot suggests two components. In the next step, we’ll extract two principal components and rotate them using a varimax rotation.

# extract 2 principal components
fit.pca <- PCA(ratings, nfactor=2, rotate="varimax")
#> 
#> Principal Components Analysis
#> Number of Factors: 2 Rotation: varimax 
#> 
#> Component Structure
#>                       PC1   PC2   h2
#> Price                0.54  0.08 0.30
#> Safety              -0.33  0.32 0.21
#> Exterior_Looks      -0.25  0.27 0.13
#> Space_comfort       -0.08  0.80 0.65
#> Technology           0.07  0.48 0.24
#> After_Sales_Service  0.24  0.60 0.42
#> Resale_Value         0.67 -0.24 0.51
#> Fuel_Type           -0.01  0.61 0.37
#> Fuel_Efficiency      0.69  0.30 0.56
#> Color                0.56 -0.04 0.32
#> Maintenance          0.74  0.05 0.55
#> Test_drive           0.19  0.41 0.20
#> Product_reviews      0.48  0.34 0.34
#> Testimonials        -0.06  0.33 0.11
#> 
#>                    PC1  PC2
#> Variance          2.59 2.33
#> Var Explained     0.19 0.17
#> Cum Var Explained 0.19 0.35

The two components account for 35% of the variance in the original data. Next, we’ll plot the pattern matrix as both a table and a bar chart.

# plot factor pattern as table
plot(fit.pca)


# plot factor pattern as bar chart
plot(fit.pca, type="bar")

Finally, we’ll add the component scores to the original data.

# save component scores
mydata <- score(ratings, fit.pca)
head(mydata)
#>   Price Safety Exterior_Looks Space_comfort Technology After_Sales_Service
#> 1     4      4              5             4          3                   4
#> 2     3      5              3             3          4                   4
#> 3     4      4              3             4          5                   5
#> 4     4      4              4             3          3                   4
#> 5     5      5              4             4          5                   4
#> 6     4      4              5             3          4                   5
#>   Resale_Value Fuel_Type Fuel_Efficiency Color Maintenance Test_drive
#> 1            5         4               4     2           4          2
#> 2            3         4               3     4           3          2
#> 3            5         4               5     4           5          4
#> 4            5         5               4     4           4          2
#> 5            5         3               4     5           5          5
#> 6            3         4               3     2           3          2
#>   Product_reviews Testimonials        PC1         PC2
#> 1               4            3 -0.3494735 -0.76706234
#> 2               2            2 -1.5464964 -1.58854139
#> 3               4            3  1.2949130  0.09537139
#> 4               5            3  0.5124876 -1.00007813
#> 5               5            2  1.4584077 -0.31851462
#> 6               2            3 -1.6599795 -0.85914096

Factor Analysis

In this section, the ratings data are re-analyzed using a maximum likelihood factor analysis.

Again, we’ll start with a scree plot. A random number seed is set to assure reproducibility.


# scree plot
set.seed(1234)
scree_plot(ratings, method = "ml")
#> 
#> Note: parallel analysis suggests 4 factors.

The parallel analysis suggests a maximum 4 factors, while the bend in the scree plot strongly suggests two factors. In the next step, we’ll extract 2 factors and rotate them using a promax rotation.

# extract 2 oblique factors
fit.fa <- FA(ratings, nfactor=2, rotate="promax", fm="ml")
#> 
#> Maximum Likelihood Factor Analysis
#> Number of Factors: 2 / Rotation: promax 
#> 
#> Factor Pattern
#>                        F1    F2   h2
#> Price                0.45  0.09 0.21
#> Safety              -0.24  0.28 0.13
#> Exterior_Looks      -0.17  0.20 0.06
#> Space_comfort       -0.04  0.87 0.76
#> Technology           0.09  0.33 0.12
#> After_Sales_Service  0.27  0.39 0.24
#> Resale_Value         0.59 -0.25 0.39
#> Fuel_Type            0.04  0.53 0.29
#> Fuel_Efficiency      0.64  0.15 0.44
#> Color                0.46 -0.12 0.22
#> Maintenance          0.65  0.02 0.42
#> Test_drive           0.20  0.22 0.10
#> Product_reviews      0.41  0.24 0.24
#> Testimonials        -0.04  0.19 0.04
#> 
#> Factor Structure
#>                        F1    F2
#> Price                0.45  0.12
#> Safety              -0.22  0.26
#> Exterior_Looks      -0.16  0.18
#> Space_comfort        0.02  0.87
#> Technology           0.11  0.33
#> After_Sales_Service  0.30  0.41
#> Resale_Value         0.57 -0.22
#> Fuel_Type            0.07  0.53
#> Fuel_Efficiency      0.65  0.19
#> Color                0.45 -0.09
#> Maintenance          0.65  0.06
#> Test_drive           0.22  0.24
#> Product_reviews      0.43  0.27
#> Testimonials        -0.03  0.19
#> 
#>                         F1   F2
#> SS loadings           1.98 1.68
#> Proportion Var        0.14 0.12
#> Cumulative Var        0.14 0.26
#> Proportion Explained  0.54 0.46
#> Cumulative Proportion 0.54 1.00
#> 
#> Factor Intercorrelations
#>      F1   F2
#> F1 1.00 0.07
#> F2 0.07 1.00

The printout includes the factor pattern, the factor structure, and the factor intercorrelations. The four factors account for 26% of the variance in the original data. Factors 1 and 2 have a low correlation (0.07), suggesting that an orthogonal (e.g., varimax) rotation may give a simpler solution.

Next, we’ll plot the pattern matrix as both a table and a bar chart.

# plot factor pattern as table
plot(fit.fa)


# plot factor pattern as bar chart
plot(fit.fa, type="bar")

Finally, we’ll add the factor scores to the original data.

# save component scores
mydata <- score(ratings, fit.fa)
head(mydata)
#>   Price Safety Exterior_Looks Space_comfort Technology After_Sales_Service
#> 1     4      4              5             4          3                   4
#> 2     3      5              3             3          4                   4
#> 3     4      4              3             4          5                   5
#> 4     4      4              4             3          3                   4
#> 5     5      5              4             4          5                   4
#> 6     4      4              5             3          4                   5
#>   Resale_Value Fuel_Type Fuel_Efficiency Color Maintenance Test_drive
#> 1            5         4               4     2           4          2
#> 2            3         4               3     4           3          2
#> 3            5         4               5     4           5          4
#> 4            5         5               4     4           4          2
#> 5            5         3               4     5           5          5
#> 6            3         4               3     2           3          2
#>   Product_reviews Testimonials         F1          F2
#> 1               4            3 -0.2191629 -0.27112360
#> 2               2            2 -1.4450747 -1.44074857
#> 3               4            3  1.2040675  0.03516678
#> 4               5            3  0.3792889 -1.12791694
#> 5               5            2  1.1548109 -0.18276132
#> 6               2            3 -1.4025048 -1.14472586