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.

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)

Next, well create a scree plot of the data.

library(qacDR)

# scree plot
scree_plot(ratings)

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

# extract 2 principal components
fit.pca <- PCA(ratings, nfactor=4, rotate="varimax")

The four components account for 55% 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)

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. This time we'll run 200 simulations. A random number seed is set to assure reproducability.

library(qacDR)

# scree plot
set.seed(1234)
scree_plot(ratings, method = "ml", n.iter=200)

Again, the scree plot suggests two factors. In the next step, we'll extract four factors and rotate them using a promax rotation.

# extract 4 factors
fit.fa <- FA(ratings, nfactor=4, rotate="promax", fm="ml")

The printout includes the factor pattern, the factor structure, and the factor intercorrelations. The four factors account for 40% of the variance in the original data. The largest factor intercorrelation is between factor 1 and factor 3 (0.37) .

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)


Rkabacoff/qacDR documentation built on March 17, 2022, 5:15 p.m.