##load packages
library(psych)
library(GPArotation)
library(plyr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:plyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# data preparation
data <- read.csv("~/Dropbox/Git/stats/allsurveysYT1_Jan2016.csv", header=T)
PWB<-select(data,  PWB_1,  PWB_2,  PWB_3,  PWB_4,  PWB_5,  PWB_6, PWB_7,  PWB_8,  PWB_9)
PWB$PWB_1  <-  7- PWB$PWB_1
PWB$PWB_2  <-  7- PWB$PWB_2
PWB$PWB_3  <-  7- PWB$PWB_3
PWB$PWB_4  <-  7- PWB$PWB_4
PWB$PWB_9  <-  7- PWB$PWB_9
 PWB<- data.frame(apply(PWB,2, as.numeric))
 PWB<-tbl_df(PWB)
 PWB
## Source: local data frame [1,288 x 9]
## 
##    PWB_1 PWB_2 PWB_3 PWB_4 PWB_5 PWB_6 PWB_7 PWB_8 PWB_9
##    (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl)
## 1      4     6     5     3     5     4     3     5     1
## 2      2     2     4     4     6     1     2     4     3
## 3      2     3     3     2     5     4     6     6     2
## 4      5     2     5     5     3     5     4     4     6
## 5      3     3     3     3     4     4     4     4     3
## 6      1     2     2     2     4     4     4     4     2
## 7      3     4     3     4     3     3     4     3     4
## 8      1     2     1     1     6     6     2     6     1
## 9      5     5     5     5     1     5     5     5     5
## 10     2     2     2     2     5     5     5     5     2
## ..   ...   ...   ...   ...   ...   ...   ...   ...   ...
str(PWB)
## Classes 'tbl_df', 'tbl' and 'data.frame':    1288 obs. of  9 variables:
##  $ PWB_1: num  4 2 2 5 3 1 3 1 5 2 ...
##  $ PWB_2: num  6 2 3 2 3 2 4 2 5 2 ...
##  $ PWB_3: num  5 4 3 5 3 2 3 1 5 2 ...
##  $ PWB_4: num  3 4 2 5 3 2 4 1 5 2 ...
##  $ PWB_5: num  5 6 5 3 4 4 3 6 1 5 ...
##  $ PWB_6: num  4 1 4 5 4 4 3 6 5 5 ...
##  $ PWB_7: num  3 2 6 4 4 4 4 2 5 5 ...
##  $ PWB_8: num  5 4 6 4 4 4 3 6 5 5 ...
##  $ PWB_9: num  1 3 2 6 3 2 4 1 5 2 ...
colnames(PWB) <- c("1","2", "3", "4", "5", "6", "7", "8", "9")
 PWB<- PWB[complete.cases(PWB[,]),]

##EFA
##number of factors
##parallal analysis and scree plot
parallel<-fa.parallel(PWB, fm="ml",fa="fa")

## Parallel analysis suggests that the number of factors =  4  and the number of components =  NA
#two factors are greater than one Eigenvalue scree plot says there are two factors. Paralel analysis suggests 4 factors
#eigenvalues (kaiser)
parallel$fa.values
## [1]  2.73344015  0.71182568  0.28802420  0.06142515 -0.02194723 -0.10593730
## [7] -0.18210012 -0.29494755 -0.47494233
#over 1=2, over .7=2
#doign aprincipal components analysis to see how many factors there might be using that method
#Deal with NA doing principle componant analysis
princomp(na.omit(PWB), cor = TRUE)
## Call:
## princomp(x = na.omit(PWB), cor = TRUE)
## 
## Standard deviations:
##    Comp.1    Comp.2    Comp.3    Comp.4    Comp.5    Comp.6    Comp.7 
## 1.8271781 1.2556471 0.9950615 0.8300724 0.8048028 0.7172643 0.6810303 
##    Comp.8    Comp.9 
## 0.6407490 0.6075076 
## 
##  9  variables and  944 observations.
parallel2<-princomp(na.omit(PWB), cor = TRUE)
summary(parallel2)
## Importance of components:
##                           Comp.1    Comp.2    Comp.3     Comp.4    Comp.5
## Standard deviation     1.8271781 1.2556471 0.9950615 0.83007244 0.8048028
## Proportion of Variance 0.3709533 0.1751833 0.1100164 0.07655781 0.0719675
## Cumulative Proportion  0.3709533 0.5461366 0.6561530 0.73271081 0.8046783
##                            Comp.6     Comp.7    Comp.8     Comp.9
## Standard deviation     0.71726434 0.68103033 0.6407490 0.60750759
## Proportion of Variance 0.05716313 0.05153359 0.0456177 0.04100727
## Cumulative Proportion  0.86184143 0.91337502 0.9589927 1.00000000
plot(parallel2)##results show at least two factors

#simple structure
twofactor<-fa(PWB, nfactors=2, rotate="oblimin", fm="ml")
twofactor
## Factor Analysis using method =  ml
## Call: fa(r = PWB, nfactors = 2, rotate = "oblimin", fm = "ml")
## Standardized loadings (pattern matrix) based upon correlation matrix
##     ML1   ML2   h2   u2 com
## 1  0.67 -0.15 0.44 0.56 1.1
## 2  0.51  0.06 0.27 0.73 1.0
## 3  0.77 -0.02 0.59 0.41 1.0
## 4  0.51  0.26 0.38 0.62 1.5
## 5 -0.78  0.00 0.60 0.40 1.0
## 6  0.43  0.30 0.32 0.68 1.8
## 7 -0.03  0.87 0.75 0.25 1.0
## 8  0.04  0.59 0.36 0.64 1.0
## 9  0.46  0.02 0.21 0.79 1.0
## 
##                        ML1  ML2
## SS loadings           2.60 1.32
## Proportion Var        0.29 0.15
## Cumulative Var        0.29 0.44
## Proportion Explained  0.66 0.34
## Cumulative Proportion 0.66 1.00
## 
##  With factor correlations of 
##      ML1  ML2
## ML1 1.00 0.19
## ML2 0.19 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  36  and the objective function was  2.48 with Chi Square of  2325.18
## The degrees of freedom for the model are 19  and the objective function was  0.21 
## 
## The root mean square of the residuals (RMSR) is  0.05 
## The df corrected root mean square of the residuals is  0.07 
## 
## The harmonic number of observations is  944 with the empirical chi square  176.93  with prob <  1.2e-27 
## The total number of observations was  944  with MLE Chi Square =  195.13  with prob <  3.2e-31 
## 
## Tucker Lewis Index of factoring reliability =  0.854
## RMSEA index =  0.099  and the 90 % confidence intervals are  0.087 0.112
## BIC =  64.98
## Fit based upon off diagonal values = 0.97
## Measures of factor score adequacy             
##                                                 ML1  ML2
## Correlation of scores with factors             0.91 0.89
## Multiple R square of scores with factors       0.84 0.79
## Minimum correlation of possible factor scores  0.67 0.59
threefactor<-fa(PWB, nfactors=3, rotate="oblimin", fm="ml")
threefactor
## Factor Analysis using method =  ml
## Call: fa(r = PWB, nfactors = 3, rotate = "oblimin", fm = "ml")
## Standardized loadings (pattern matrix) based upon correlation matrix
##     ML1   ML2   ML3   h2   u2 com
## 1  0.54 -0.17  0.23 0.44 0.56 1.6
## 2  0.13  0.12  0.58 0.44 0.56 1.2
## 3  0.70 -0.07  0.15 0.59 0.41 1.1
## 4  0.27  0.29  0.38 0.42 0.58 2.7
## 5 -0.72  0.05 -0.15 0.61 0.39 1.1
## 6  0.69  0.23 -0.28 0.52 0.48 1.6
## 7 -0.04  0.84  0.05 0.70 0.30 1.0
## 8  0.03  0.61  0.02 0.38 0.62 1.0
## 9  0.13  0.07  0.48 0.32 0.68 1.2
## 
##                        ML1  ML2  ML3
## SS loadings           2.07 1.29 1.06
## Proportion Var        0.23 0.14 0.12
## Cumulative Var        0.23 0.37 0.49
## Proportion Explained  0.47 0.29 0.24
## Cumulative Proportion 0.47 0.76 1.00
## 
##  With factor correlations of 
##      ML1   ML2   ML3
## ML1 1.00  0.25  0.44
## ML2 0.25  1.00 -0.02
## ML3 0.44 -0.02  1.00
## 
## Mean item complexity =  1.4
## Test of the hypothesis that 3 factors are sufficient.
## 
## The degrees of freedom for the null model are  36  and the objective function was  2.48 with Chi Square of  2325.18
## The degrees of freedom for the model are 12  and the objective function was  0.07 
## 
## 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  944 with the empirical chi square  40.04  with prob <  7.1e-05 
## The total number of observations was  944  with MLE Chi Square =  69.45  with prob <  4.1e-10 
## 
## Tucker Lewis Index of factoring reliability =  0.925
## RMSEA index =  0.072  and the 90 % confidence intervals are  0.056 0.088
## BIC =  -12.76
## Fit based upon off diagonal values = 0.99
## Measures of factor score adequacy             
##                                                 ML1  ML2  ML3
## Correlation of scores with factors             0.91 0.88 0.80
## Multiple R square of scores with factors       0.82 0.77 0.64
## Minimum correlation of possible factor scores  0.64 0.53 0.28
fourfactor<-fa(PWB, nfactors=4, rotate="oblimin", fm="ml")
fourfactor
## Factor Analysis using method =  ml
## Call: fa(r = PWB, nfactors = 4, rotate = "oblimin", fm = "ml")
## Standardized loadings (pattern matrix) based upon correlation matrix
##     ML1   ML2   ML4   ML3   h2   u2 com
## 1  0.13 -0.23  0.52  0.30 0.54 0.46 2.2
## 2 -0.01  0.09  0.75 -0.06 0.56 0.44 1.0
## 3  0.84 -0.05 -0.05  0.04 0.68 0.32 1.0
## 4  0.50  0.31  0.15 -0.15 0.45 0.55 2.1
## 5 -0.60  0.04 -0.13 -0.16 0.58 0.42 1.2
## 6  0.11  0.19  0.00  0.70 0.66 0.34 1.2
## 7 -0.02  0.79  0.01  0.08 0.65 0.35 1.0
## 8  0.01  0.60  0.01  0.09 0.40 0.60 1.0
## 9  0.31  0.07  0.32 -0.17 0.29 0.71 2.6
## 
##                        ML1  ML2  ML4  ML3
## SS loadings           1.68 1.22 1.15 0.75
## Proportion Var        0.19 0.14 0.13 0.08
## Cumulative Var        0.19 0.32 0.45 0.53
## Proportion Explained  0.35 0.25 0.24 0.16
## Cumulative Proportion 0.35 0.60 0.84 1.00
## 
##  With factor correlations of 
##      ML1  ML2  ML4  ML3
## ML1 1.00 0.14 0.63 0.48
## ML2 0.14 1.00 0.12 0.20
## ML4 0.63 0.12 1.00 0.19
## ML3 0.48 0.20 0.19 1.00
## 
## Mean item complexity =  1.5
## Test of the hypothesis that 4 factors are sufficient.
## 
## The degrees of freedom for the null model are  36  and the objective function was  2.48 with Chi Square of  2325.18
## The degrees of freedom for the model are 6  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 
## 
## The harmonic number of observations is  944 with the empirical chi square  2.69  with prob <  0.85 
## The total number of observations was  944  with MLE Chi Square =  4.93  with prob <  0.55 
## 
## Tucker Lewis Index of factoring reliability =  1.003
## RMSEA index =  0  and the 90 % confidence intervals are  NA 0.038
## BIC =  -36.17
## Fit based upon off diagonal values = 1
## Measures of factor score adequacy             
##                                                 ML1  ML2  ML4  ML3
## Correlation of scores with factors             0.91 0.86 0.85 0.82
## Multiple R square of scores with factors       0.83 0.74 0.73 0.68
## Minimum correlation of possible factor scores  0.65 0.48 0.45 0.35
#question 1,3,5,6,9 seems to be one factor and all talk about plans or lack of plans
 PWBWO15<-select(PWB,  1,3,4,5,6,9)
 PWBWO15<-tbl_df(PWBWO15)
 PWBWO15
## Source: local data frame [944 x 6]
## 
##        1     3     4     5     6     9
##    (dbl) (dbl) (dbl) (dbl) (dbl) (dbl)
## 1      4     5     3     5     4     1
## 2      2     4     4     6     1     3
## 3      2     3     2     5     4     2
## 4      5     5     5     3     5     6
## 5      3     3     3     4     4     3
## 6      1     2     2     4     4     2
## 7      3     3     4     3     3     4
## 8      1     1     1     6     6     1
## 9      5     5     5     1     5     5
## 10     2     2     2     5     5     2
## ..   ...   ...   ...   ...   ...   ...
twofactorWO15<-fa(PWBWO15, nfactors=1, rotate="oblimin", fm="ml")
twofactorWO15
## Factor Analysis using method =  ml
## Call: fa(r = PWBWO15, nfactors = 1, rotate = "oblimin", fm = "ml")
## Standardized loadings (pattern matrix) based upon correlation matrix
##     ML1   h2   u2 com
## 1  0.60 0.36 0.64   1
## 3  0.78 0.61 0.39   1
## 4  0.55 0.30 0.70   1
## 5 -0.79 0.63 0.37   1
## 6  0.50 0.25 0.75   1
## 9  0.44 0.19 0.81   1
## 
##                 ML1
## SS loadings    2.35
## Proportion Var 0.39
## 
## Mean item complexity =  1
## Test of the hypothesis that 1 factor is sufficient.
## 
## The degrees of freedom for the null model are  15  and the objective function was  1.54 with Chi Square of  1444.14
## The degrees of freedom for the model are 9  and the objective function was  0.05 
## 
## The root mean square of the residuals (RMSR) is  0.04 
## The df corrected root mean square of the residuals is  0.05 
## 
## The harmonic number of observations is  944 with the empirical chi square  46.91  with prob <  4.1e-07 
## The total number of observations was  944  with MLE Chi Square =  45.65  with prob <  7e-07 
## 
## Tucker Lewis Index of factoring reliability =  0.957
## RMSEA index =  0.066  and the 90 % confidence intervals are  0.048 0.085
## BIC =  -16
## Fit based upon off diagonal values = 0.99
## Measures of factor score adequacy             
##                                                 ML1
## Correlation of scores with factors             0.91
## Multiple R square of scores with factors       0.83
## Minimum correlation of possible factor scores  0.66
#CFI, should be slightly higher than the TLI
1-((twofactorWO15$STATISTIC - twofactor$dof)/(twofactor$null.chisq- twofactor$null.dof))
## [1] 0.988359
#question 7,8 seems to be one factor and all talk about being active or wondering aimlessly
 PWB78<-select(PWB,  7,8)
 PWB78<-tbl_df(PWB78)
 PWB78
## Source: local data frame [944 x 2]
## 
##        7     8
##    (dbl) (dbl)
## 1      3     5
## 2      2     4
## 3      6     6
## 4      4     4
## 5      4     4
## 6      4     4
## 7      4     3
## 8      2     6
## 9      5     5
## 10     5     5
## ..   ...   ...
twofactor78<-fa(PWB78, nfactors=1, rotate="oblimin", fm="ml")
twofactor78
## Factor Analysis using method =  ml
## Call: fa(r = PWB78, nfactors = 1, rotate = "oblimin", fm = "ml")
## Standardized loadings (pattern matrix) based upon correlation matrix
##    ML1  h2  u2 com
## 7 0.71 0.5 0.5   1
## 8 0.71 0.5 0.5   1
## 
##                 ML1
## SS loadings    1.01
## Proportion Var 0.50
## 
## Mean item complexity =  1
## Test of the hypothesis that 1 factor is sufficient.
## 
## The degrees of freedom for the null model are  1  and the objective function was  0.29 with Chi Square of  277.04
## The degrees of freedom for the model are -1  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  944 with the empirical chi square  0  with prob <  NA 
## The total number of observations was  944  with MLE Chi Square =  0  with prob <  NA 
## 
## Tucker Lewis Index of factoring reliability =  1.004
## Fit based upon off diagonal values = 1
## Measures of factor score adequacy             
##                                                 ML1
## Correlation of scores with factors             0.82
## Multiple R square of scores with factors       0.67
## Minimum correlation of possible factor scores  0.34
#CFI, should be slightly higher than the TLI
1-((twofactorWO15$STATISTIC - twofactor$dof)/(twofactor$null.chisq- twofactor$null.dof))
## [1] 0.988359
#question 2,8 seems to be one factor 
 PWB29<-select(PWB, 2,8)
 PWB29<-tbl_df(PWB29)
 PWB29
## Source: local data frame [944 x 2]
## 
##        2     8
##    (dbl) (dbl)
## 1      6     5
## 2      2     4
## 3      3     6
## 4      2     4
## 5      3     4
## 6      2     4
## 7      4     3
## 8      2     6
## 9      5     5
## 10     2     5
## ..   ...   ...
twofactor29<-fa(PWB29, nfactors=1, rotate="oblimin", fm="ml")
twofactor29
## Factor Analysis using method =  ml
## Call: fa(r = PWB29, nfactors = 1, rotate = "oblimin", fm = "ml")
## Standardized loadings (pattern matrix) based upon correlation matrix
##    ML1   h2   u2 com
## 2 0.34 0.12 0.88   1
## 8 0.34 0.12 0.88   1
## 
##                 ML1
## SS loadings    0.24
## Proportion Var 0.12
## 
## Mean item complexity =  1
## Test of the hypothesis that 1 factor is sufficient.
## 
## The degrees of freedom for the null model are  1  and the objective function was  0.01 with Chi Square of  13.32
## The degrees of freedom for the model are -1  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  944 with the empirical chi square  0  with prob <  NA 
## The total number of observations was  944  with MLE Chi Square =  0  with prob <  NA 
## 
## Tucker Lewis Index of factoring reliability =  1.081
## Fit based upon off diagonal values = 1
## Measures of factor score adequacy             
##                                                  ML1
## Correlation of scores with factors              0.46
## Multiple R square of scores with factors        0.21
## Minimum correlation of possible factor scores  -0.58
#CFI, should be slightly higher than the TLI
1-((twofactor29$STATISTIC - twofactor$dof)/(twofactor$null.chisq- twofactor$null.dof))
## [1] 1.0083
##reliability
#alpha(PWB[,c(1,2,3,4,5,6,7,8,9)])
#alpha(PWB[,c(5,9,10,14)])
#What are the factors
#Factor 1 is positive emotions
#Factor 2 is negative emotions

#Create dataset for Target rotation
all_surveys<-read.csv("allsurveysT1.csv")
PWB<-select(all_surveys, PWB_1, PWB_3, PWB_5,PWB_6, PWB_8,PWB_7, PWB_4, PWB_2, PWB_9)
PWB<- data.frame(apply(PWB,2, as.numeric))
library(GPArotation)
library(psych)
library(dplyr)

PWB<-tbl_df(PWB)
PWB
## Source: local data frame [757 x 9]
## 
##    PWB_1 PWB_3 PWB_5 PWB_6 PWB_8 PWB_7 PWB_4 PWB_2 PWB_9
##    (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl)
## 1      3     2     4     5     3     4     5     4     1
## 2      3     2     2     5     2     3     5     2     2
## 3      2     2     1     4     3     6     1     1     1
## 4      5     3     3     4     4     5     3     5     3
## 5      5     4     4     3     3     2     4     5     3
## 6      2     1     3     4     4     3     2     3     1
## 7      5     2     1     4     3     3     5     5     4
## 8      1     2     2     4     4     4     6     1     1
## 9      2     2     1     5     5     5     2     2     1
## 10     1     4     2     6     3     6     4     1     1
## ..   ...   ...   ...   ...   ...   ...   ...   ...   ...
str(PWB)
## Classes 'tbl_df', 'tbl' and 'data.frame':    757 obs. of  9 variables:
##  $ PWB_1: num  3 3 2 5 5 2 5 1 2 1 ...
##  $ PWB_3: num  2 2 2 3 4 1 2 2 2 4 ...
##  $ PWB_5: num  4 2 1 3 4 3 1 2 1 2 ...
##  $ PWB_6: num  5 5 4 4 3 4 4 4 5 6 ...
##  $ PWB_8: num  3 2 3 4 3 4 3 4 5 3 ...
##  $ PWB_7: num  4 3 6 5 2 3 3 4 5 6 ...
##  $ PWB_4: num  5 5 1 3 4 2 5 6 2 4 ...
##  $ PWB_2: num  4 2 1 5 5 3 5 1 2 1 ...
##  $ PWB_9: num  1 2 1 3 3 1 4 1 1 1 ...
colnames(PWB) <- c("1","2", "3", "4", "5", "6", "7", "8", "9")
#Target rotation: choose "simple structure" a priori and can be applied to oblique and orthogonal rotation based on 
#what paper says facotrs should be PWB
Targ_key <- make.keys(9,list(f1=1:4,f2=5:7, f3=8:9))
Targ_key <- scrub(Targ_key,isvalue=1)  #fix the 0s, allow the NAs to be estimated
Targ_key <- list(Targ_key)
PWB_cor <- corFiml(PWB) # convert the raw data to correlation matrix uisng FIML
out_targetQ <- fa(PWB_cor,3,rotate="TargetQ",n.obs = 816,Target=Targ_key) #TargetT for orthogonal rotation
out_targetQ[c("loadings", "score.cor", "TLI", "RMSEA")]
## $loadings
## 
## Loadings:
##   MR1    MR2    MR3   
## 1  0.584  0.250  0.232
## 2  0.833              
## 3  0.784              
## 4 -0.692  0.153  0.173
## 5         0.739       
## 6  0.173  0.864       
## 7  0.304 -0.414  0.210
## 8                0.903
## 9  0.159         0.358
## 
##                  MR1   MR2   MR3
## SS loadings    2.284 1.567 1.077
## Proportion Var 0.254 0.174 0.120
## Cumulative Var 0.254 0.428 0.547
## 
## $score.cor
##            [,1]       [,2]       [,3]
## [1,]  1.0000000 -0.2363435  0.3815248
## [2,] -0.2363435  1.0000000 -0.2859293
## [3,]  0.3815248 -0.2859293  1.0000000
## 
## $TLI
## [1] 0.9200451
## 
## $RMSEA
##      RMSEA      lower      upper confidence 
## 0.08110859 0.06388654 0.09855956 0.10000000
out_targetQ
## Factor Analysis using method =  minres
## Call: fa(r = PWB_cor, nfactors = 3, n.obs = 816, rotate = "TargetQ", 
##     Target = Targ_key)
## Standardized loadings (pattern matrix) based upon correlation matrix
##     MR1   MR2   MR3   h2   u2 com
## 1  0.58  0.25  0.23 0.51 0.49 1.7
## 2  0.83 -0.01 -0.04 0.67 0.33 1.0
## 3  0.78 -0.07  0.04 0.67 0.33 1.0
## 4 -0.69  0.15  0.17 0.46 0.54 1.2
## 5 -0.05  0.74  0.03 0.55 0.45 1.0
## 6  0.17  0.86 -0.04 0.73 0.27 1.1
## 7  0.30 -0.41  0.21 0.43 0.57 2.4
## 8 -0.06 -0.08  0.90 0.79 0.21 1.0
## 9  0.16 -0.08  0.36 0.22 0.78 1.5
## 
##                        MR1  MR2  MR3
## SS loadings           2.32 1.59 1.12
## Proportion Var        0.26 0.18 0.12
## Cumulative Var        0.26 0.43 0.56
## Proportion Explained  0.46 0.32 0.22
## Cumulative Proportion 0.46 0.78 1.00
## 
##  With factor correlations of 
##       MR1   MR2   MR3
## MR1  1.00 -0.18  0.44
## MR2 -0.18  1.00 -0.13
## MR3  0.44 -0.13  1.00
## 
## Mean item complexity =  1.3
## Test of the hypothesis that 3 factors are sufficient.
## 
## The degrees of freedom for the null model are  36  and the objective function was  3 with Chi Square of  2435.57
## The degrees of freedom for the model are 12  and the objective function was  0.09 
## 
## 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  816 with the empirical chi square  40.13  with prob <  6.9e-05 
## The total number of observations was  816  with MLE Chi Square =  75.79  with prob <  2.6e-11 
## 
## Tucker Lewis Index of factoring reliability =  0.92
## RMSEA index =  0.081  and the 90 % confidence intervals are  0.064 0.099
## BIC =  -4.66
## Fit based upon off diagonal values = 0.99
## Measures of factor score adequacy             
##                                                 MR1  MR2  MR3
## Correlation of scores with factors             0.93 0.90 0.91
## Multiple R square of scores with factors       0.86 0.82 0.82
## Minimum correlation of possible factor scores  0.72 0.63 0.64
#The best fit to the data seems to be three factors. F1: questions 1,3,5,6. f2: 8,7,4. f3: 2,9

CFI

1-((out_targetQ$STATISTIC - out_targetQ$dof)/(out_targetQ$null.chisq- out_targetQ$null.dof))
## [1] 0.9734151

Droping question 4 because it loads on all of the factors. Much better fit to the data

all_surveys<-read.csv("allsurveysT1.csv")
PWB<-select(all_surveys, PWB_1, PWB_3, PWB_5,PWB_6, PWB_8,PWB_7, PWB_2, PWB_9)
PWB<- data.frame(apply(PWB,2, as.numeric))
PWB<-tbl_df(PWB)
PWB
## Source: local data frame [757 x 8]
## 
##    PWB_1 PWB_3 PWB_5 PWB_6 PWB_8 PWB_7 PWB_2 PWB_9
##    (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl)
## 1      3     2     4     5     3     4     4     1
## 2      3     2     2     5     2     3     2     2
## 3      2     2     1     4     3     6     1     1
## 4      5     3     3     4     4     5     5     3
## 5      5     4     4     3     3     2     5     3
## 6      2     1     3     4     4     3     3     1
## 7      5     2     1     4     3     3     5     4
## 8      1     2     2     4     4     4     1     1
## 9      2     2     1     5     5     5     2     1
## 10     1     4     2     6     3     6     1     1
## ..   ...   ...   ...   ...   ...   ...   ...   ...
colnames(PWB) <- c("1","2", "3", "4", "5", "6", "7", "8")
#Target rotation: choose "simple structure" a priori and can be applied to oblique and orthogonal rotation based on 
#what paper says facotrs should be PWB
Targ_key <- make.keys(8,list(f1=1:4,f2=5:6, f3=7:8))
Targ_key <- scrub(Targ_key,isvalue=1)  #fix the 0s, allow the NAs to be estimated
Targ_key <- list(Targ_key)
PWB_cor <- corFiml(PWB) # convert the raw data to correlation matrix uisng FIML
out_targetQ <- fa(PWB_cor,3,rotate="TargetQ",n.obs = 816,Target=Targ_key) #TargetT for orthogonal rotation
out_targetQ[c("loadings", "score.cor", "TLI", "RMSEA")]
## $loadings
## 
## Loadings:
##   MR1    MR2    MR3   
## 1  0.569  0.251  0.235
## 2  0.808              
## 3  0.788              
## 4 -0.728  0.150  0.174
## 5 -0.113  0.713       
## 6         0.864       
## 7                0.832
## 8  0.153         0.397
## 
##                  MR1   MR2   MR3
## SS loadings    2.175 1.353 0.947
## Proportion Var 0.272 0.169 0.118
## Cumulative Var 0.272 0.441 0.559
## 
## $score.cor
##             [,1]        [,2]       [,3]
## [1,]  1.00000000 -0.08530808  0.3815220
## [2,] -0.08530808  1.00000000 -0.1663979
## [3,]  0.38152199 -0.16639785  1.0000000
## 
## $TLI
## [1] 0.9417259
## 
## $RMSEA
##      RMSEA      lower      upper confidence 
## 0.07150314 0.04917574 0.09499976 0.10000000
out_targetQ
## Factor Analysis using method =  minres
## Call: fa(r = PWB_cor, nfactors = 3, n.obs = 816, rotate = "TargetQ", 
##     Target = Targ_key)
## Standardized loadings (pattern matrix) based upon correlation matrix
##     MR1   MR2   MR3   h2   u2 com
## 1  0.57  0.25  0.23 0.53 0.47 1.7
## 2  0.81  0.02 -0.03 0.63 0.37 1.0
## 3  0.79 -0.05  0.05 0.67 0.33 1.0
## 4 -0.73  0.15  0.17 0.49 0.51 1.2
## 5 -0.11  0.71 -0.01 0.54 0.46 1.1
## 6  0.09  0.86 -0.09 0.75 0.25 1.0
## 7 -0.01 -0.07  0.83 0.69 0.31 1.0
## 8  0.15 -0.07  0.40 0.24 0.76 1.4
## 
##                        MR1  MR2  MR3
## SS loadings           2.20 1.36 0.98
## Proportion Var        0.28 0.17 0.12
## Cumulative Var        0.28 0.45 0.57
## Proportion Explained  0.49 0.30 0.22
## Cumulative Proportion 0.49 0.78 1.00
## 
##  With factor correlations of 
##       MR1   MR2   MR3
## MR1  1.00 -0.09  0.43
## MR2 -0.09  1.00 -0.04
## MR3  0.43 -0.04  1.00
## 
## Mean item complexity =  1.2
## Test of the hypothesis that 3 factors are sufficient.
## 
## The degrees of freedom for the null model are  28  and the objective function was  2.49 with Chi Square of  2018.34
## The degrees of freedom for the model are 7  and the objective function was  0.04 
## 
## 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  816 with the empirical chi square  17.53  with prob <  0.014 
## The total number of observations was  816  with MLE Chi Square =  35.92  with prob <  7.5e-06 
## 
## Tucker Lewis Index of factoring reliability =  0.942
## RMSEA index =  0.072  and the 90 % confidence intervals are  0.049 0.095
## BIC =  -11.01
## Fit based upon off diagonal values = 1
## Measures of factor score adequacy             
##                                                 MR1  MR2  MR3
## Correlation of scores with factors             0.92 0.90 0.86
## Multiple R square of scores with factors       0.85 0.81 0.74
## Minimum correlation of possible factor scores  0.70 0.62 0.48
#The best fit to the data seems to be three factors. F1: questions 1,3,5,6. f2: 8,7,4. f3: 2,9

CFI

1-((out_targetQ$STATISTIC - out_targetQ$dof)/(out_targetQ$null.chisq- out_targetQ$null.dof))
## [1] 0.9854679

Droping question 1 as well because it also loads on all of the factors. Much better fit to the data

all_surveys<-read.csv("allsurveysT1.csv")
PWB<-select(all_surveys, PWB_3, PWB_5,PWB_6, PWB_8,PWB_7, PWB_2, PWB_9)
PWB<- data.frame(apply(PWB,2, as.numeric))
PWB<-tbl_df(PWB)
PWB
## Source: local data frame [757 x 7]
## 
##    PWB_3 PWB_5 PWB_6 PWB_8 PWB_7 PWB_2 PWB_9
##    (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl)
## 1      2     4     5     3     4     4     1
## 2      2     2     5     2     3     2     2
## 3      2     1     4     3     6     1     1
## 4      3     3     4     4     5     5     3
## 5      4     4     3     3     2     5     3
## 6      1     3     4     4     3     3     1
## 7      2     1     4     3     3     5     4
## 8      2     2     4     4     4     1     1
## 9      2     1     5     5     5     2     1
## 10     4     2     6     3     6     1     1
## ..   ...   ...   ...   ...   ...   ...   ...
colnames(PWB) <- c("1","2", "3", "4", "5", "6", "7")
#Target rotation: choose "simple structure" a priori and can be applied to oblique and orthogonal rotation based on 
#what paper says facotrs should be PWB
Targ_key <- make.keys(7,list(f1=1:3,f2=4:5, f3=6:7))
Targ_key <- scrub(Targ_key,isvalue=1)  #fix the 0s, allow the NAs to be estimated
Targ_key <- list(Targ_key)
PWB_cor <- corFiml(PWB) # convert the raw data to correlation matrix uisng FIML
out_targetQ <- fa(PWB_cor,3,rotate="TargetQ",n.obs = 816,Target=Targ_key) #TargetT for orthogonal rotation
out_targetQ[c("loadings", "score.cor", "TLI", "RMSEA")]
## $loadings
## 
## Loadings:
##   MR1    MR2    MR3   
## 1  0.802              
## 2  0.766         0.136
## 3 -0.717  0.126  0.169
## 4         0.759       
## 5         0.811       
## 6                0.698
## 7                0.514
## 
##                  MR1   MR2   MR3
## SS loadings    1.766 1.259 0.802
## Proportion Var 0.252 0.180 0.115
## Cumulative Var 0.252 0.432 0.547
## 
## $score.cor
##            [,1]       [,2]       [,3]
## [1,]  1.0000000 -0.1618757  0.3299602
## [2,] -0.1618757  1.0000000 -0.1663915
## [3,]  0.3299602 -0.1663915  1.0000000
## 
## $TLI
## [1] 0.9511193
## 
## $RMSEA
##      RMSEA      lower      upper confidence 
## 0.06616171 0.03316035 0.10314114 0.10000000
out_targetQ
## Factor Analysis using method =  minres
## Call: fa(r = PWB_cor, nfactors = 3, n.obs = 816, rotate = "TargetQ", 
##     Target = Targ_key)
## Standardized loadings (pattern matrix) based upon correlation matrix
##     MR1   MR2   MR3   h2   u2 com
## 1  0.80  0.08  0.04 0.65 0.35 1.0
## 2  0.77  0.03  0.14 0.69 0.31 1.1
## 3 -0.72  0.13  0.17 0.48 0.52 1.2
## 4 -0.10  0.76  0.00 0.61 0.39 1.0
## 5  0.09  0.81 -0.04 0.65 0.35 1.0
## 6 -0.01 -0.03  0.70 0.49 0.51 1.0
## 7  0.07 -0.03  0.51 0.31 0.69 1.0
## 
##                        MR1  MR2  MR3
## SS loadings           1.78 1.27 0.83
## Proportion Var        0.25 0.18 0.12
## Cumulative Var        0.25 0.44 0.55
## Proportion Explained  0.46 0.33 0.21
## Cumulative Proportion 0.46 0.79 1.00
## 
##  With factor correlations of 
##       MR1   MR2   MR3
## MR1  1.00 -0.18  0.44
## MR2 -0.18  1.00 -0.17
## MR3  0.44 -0.17  1.00
## 
## Mean item complexity =  1.1
## Test of the hypothesis that 3 factors are sufficient.
## 
## The degrees of freedom for the null model are  21  and the objective function was  1.9 with Chi Square of  1545.05
## The degrees of freedom for the model are 3  and the objective function was  0.02 
## 
## The root mean square of the residuals (RMSR) is  0.01 
## The df corrected root mean square of the residuals is  0.03 
## 
## The harmonic number of observations is  816 with the empirical chi square  5.86  with prob <  0.12 
## The total number of observations was  816  with MLE Chi Square =  13.62  with prob <  0.0035 
## 
## Tucker Lewis Index of factoring reliability =  0.951
## RMSEA index =  0.066  and the 90 % confidence intervals are  0.033 0.103
## BIC =  -6.5
## Fit based upon off diagonal values = 1
## Measures of factor score adequacy             
##                                                 MR1  MR2  MR3
## Correlation of scores with factors             0.91 0.88 0.79
## Multiple R square of scores with factors       0.83 0.78 0.63
## Minimum correlation of possible factor scores  0.67 0.55 0.26
#The best fit to the data seems to be three factors. F1: questions 1,3,5,6. f2: 8,7,4. f3: 2,9

CFI

1-((out_targetQ$STATISTIC - out_targetQ$dof)/(out_targetQ$null.chisq- out_targetQ$null.dof))
## [1] 0.9930345

Adding a 4th factor with questions 1 and 4

all_surveys<-read.csv("allsurveysT1.csv")
PWB<-select(all_surveys, PWB_3, PWB_5,PWB_6, PWB_8,PWB_7, PWB_2, PWB_9, PWB_1, PWB_4)
PWB<- data.frame(apply(PWB,2, as.numeric))
PWB<-tbl_df(PWB)
PWB
## Source: local data frame [757 x 9]
## 
##    PWB_3 PWB_5 PWB_6 PWB_8 PWB_7 PWB_2 PWB_9 PWB_1 PWB_4
##    (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl)
## 1      2     4     5     3     4     4     1     3     5
## 2      2     2     5     2     3     2     2     3     5
## 3      2     1     4     3     6     1     1     2     1
## 4      3     3     4     4     5     5     3     5     3
## 5      4     4     3     3     2     5     3     5     4
## 6      1     3     4     4     3     3     1     2     2
## 7      2     1     4     3     3     5     4     5     5
## 8      2     2     4     4     4     1     1     1     6
## 9      2     1     5     5     5     2     1     2     2
## 10     4     2     6     3     6     1     1     1     4
## ..   ...   ...   ...   ...   ...   ...   ...   ...   ...
colnames(PWB) <- c("1","2", "3", "4", "5", "6", "7", "8", "9")
#Target rotation: choose "simple structure" a priori and can be applied to oblique and orthogonal rotation based on 
#what paper says facotrs should be PWB
Targ_key <- make.keys(9,list(f1=1:3,f2=4:5, f3=6:7, f4=8:9))
Targ_key <- scrub(Targ_key,isvalue=1)  #fix the 0s, allow the NAs to be estimated
Targ_key <- list(Targ_key)
PWB_cor <- corFiml(PWB) # convert the raw data to correlation matrix uisng FIML
out_targetQ <- fa(PWB_cor,4,rotate="TargetQ",n.obs = 816,Target=Targ_key) #TargetT for orthogonal rotation
out_targetQ[c("loadings", "score.cor", "TLI", "RMSEA")]
## $loadings
## 
## Loadings:
##   MR1    MR2    MR3    MR4   
## 1  0.906  0.144         0.164
## 2  0.724         0.115       
## 3 -0.676  0.247  0.152  0.273
## 4 -0.121  0.692              
## 5         0.814        -0.102
## 6 -0.163         0.890       
## 7                0.443       
## 8  0.410  0.155  0.339 -0.361
## 9  0.375 -0.219  0.262  0.331
## 
##                  MR1   MR2   MR3   MR4
## SS loadings    2.164 1.298 1.212 0.360
## Proportion Var 0.240 0.144 0.135 0.040
## Cumulative Var 0.240 0.385 0.519 0.559
## 
## $score.cor
##            [,1]       [,2]       [,3]
## [1,]  1.0000000 -0.1829474  0.4307783
## [2,] -0.1829474  1.0000000 -0.1663943
## [3,]  0.4307783 -0.1663943  1.0000000
## 
## $TLI
## [1] 0.976284
## 
## $RMSEA
##      RMSEA      lower      upper confidence 
## 0.04425545 0.01720263 0.07158094 0.10000000
out_targetQ
## Factor Analysis using method =  minres
## Call: fa(r = PWB_cor, nfactors = 4, n.obs = 816, rotate = "TargetQ", 
##     Target = Targ_key)
## Standardized loadings (pattern matrix) based upon correlation matrix
##     MR1   MR2   MR3   MR4   h2   u2 com
## 1  0.91  0.14 -0.02  0.16 0.75 0.25 1.1
## 2  0.72 -0.01  0.12 -0.03 0.64 0.36 1.1
## 3 -0.68  0.25  0.15  0.27 0.56 0.44 1.7
## 4 -0.12  0.69  0.00 -0.08 0.56 0.44 1.1
## 5  0.09  0.81 -0.06 -0.10 0.72 0.28 1.1
## 6 -0.16 -0.05  0.89 -0.04 0.67 0.33 1.1
## 7  0.07 -0.04  0.44  0.01 0.24 0.76 1.1
## 8  0.41  0.15  0.34 -0.36 0.61 0.39 3.2
## 9  0.37 -0.22  0.26  0.33 0.53 0.47 3.5
## 
##                        MR1  MR2  MR3  MR4
## SS loadings           2.23 1.37 1.26 0.42
## Proportion Var        0.25 0.15 0.14 0.05
## Cumulative Var        0.25 0.40 0.54 0.59
## Proportion Explained  0.42 0.26 0.24 0.08
## Cumulative Proportion 0.42 0.68 0.92 1.00
## 
##  With factor correlations of 
##       MR1   MR2   MR3   MR4
## MR1  1.00 -0.16  0.53 -0.13
## MR2 -0.16  1.00 -0.14 -0.29
## MR3  0.53 -0.14  1.00  0.04
## MR4 -0.13 -0.29  0.04  1.00
## 
## Mean item complexity =  1.7
## Test of the hypothesis that 4 factors are sufficient.
## 
## The degrees of freedom for the null model are  36  and the objective function was  3 with Chi Square of  2435.57
## The degrees of freedom for the model are 6  and the objective function was  0.02 
## 
## The root mean square of the residuals (RMSR) is  0.01 
## The df corrected root mean square of the residuals is  0.03 
## 
## The harmonic number of observations is  816 with the empirical chi square  8.07  with prob <  0.23 
## The total number of observations was  816  with MLE Chi Square =  15.45  with prob <  0.017 
## 
## Tucker Lewis Index of factoring reliability =  0.976
## RMSEA index =  0.044  and the 90 % confidence intervals are  0.017 0.072
## BIC =  -24.77
## Fit based upon off diagonal values = 1
## Measures of factor score adequacy             
##                                                 MR1  MR2  MR3  MR4
## Correlation of scores with factors             0.94 0.89 0.88 0.71
## Multiple R square of scores with factors       0.87 0.80 0.78 0.51
## Minimum correlation of possible factor scores  0.75 0.60 0.56 0.01
#The best fit to the data seems to be three factors. F1: questions 1,3,5,6. f2: 8,7,4. f3: 2,9

CFI

1-((out_targetQ$STATISTIC - out_targetQ$dof)/(out_targetQ$null.chisq- out_targetQ$null.dof))
## [1] 0.9960605