FILEPATH='~/Downloads/School Survey(1-14).xls'
SS <- read_excel(FILEPATH)
names(SS) <- c( "ID", "Start_time","Completion_time","Email","Name",
               "Year_Level","Email_2","Postal_Code","Accessibility",
               "Achievement_Results","Teaching_Experience","Teacher_Friendly", 
               "Facilities_Maintanance","Teacher_Additional", "Student_Behaviour", 
               "Facilities","Personal_Effort","Parental_Influence",
               "Other_Engagement", "Peer_friend","School_Experience","Recommendation"
               )
SS2 = subset(SS, 
             select = -c(ID, Name, Email, Postal_Code, Year_Level,Email_2, Start_time, Completion_time))

SS2.1 = subset(SS, select = -c(Start_time, Completion_time, Email, Name) ) 
summary(SS2.1)
##        ID        Year_Level          Email_2          Postal_Code       
##  Min.   : 1.0   Length:63          Length:63          Length:63         
##  1st Qu.:16.5   Class :character   Class :character   Class :character  
##  Median :32.0   Mode  :character   Mode  :character   Mode  :character  
##  Mean   :32.0                                                           
##  3rd Qu.:47.5                                                           
##  Max.   :63.0                                                           
##  Accessibility    Achievement_Results Teaching_Experience Teacher_Friendly
##  Min.   : 1.000   Min.   : 1.000      Min.   : 1.00       Min.   : 1.000  
##  1st Qu.: 7.000   1st Qu.: 6.000      1st Qu.: 7.00       1st Qu.: 7.000  
##  Median : 8.000   Median : 7.000      Median : 7.00       Median : 8.000  
##  Mean   : 7.968   Mean   : 7.032      Mean   : 7.27       Mean   : 8.032  
##  3rd Qu.:10.000   3rd Qu.: 8.000      3rd Qu.: 8.00       3rd Qu.:10.000  
##  Max.   :10.000   Max.   :10.000      Max.   :10.00       Max.   :10.000  
##  Facilities_Maintanance Teacher_Additional Student_Behaviour   Facilities    
##  Min.   : 1.000         Min.   : 1.000     Min.   : 1.000    Min.   : 1.000  
##  1st Qu.: 5.000         1st Qu.: 7.000     1st Qu.: 5.500    1st Qu.: 5.000  
##  Median : 6.000         Median : 8.000     Median : 6.000    Median : 7.000  
##  Mean   : 6.365         Mean   : 8.048     Mean   : 6.444    Mean   : 6.587  
##  3rd Qu.: 8.000         3rd Qu.: 9.500     3rd Qu.: 8.000    3rd Qu.: 8.000  
##  Max.   :10.000         Max.   :10.000     Max.   :10.000    Max.   :10.000  
##  Personal_Effort  Parental_Influence Other_Engagement  Peer_friend   
##  Min.   : 1.000   Min.   : 1.000     Min.   : 2.000   Min.   : 3.00  
##  1st Qu.: 7.000   1st Qu.: 6.000     1st Qu.: 6.000   1st Qu.: 7.00  
##  Median : 8.000   Median : 7.000     Median : 7.000   Median : 9.00  
##  Mean   : 7.952   Mean   : 7.159     Mean   : 6.746   Mean   : 8.46  
##  3rd Qu.: 9.000   3rd Qu.: 9.000     3rd Qu.: 8.000   3rd Qu.:10.00  
##  Max.   :10.000   Max.   :10.000     Max.   :10.000   Max.   :10.00  
##  School_Experience Recommendation  
##  Min.   : 1.000    Min.   : 1.000  
##  1st Qu.: 7.000    1st Qu.: 6.500  
##  Median : 8.000    Median : 8.000  
##  Mean   : 7.746    Mean   : 7.587  
##  3rd Qu.: 9.000    3rd Qu.:10.000  
##  Max.   :10.000    Max.   :10.000
SS2$School_Experience=as.numeric(SS2$School_Experience) 
SS2$Recommendation=as.numeric(SS2$Recommendation)
cormatrix <- cor(SS2)
ggcorrplot(cormatrix)

KMO(cormatrix)
## Kaiser-Meyer-Olkin factor adequacy
## Call: KMO(r = cormatrix)
## Overall MSA =  0.8
## MSA for each item = 
##          Accessibility    Achievement_Results    Teaching_Experience 
##                   0.76                   0.86                   0.87 
##       Teacher_Friendly Facilities_Maintanance     Teacher_Additional 
##                   0.94                   0.81                   0.80 
##      Student_Behaviour             Facilities        Personal_Effort 
##                   0.79                   0.85                   0.77 
##     Parental_Influence       Other_Engagement            Peer_friend 
##                   0.48                   0.71                   0.79 
##      School_Experience         Recommendation 
##                   0.78                   0.78
parallel <- fa.parallel(cormatrix, fm = 'minres', fa = 'fa')

