Principal Component Analysis

Reading the data

# install psych package
# install.packages("psych")
# load psych package
library(psych)
## Warning: package 'psych' was built under R version 4.1.3
# 
# install nFactors package
# install.packages("nFactors")
#load nFactors package
library(nFactors)
## Warning: package 'nFactors' was built under R version 4.1.3
## Loading required package: lattice
## 
## Attaching package: 'nFactors'
## The following object is masked from 'package:lattice':
## 
##     parallel
# 
# install GPArotation package
# install.packages("GPArotation")
#load GPArotation package
library(GPArotation)

#Read in data file0
ss<-read.csv("C:/Users/Administrador/Documents/R/ABDA/PCA/StudentSurveyFA.csv", sep=",", header=T)
summary(ss)
##       BIO             GEO            CHEM            ALG            CALC      
##  Min.   :1.000   Min.   :1.00   Min.   :1.000   Min.   :1.00   Min.   :1.000  
##  1st Qu.:1.000   1st Qu.:1.00   1st Qu.:1.000   1st Qu.:2.00   1st Qu.:2.000  
##  Median :2.000   Median :2.00   Median :2.000   Median :3.00   Median :3.000  
##  Mean   :2.353   Mean   :2.17   Mean   :2.237   Mean   :3.05   Mean   :3.063  
##  3rd Qu.:3.000   3rd Qu.:3.00   3rd Qu.:3.000   3rd Qu.:4.00   3rd Qu.:4.000  
##  Max.   :5.000   Max.   :5.00   Max.   :5.000   Max.   :5.00   Max.   :5.000  
##       STAT      
##  Min.   :1.000  
##  1st Qu.:2.000  
##  Median :3.000  
##  Mean   :2.937  
##  3rd Qu.:4.000  
##  Max.   :5.000
# generate the correlation matrix
corMat <- cor(ss)
#
# display the correlation matrix
corMat
##            BIO       GEO      CHEM       ALG      CALC      STAT
## BIO  1.0000000 0.6822208 0.7470278 0.1153204 0.2134271 0.2028315
## GEO  0.6822208 1.0000000 0.6814857 0.1353557 0.2045215 0.2316288
## CHEM 0.7470278 0.6814857 1.0000000 0.0838225 0.1364251 0.1659747
## ALG  0.1153204 0.1353557 0.0838225 1.0000000 0.7709303 0.4094324
## CALC 0.2134271 0.2045215 0.1364251 0.7709303 1.0000000 0.5073147
## STAT 0.2028315 0.2316288 0.1659747 0.4094324 0.5073147 1.0000000

Use Factor Analysis

as PCA is a subcategory of FA

# use fa() to conduct factor analysis

# save the solution to an R variable
solution <- fa(r = corMat, nfactors = 2, rotate ="oblimin", fm ="pa")
#display the solution output
solution
## Factor Analysis using method =  pa
## Call: fa(r = corMat, nfactors = 2, rotate = "oblimin", fm = "pa")
## Standardized loadings (pattern matrix) based upon correlation matrix
##        PA1   PA2   h2    u2 com
## BIO   0.86  0.02 0.75 0.255 1.0
## GEO   0.78  0.05 0.63 0.369 1.0
## CHEM  0.87 -0.05 0.75 0.253 1.0
## ALG  -0.04  0.81 0.65 0.354 1.0
## CALC  0.01  0.96 0.92 0.081 1.0
## STAT  0.13  0.50 0.29 0.709 1.1
## 
##                        PA1  PA2
## SS loadings           2.14 1.84
## Proportion Var        0.36 0.31
## Cumulative Var        0.36 0.66
## Proportion Explained  0.54 0.46
## Cumulative Proportion 0.54 1.00
## 
##  With factor correlations of 
##      PA1  PA2
## PA1 1.00 0.21
## PA2 0.21 1.00
## 
## Mean item complexity =  1
## Test of the hypothesis that 2 factors are sufficient.
## 
## The degrees of freedom for the null model are  15  and the objective function was  2.87
## The degrees of freedom for the model are 4  and the objective function was  0.01 
## 
## The root mean square of the residuals (RMSR) is  0.01 
## The df corrected root mean square of the residuals is  0.02 
## 
## Fit based upon off diagonal values = 1
## Measures of factor score adequacy             
##                                                    PA1  PA2
## Correlation of (regression) scores with factors   0.94 0.96
## Multiple R square of scores with factors          0.88 0.93
## Minimum correlation of possible factor scores     0.77 0.86

Using prcomp()

PCA <- prcomp(ss, scale=TRUE)
plot(PCA$x[,1],PCA$x[,2])

pca.var=PCA$sdev^2
pca.var.per<-round(pca.var/sum(pca.var)*100,2)
barplot(pca.var.per,main="Scree plot",xlab="principal component",ylab="percentage variation")
barplot(pca.var.per,main="Scree plot",xlab="principal component",ylab="percentage variation")

loadingscores<-PCA$rotation[,1]