##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("~/Psychometric_study_data/allsurveysT1.csv")
 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 [757 x 9]
## 
##    PWB_1 PWB_2 PWB_3 PWB_4 PWB_5 PWB_6 PWB_7 PWB_8 PWB_9
## 1      4     3     5     2     4     5     4     3     6
## 2      4     5     5     2     2     5     3     2     5
## 3      5     6     5     6     1     4     6     3     6
## 4      2     2     4     4     3     4     5     4     4
## 5      2     2     3     3     4     3     2     3     4
## 6      5     4     6     5     3     4     3     4     6
## 7      2     2     5     2     1     4     3     3     3
## 8      6     6     5     1     2     4     4     4     6
## 9      5     5     5     5     1     5     5     5     6
## 10     6     6     3     3     2     6     6     3     6
## ..   ...   ...   ...   ...   ...   ...   ...   ...   ...
str(PWB)
## Classes 'tbl_df', 'tbl' and 'data.frame':    757 obs. of  9 variables:
##  $ PWB_1: num  4 4 5 2 2 5 2 6 5 6 ...
##  $ PWB_2: num  3 5 6 2 2 4 2 6 5 6 ...
##  $ PWB_3: num  5 5 5 4 3 6 5 5 5 3 ...
##  $ PWB_4: num  2 2 6 4 3 5 2 1 5 3 ...
##  $ 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_7: num  4 3 6 5 2 3 3 4 5 6 ...
##  $ PWB_8: num  3 2 3 4 3 4 3 4 5 3 ...
##  $ PWB_9: num  6 5 6 4 4 6 3 6 6 6 ...
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 =  3  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.764589905  0.925824683  0.367705915  0.031000693 -0.006112395
## [6] -0.150861796 -0.275610741 -0.317569437 -0.606030196
#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.8258502 1.3343875 1.0554192 0.8256147 0.7860937 0.6451339 0.6328747 
##    Comp.8    Comp.9 
## 0.5906555 0.5537025 
## 
##  9  variables and  471 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.8258502 1.3343875 1.0554192 0.82561467 0.78609375
## Proportion of Variance 0.3704143 0.1978433 0.1237677 0.07573773 0.06866038
## Cumulative Proportion  0.3704143 0.5682577 0.6920254 0.76776312 0.83642349
##                           Comp.6     Comp.7     Comp.8     Comp.9
## Standard deviation     0.6451339 0.63287472 0.59065550 0.55370250
## Proportion of Variance 0.0462442 0.04450338 0.03876377 0.03406516
## Cumulative Proportion  0.8826677 0.92717107 0.96593484 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.68 -0.25 0.48 0.52 1.3
## 2  0.40  0.13 0.18 0.82 1.2
## 3  0.80 -0.02 0.64 0.36 1.0
## 4  0.46  0.41 0.42 0.58 2.0
## 5 -0.81 -0.04 0.67 0.33 1.0
## 6  0.60  0.11 0.39 0.61 1.1
## 7 -0.07  0.87 0.74 0.26 1.0
## 8  0.10  0.72 0.54 0.46 1.0
## 9  0.36  0.10 0.15 0.85 1.1
## 
##                        ML1  ML2
## SS loadings           2.66 1.55
## Proportion Var        0.30 0.17
## Cumulative Var        0.30 0.47
## Proportion Explained  0.63 0.37
## Cumulative Proportion 0.63 1.00
## 
##  With factor correlations of 
##      ML1  ML2
## ML1 1.00 0.12
## ML2 0.12 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  3 with Chi Square of  1399.69
## The degrees of freedom for the model are 19  and the objective function was  0.32 
## 
## The root mean square of the residuals (RMSR) is  0.06 
## The df corrected root mean square of the residuals is  0.09 
## 
## The harmonic number of observations is  471 with the empirical chi square  138.64  with prob <  3.3e-20 
## The total number of observations was  471  with MLE Chi Square =  147.52  with prob <  6.6e-22 
## 
## Tucker Lewis Index of factoring reliability =  0.821
## RMSEA index =  0.121  and the 90 % confidence intervals are  0.102 0.138
## BIC =  30.58
## Fit based upon off diagonal values = 0.96
## Measures of factor score adequacy             
##                                                 ML1  ML2
## Correlation of scores with factors             0.93 0.90
## Multiple R square of scores with factors       0.86 0.82
## Minimum correlation of possible factor scores  0.72 0.63
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
##     ML2   ML3   ML1   h2    u2 com
## 1  0.58 -0.28  0.21 0.50 0.500 1.7
## 2 -0.01  0.02  1.00 1.00 0.005 1.0
## 3  0.83 -0.02 -0.03 0.67 0.331 1.0
## 4  0.38  0.38  0.19 0.43 0.572 2.5
## 5 -0.80 -0.03 -0.04 0.67 0.331 1.0
## 6  0.68  0.13 -0.13 0.45 0.553 1.1
## 7 -0.08  0.85  0.05 0.72 0.278 1.0
## 8  0.12  0.73 -0.02 0.56 0.442 1.1
## 9  0.22  0.06  0.30 0.20 0.803 1.9
## 
##                        ML2  ML3  ML1
## SS loadings           2.41 1.52 1.26
## Proportion Var        0.27 0.17 0.14
## Cumulative Var        0.27 0.44 0.58
## Proportion Explained  0.46 0.29 0.24
## Cumulative Proportion 0.46 0.76 1.00
## 
##  With factor correlations of 
##      ML2  ML3  ML1
## ML2 1.00 0.11 0.35
## ML3 0.11 1.00 0.12
## ML1 0.35 0.12 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  3 with Chi Square of  1399.69
## 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  471 with the empirical chi square  24.05  with prob <  0.02 
## The total number of observations was  471  with MLE Chi Square =  42.44  with prob <  2.8e-05 
## 
## Tucker Lewis Index of factoring reliability =  0.933
## RMSEA index =  0.074  and the 90 % confidence intervals are  0.05 0.098
## BIC =  -31.42
## Fit based upon off diagonal values = 0.99
## Measures of factor score adequacy             
##                                                 ML2  ML3  ML1
## Correlation of scores with factors             0.93 0.90 1.00
## Multiple R square of scores with factors       0.86 0.81 0.99
## Minimum correlation of possible factor scores  0.72 0.62 0.99
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   ML3   ML4   h2   u2 com
## 1  0.12 -0.20  0.37  0.51 0.61 0.39 2.3
## 2 -0.01  0.03  0.83 -0.01 0.68 0.32 1.0
## 3  0.85 -0.10 -0.03  0.08 0.75 0.25 1.0
## 4  0.57  0.29  0.20 -0.22 0.53 0.47 2.1
## 5 -0.56 -0.03 -0.12 -0.26 0.64 0.36 1.5
## 6  0.26  0.24 -0.09  0.57 0.56 0.44 1.9
## 7 -0.08  0.86  0.04 -0.01 0.72 0.28 1.0
## 8  0.07  0.73 -0.02  0.06 0.56 0.44 1.0
## 9  0.13  0.04  0.40  0.02 0.24 0.76 1.2
## 
##                        ML1  ML2  ML3  ML4
## SS loadings           1.75 1.49 1.16 0.89
## Proportion Var        0.19 0.17 0.13 0.10
## Cumulative Var        0.19 0.36 0.49 0.59
## Proportion Explained  0.33 0.28 0.22 0.17
## Cumulative Proportion 0.33 0.61 0.83 1.00
## 
##  With factor correlations of 
##      ML1   ML2  ML3   ML4
## ML1 1.00  0.21 0.43  0.55
## ML2 0.21  1.00 0.14 -0.12
## ML3 0.43  0.14 1.00  0.17
## ML4 0.55 -0.12 0.17  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  3 with Chi Square of  1399.69
## 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  471 with the empirical chi square  4.69  with prob <  0.58 
## The total number of observations was  471  with MLE Chi Square =  8.85  with prob <  0.18 
## 
## Tucker Lewis Index of factoring reliability =  0.987
## RMSEA index =  0.032  and the 90 % confidence intervals are  NA 0.073
## BIC =  -28.08
## Fit based upon off diagonal values = 1
## Measures of factor score adequacy             
##                                                 ML1  ML2  ML3  ML4
## Correlation of scores with factors             0.92 0.90 0.87 0.83
## Multiple R square of scores with factors       0.85 0.81 0.75 0.69
## Minimum correlation of possible factor scores  0.69 0.63 0.51 0.38
#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 [471 x 6]
## 
##    1 3 4 5 6 9
## 1  4 5 2 4 5 6
## 2  4 5 2 2 5 5
## 3  5 5 6 1 4 6
## 4  2 4 4 3 4 4
## 5  2 3 3 4 3 4
## 6  5 6 5 3 4 6
## 7  2 5 2 1 4 3
## 8  6 5 1 2 4 6
## 9  5 5 5 1 5 6
## 10 6 3 3 2 6 6
## .. . . . . . .
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.61 0.37 0.63   1
## 3  0.81 0.66 0.34   1
## 4  0.49 0.24 0.76   1
## 5 -0.83 0.69 0.31   1
## 6  0.63 0.40 0.60   1
## 9  0.35 0.12 0.88   1
## 
##                 ML1
## SS loadings    2.48
## Proportion Var 0.41
## 
## 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.78 with Chi Square of  833.12
## The degrees of freedom for the model are 9  and the objective function was  0.09 
## 
## 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  471 with the empirical chi square  36.77  with prob <  2.9e-05 
## The total number of observations was  471  with MLE Chi Square =  40.05  with prob <  7.4e-06 
## 
## Tucker Lewis Index of factoring reliability =  0.937
## RMSEA index =  0.086  and the 90 % confidence intervals are  0.06 0.113
## BIC =  -15.34
## Fit based upon off diagonal values = 0.98
## Measures of factor score adequacy             
##                                                 ML1
## Correlation of scores with factors             0.92
## Multiple R square of scores with factors       0.85
## Minimum correlation of possible factor scores  0.71
#CFI, should be slightly higher than the TLI
1-((twofactorWO15$STATISTIC - twofactor$dof)/(twofactor$null.chisq- twofactor$null.dof))
## [1] 0.984563
#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 [471 x 2]
## 
##    7 8
## 1  4 3
## 2  3 2
## 3  6 3
## 4  5 4
## 5  2 3
## 6  3 4
## 7  3 3
## 8  4 4
## 9  5 5
## 10 6 3
## .. . .
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.79 0.62 0.38   1
## 8 0.79 0.62 0.38   1
## 
##                 ML1
## SS loadings    1.23
## Proportion Var 0.62
## 
## 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.48 with Chi Square of  224.1
## 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  471 with the empirical chi square  0  with prob <  NA 
## The total number of observations was  471  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.87
## Multiple R square of scores with factors       0.76
## Minimum correlation of possible factor scores  0.53
#CFI, should be slightly higher than the TLI
1-((twofactorWO15$STATISTIC - twofactor$dof)/(twofactor$null.chisq- twofactor$null.dof))
## [1] 0.984563
#question 2,8 seems to be one factor 
 PWB29<-select(PWB, 2,8)
 PWB29<-tbl_df(PWB29)
 PWB29
## Source: local data frame [471 x 2]
## 
##    2 8
## 1  3 3
## 2  5 2
## 3  6 3
## 4  2 4
## 5  2 3
## 6  4 4
## 7  2 3
## 8  6 4
## 9  5 5
## 10 6 3
## .. . .
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  6.65
## 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  471 with the empirical chi square  0  with prob <  NA 
## The total number of observations was  471  with MLE Chi Square =  0  with prob <  NA 
## 
## Tucker Lewis Index of factoring reliability =  1.177
## 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.013933
##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
## 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
## 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
## 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
## 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