## Parallel analysis suggests that the number of factors =  3  and the number of components =  NA
nfac <- fa(SS2, nfactors = 5, rotate = "varimax",fm="minres") 
nfac
## Factor Analysis using method =  minres
## Call: fa(r = SS2, nfactors = 5, rotate = "varimax", fm = "minres")
## Standardized loadings (pattern matrix) based upon correlation matrix
##                          MR1  MR3  MR2  MR5   MR4   h2    u2 com
## Accessibility           0.74 0.04 0.16 0.12 -0.17 0.62 0.385 1.3
## Achievement_Results     0.62 0.31 0.35 0.12  0.03 0.63 0.373 2.2
## Teaching_Experience     0.38 0.60 0.16 0.21  0.09 0.59 0.414 2.2
## Teacher_Friendly        0.48 0.36 0.33 0.27  0.00 0.55 0.450 3.4
## Facilities_Maintanance  0.12 0.79 0.20 0.06  0.00 0.68 0.315 1.2
## Teacher_Additional      0.42 0.39 0.04 0.30 -0.03 0.43 0.575 2.8
## Student_Behaviour       0.39 0.26 0.35 0.60 -0.09 0.71 0.291 2.9
## Facilities              0.15 0.74 0.22 0.10 -0.01 0.63 0.366 1.3
## Personal_Effort         0.81 0.28 0.00 0.23  0.29 0.86 0.139 1.7
## Parental_Influence     -0.01 0.00 0.15 0.07  0.82 0.70 0.296 1.1
## Other_Engagement        0.19 0.18 0.18 0.93  0.20 1.00 0.002 1.3
## Peer_friend             0.19 0.03 0.45 0.50  0.00 0.49 0.506 2.3
## School_Experience       0.23 0.32 0.83 0.19  0.14 0.91 0.093 1.6
## Recommendation          0.11 0.33 0.77 0.29  0.20 0.84 0.157 1.9
## 
##                        MR1  MR3  MR2  MR5  MR4
## SS loadings           2.47 2.32 2.04 1.90 0.91
## Proportion Var        0.18 0.17 0.15 0.14 0.06
## Cumulative Var        0.18 0.34 0.49 0.62 0.69
## Proportion Explained  0.26 0.24 0.21 0.20 0.09
## Cumulative Proportion 0.26 0.50 0.71 0.91 1.00
## 
## Mean item complexity =  2
## Test of the hypothesis that 5 factors are sufficient.
## 
## The degrees of freedom for the null model are  91  and the objective function was  9.07 with Chi Square of  512.63
## The degrees of freedom for the model are 31  and the objective function was  0.78 
## 
## The root mean square of the residuals (RMSR) is  0.03 
## The df corrected root mean square of the residuals is  0.05 
## 
## The harmonic number of observations is  63 with the empirical chi square  10.28  with prob <  1 
## The total number of observations was  63  with Likelihood Chi Square =  41.22  with prob <  0.1 
## 
## Tucker Lewis Index of factoring reliability =  0.923
## RMSEA index =  0.071  and the 90 % confidence intervals are  0 0.127
## BIC =  -87.22
## Fit based upon off diagonal values = 1
print(nfac$loadings, cutoff = 0.4) 
## 
## Loadings:
##                        MR1    MR3    MR2    MR5    MR4   
## Accessibility           0.737                            
## Achievement_Results     0.624                            
## Teaching_Experience            0.601                     
## Teacher_Friendly        0.483                            
## Facilities_Maintanance         0.790                     
## Teacher_Additional      0.421                            
## Student_Behaviour                            0.600       
## Facilities                     0.745                     
## Personal_Effort         0.805                            
## Parental_Influence                                  0.822
## Other_Engagement                             0.925       
## Peer_friend                           0.449  0.504       
## School_Experience                     0.834              
## Recommendation                        0.770              
## 
##                  MR1   MR3   MR2   MR5   MR4
## SS loadings    2.469 2.320 2.041 1.902 0.907
## Proportion Var 0.176 0.166 0.146 0.136 0.065
## Cumulative Var 0.176 0.342 0.488 0.624 0.688
SS3 = subset(SS2, select = -c(Teaching_Experience, Teacher_Additional, Recommendation, Peer_friend, Student_Behaviour, Teacher_Friendly) ) 

