knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) library(qacr) library(GPArotation)
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.
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)
Next, well create a scree plot of the data.
# scree plot scree_plot(ratings)
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")
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)
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")
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")
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.