knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
The qacDR package provides functions for principal components and common factor analysis. Based on the William Revelle's comprehensive \link[psych]{https://cran.r-project.org/web/packages/psych/index.html} package, the \code{factorAnalysis} package provides 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.
library(qacDR) # scree plot screePlot(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 bar chart plot(fit.pca, sort=TRUE) # plot factor pattern as a table plot(fit.pca, sort=TRUE, type="table")
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. This time we'll run 200 simulations. A random number seed is set to assure reproducability.
library(qacDR) # scree plot set.seed(1234) screePlot(ratings, method = "ml", n.iter=200)
Again, the scree plot suggests two factors. In the next step, we'll extract two factors and rotate them using a promax rotation.
# extract 4 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. The factors have a correlation of 0.07 (i.e., practically orthogonal).
Next, we'll plot the pattern matrix as both a table and a bar chart.
# plot factor pattern as table plot(fit.fa, sort=TRUE) # plot factor pattern as bar chart plot(fit.fa, sort=TRUE, type="table")
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.