nfac <- fa(SS3, nfactors = 5, rotate = "varimax",fm="minres") 
nfac
## Factor Analysis using method =  minres
## Call: fa(r = SS3, nfactors = 5, rotate = "varimax", fm = "minres")
## Standardized loadings (pattern matrix) based upon correlation matrix
##                         MR1   MR2   MR3   MR4  MR5   h2    u2 com
## Accessibility          0.79  0.05 -0.22  0.22 0.11 0.74 0.262 1.4
## Achievement_Results    0.62  0.31  0.07  0.34 0.05 0.60 0.403 2.1
## Facilities_Maintanance 0.15  0.82  0.03  0.13 0.03 0.72 0.283 1.1
## Facilities             0.17  0.76  0.01  0.17 0.16 0.67 0.335 1.3
## Personal_Effort        0.86  0.26  0.37 -0.11 0.19 0.99 0.015 1.7
## Parental_Influence     0.00 -0.01  0.65  0.10 0.09 0.44 0.560 1.1
## Other_Engagement       0.30  0.22  0.30  0.19 0.46 0.47 0.526 3.5
## School_Experience      0.25  0.39  0.26  0.70 0.17 0.81 0.192 2.3
## 
##                        MR1  MR2  MR3  MR4  MR5
## SS loadings           1.94 1.63 0.77 0.76 0.33
## Proportion Var        0.24 0.20 0.10 0.10 0.04
## Cumulative Var        0.24 0.45 0.54 0.64 0.68
## Proportion Explained  0.36 0.30 0.14 0.14 0.06
## Cumulative Proportion 0.36 0.66 0.80 0.94 1.00
## 
## Mean item complexity =  1.8
## Test of the hypothesis that 5 factors are sufficient.
## 
## The degrees of freedom for the null model are  28  and the objective function was  3.11 with Chi Square of  181.66
## The degrees of freedom for the model are -2  and the objective function was  0 
## 
## The root mean square of the residuals (RMSR) is  0 
## The df corrected root mean square of the residuals is  NA 
## 
## The harmonic number of observations is  63 with the empirical chi square  0  with prob <  NA 
## The total number of observations was  63  with Likelihood Chi Square =  0  with prob <  NA 
## 
## Tucker Lewis Index of factoring reliability =  1.195
## Fit based upon off diagonal values = 1
## Measures of factor score adequacy             
##                                                    MR1  MR2  MR3  MR4   MR5
## Correlation of (regression) scores with factors   0.95 0.88 0.82 0.87  0.50
## Multiple R square of scores with factors          0.90 0.78 0.67 0.75  0.25
## Minimum correlation of possible factor scores     0.81 0.56 0.35 0.50 -0.51
print(nfac$loadings, cutoff = 0.4) 
## 
## Loadings:
##                        MR1    MR2    MR3    MR4    MR5   
## Accessibility           0.790                            
## Achievement_Results     0.616                            
## Facilities_Maintanance         0.822                     
## Facilities                     0.764                     
## Personal_Effort         0.857                            
## Parental_Influence                    0.649              
## Other_Engagement                                    0.460
## School_Experience                            0.704       
## 
##                  MR1   MR2   MR3   MR4   MR5
## SS loadings    1.936 1.626 0.771 0.764 0.326
## Proportion Var 0.242 0.203 0.096 0.095 0.041
## Cumulative Var 0.242 0.445 0.542 0.637 0.678
MRw=nfac$weights 
MR1w=nfac$weights[,1] 
MR2w=nfac$weights[,2] 
MR3w=nfac$weights[,3] 
MR4w=nfac$weights[,4]
MR5w=nfac$weights[,5]


dim(SS3) 
## [1] 63  8
dim(MRw) 
## [1] 8 5
SS4= data.matrix(SS3) %*% data.matrix(MRw) 
dim(SS4) 
## [1] 63  5
SS5=as.data.frame(SS4) 
SS5$Satis=SS$Recommendation
reg <- lm( SS5$Satis ~ SS5$MR1 + SS5$MR2 + SS5$MR3 + SS5$MR4 + SS5$MR5, data = SS5) 

coefficients <- as.data.frame(reg$coefficients) 
colnames(coefficients) <- 'coefficients' 

summary(reg)
## 
## Call:
## lm(formula = SS5$Satis ~ SS5$MR1 + SS5$MR2 + SS5$MR3 + SS5$MR4 + 
##     SS5$MR5, data = SS5)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.63590 -0.63813 -0.02759  0.73685  2.97328 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1.72314    0.86035  -2.003   0.0500 *  
## SS5$MR1      0.12934    0.09053   1.429   0.1585    
## SS5$MR2      0.45761    0.07856   5.825 2.78e-07 ***
## SS5$MR3      0.45393    0.09640   4.709 1.64e-05 ***
## SS5$MR4      0.93928    0.09568   9.816 7.38e-14 ***
## SS5$MR5      0.36233    0.18046   2.008   0.0494 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.189 on 57 degrees of freedom
## Multiple R-squared:  0.768,  Adjusted R-squared:  0.7476 
## F-statistic: 37.73 on 5 and 57 DF,  p-value: < 2.2e-16