library(psych)
#library(psychTools)
test<-read.table("test_score.csv", sep=",", header=T)
lowerCor(test)
## Y1 Y2 Y3 Y4 Y5 Y6
## Y1 1.00
## Y2 0.65 1.00
## Y3 0.70 0.57 1.00
## Y4 -0.56 -0.50 -0.38 1.00
## Y5 -0.46 -0.35 -0.27 0.81 1.00
## Y6 -0.44 -0.46 -0.24 0.83 0.82 1.00
pairs.panels(test,pch='.')

cor.plot(test,numbers=TRUE,main="6 variables from Test Score")

uls <- fa(test,2,rotate="varimax")
print(uls,sort=TRUE)
## Factor Analysis using method = minres
## Call: fa(r = test, nfactors = 2, rotate = "varimax")
## Standardized loadings (pattern matrix) based upon correlation matrix
## item MR1 MR2 h2 u2 com
## Y6 6 0.90 -0.20 0.86 0.14 1.1
## Y5 5 0.86 -0.20 0.78 0.22 1.1
## Y4 4 0.85 -0.36 0.85 0.15 1.3
## Y1 1 -0.32 0.82 0.77 0.23 1.3
## Y3 3 -0.11 0.81 0.66 0.34 1.0
## Y2 2 -0.31 0.67 0.54 0.46 1.4
##
## MR1 MR2
## SS loadings 2.49 1.98
## Proportion Var 0.41 0.33
## Cumulative Var 0.41 0.75
## Proportion Explained 0.56 0.44
## Cumulative Proportion 0.56 1.00
##
## Mean item complexity = 1.2
## Test of the hypothesis that 2 factors are sufficient.
##
## The degrees of freedom for the null model are 15 and the objective function was 4.28 with Chi Square of 205.97
## The degrees of freedom for the model are 4 and the objective function was 0.08
##
## The root mean square of the residuals (RMSR) is 0.02
## The df corrected root mean square of the residuals is 0.04
##
## The harmonic number of observations is 52 with the empirical chi square 0.58 with prob < 0.96
## The total number of observations was 52 with Likelihood Chi Square = 3.72 with prob < 0.45
##
## Tucker Lewis Index of factoring reliability = 1.006
## RMSEA index = 0 and the 90 % confidence intervals are 0 0.205
## BIC = -12.09
## Fit based upon off diagonal values = 1
## Measures of factor score adequacy
## MR1 MR2
## Correlation of (regression) scores with factors 0.96 0.91
## Multiple R square of scores with factors 0.91 0.83
## Minimum correlation of possible factor scores 0.82 0.66
fa.diagram(uls,digits = 2,main="Test Score Factors")

plot(uls)

#add factor scores
f <- factanal(test, factors=2, rotation="promax", scores="regression")
data <- cbind(test, f$scores) #add two variables
#another solution
fs <- factor.scores(test,uls) #obtain factor scores
fs <- fs$scores #get the columns of factor scores for each case
bfi <- cbind(test,fs) #append factor scores to dataset
#library(ggfortify)
#autoplot(f,label = TRUE, label.size = 3,
# loadings = TRUE, loadings.label = TRUE, loadings.label.size = 3)