1.Load the packages and set working directory
library(psych);library(ggplot2);library(reshape2)
setwd("~/Dropbox/R_wd/EFA_COURSE/NIHSS")
2. Data Preprocesing
Correlation matrix : R1
R <- read.csv("NIHSScsv.csv",header = T)
row.names(R) <- R[,1]
R <- R[,-1]
R[upper.tri(R)] <- t(R)[upper.tri(t(R))]
R <- as.matrix(R)
R1 <- R 
diag(R1) <- 1
3. Examine the appropriateness of dat for EFA
MSA and bartlett test
KMO(R1)
cortest.bartlett(R1, n = 152)
4. Determinate numbers of factor
Kaiser’s rule, Scree test, and Parallel analysis (50th and 99th)
eigen(R1)$values[eigen(R1)$values >1]
PA50 <- fa.parallel(R1,152, fa ="pc",quant=.5,plot = F)
PA99 <- fa.parallel(R1,152, fa ="pc",quant=.99,plot = F)
5. Plot the results
Kaiser’s rule, Scree test, and Parallel analysis
plotpa <- melt(data.frame(Actual = PA50$pc.values ,
                          PA50 = PA50$pc.sim, PA99 = PA99$pc.sim))
plotpa$index <- as.factor(rep(seq(1,15),3))
Fig <- ggplot(plotpa, aes(x = index,y = value, group = variable,
                   col = variable, linetype = variable))+
        geom_point(aes(size = variable))+
        geom_line()+
        theme_bw()+
        scale_x_discrete(labels = c(seq(1,15)))+
        scale_linetype_manual(values=c(1,2,2),name = "",
                              labels = c("Actual Data",
                                         "Parallel Analysis : 50%",
                                         "Parallel Analysis : 99%"))+
        scale_size_manual(values=c(3,1,1),name = "",
                          labels = c("Actual Data",
                                     "Parallel Analysis : 50%",
                                     "Parallel Analysis : 99%"))+
        scale_color_manual(values = c("firebrick",
                                      "chartreuse4",
                                      "chartreuse2"),name = "",
                           labels = c("Actual Data",
                                      "Parallel Analysis : 50%",
                                      "Parallel Analysis : 99%"))+
        labs(list(x = "Number", y = "Eigen Values"))+
        ggtitle("Scree Plots : Parallel Analysis")+
        theme(legend.position = c(0.7,0.77),
              plot.title = element_text(hjust = 0.5))
6. Initial estimation fo factor loadings
Using Iterated principal factor method (no rotation)
pam2 <- fa(R1,nfactors = 2,n.obs = 152, rotate = "none",fm  =  "pa")
pam3 <- fa(R1,nfactors = 3,n.obs = 152, rotate = "none",fm  =  "pa")
pam4 <- fa(R1,nfactors = 4,n.obs = 152, rotate = "none",fm  =  "pa")
7. Estimation fo factor loadings
Using Iterated principal factor method and Promax rotation
pam2 <- fa(R1,nfactors = 2,n.obs = 152, rotate = "Promax",fm  =  "pa")
pam3 <- fa(R1,nfactors = 3,n.obs = 152, rotate = "Promax",fm  =  "pa")
pam4 <- fa(R1,nfactors = 4,n.obs = 152, rotate = "Promax",fm  =  "pa")
8. Data analysis for reduced NIHSS (11 items)
The steps were similar to the original analyzes.
R1_v2 <- R1[-c(4,5,11,15),-c(4,5,11,15)]
eigen(R1_v2)$values[eigen(R1_v2)$values >1]
PA50 <- fa.parallel(R1_v2,152, fa ="pc",quant=.5,plot = F)
PA99 <- fa.parallel(R1_v2,152, fa ="pc",quant=.99,plot = F)
pam2 <- fa(R1_v2,nfactors = 2,n.obs = 152, rotate = "Promax",fm  =  "pa")
pam3 <- fa(R1_v2,nfactors = 3,n.obs = 152, rotate = "Promax",fm  =  "pa")
plotpa <- melt(data.frame(Actual = PA50$pc.values ,
                          PA50 = PA50$pc.sim, PA99 = PA99$pc.sim))
plotpa$index <- as.factor(rep(seq(1,11),3))
Fig2 <- ggplot(plotpa, aes(x = index,y = value, group = variable,
                          col = variable, linetype = variable))+
        geom_point(aes(size = variable))+
        geom_line()+
        theme_bw()+
        scale_x_discrete(labels = c(seq(1,15)))+
        scale_linetype_manual(values=c(1,2,2),name = "",
                              labels = c("Actual Data",
                                         "Parallel Analysis : 50%",
                                         "Parallel Analysis : 99%"))+
        scale_size_manual(values=c(3,1,1),name = "",
                          labels = c("Actual Data",
                                     "Parallel Analysis : 50%",
                                     "Parallel Analysis : 99%"))+
        scale_color_manual(values = c("firebrick",
                                      "chartreuse4",
                                      "chartreuse2"),name = "",
                           labels = c("Actual Data",
                                      "Parallel Analysis : 50%",
                                      "Parallel Analysis : 99%"))+
        labs(list(x = "Number", y = "Eigen Values"))+
        ggtitle("Scree Plots : Parallel Analysis")+
        theme(legend.position = c(0.7,0.77),
              plot.title = element_text(hjust = 0.5))