library(lavaan)
## This is lavaan 0.5-18
## lavaan is BETA software! Please report any bugs.
library(semPlot)
library(dplyr)
##
## Attaching package: 'dplyr'
##
## The following objects are masked from 'package:stats':
##
## filter, lag
##
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(GPArotation)
library(psych)
library(car)
##
## Attaching package: 'car'
##
## The following object is masked from 'package:psych':
##
## logit
library(ggplot2)
##
## Attaching package: 'ggplot2'
##
## The following object is masked from 'package:psych':
##
## %+%
library(GGally)
##
## Attaching package: 'GGally'
##
## The following object is masked from 'package:dplyr':
##
## nasa
data <- read.csv("~/Psychometric_study_data/allsurveysT1.csv")
data<-tbl_df(data)
LET<-select(data, LET_1, LET_2, LET_3, LET_4, LET_5, LET_6)
LET$LET_1 <- 6- LET$LET_1
LET$LET_3 <- 6- LET$LET_3
LET$LET_5 <- 6- LET$LET_5
LET<-tbl_df(LET)
LET
## Source: local data frame [757 x 6]
##
## LET_1 LET_2 LET_3 LET_4 LET_5 LET_6
## 1 4 4 4 5 5 5
## 2 3 3 4 4 4 5
## 3 3 4 3 4 4 5
## 4 1 4 4 4 4 4
## 5 3 2 3 4 2 4
## 6 5 5 5 5 5 4
## 7 3 4 2 3 3 5
## 8 3 4 3 4 4 5
## 9 5 4 5 5 5 5
## 10 3 3 5 5 5 5
## .. ... ... ... ... ... ...
#ggpairs(LET, columns = 1:15, title="Big 5 Marsh" )
two.model= ' Negative =~ LET_1 + LET_3 + LET_5
Positive =~ LET_2 + LET_4 + LET_6'
one.model= 'LET =~ LET_1 + LET_2 + LET_3 + LET_4 + LET_5 + LET_6'
two.fit=cfa(two.model, data=LET)
one.fit=cfa(one.model, data=LET)
semPaths(two.fit, whatLabels = "std", layout = "tree")
semPaths(one.fit, whatLabels = "std", layout = "tree")
#summaries
summary(two.fit, standardized = TRUE, rsquare=TRUE)
## lavaan (0.5-18) converged normally after 31 iterations
##
## Used Total
## Number of observations 474 757
##
## Estimator ML
## Minimum Function Test Statistic 163.816
## Degrees of freedom 8
## P-value (Chi-square) 0.000
##
## Parameter estimates:
##
## Information Expected
## Standard Errors Standard
##
## Estimate Std.err Z-value P(>|z|) Std.lv Std.all
## Latent variables:
## Negative =~
## LET_1 1.000 0.987 0.639
## LET_3 1.415 0.102 13.846 0.000 1.396 0.926
## LET_5 1.247 0.087 14.323 0.000 1.231 0.806
## Positive =~
## LET_2 1.000 0.795 0.614
## LET_4 1.216 0.206 5.900 0.000 0.967 0.837
## LET_6 0.533 0.079 6.725 0.000 0.424 0.386
##
## Covariances:
## Negative ~~
## Positive -0.054 0.045 -1.221 0.222 -0.069 -0.069
##
## Variances:
## LET_1 1.407 0.103 1.407 0.591
## LET_3 0.322 0.096 0.322 0.142
## LET_5 0.814 0.090 0.814 0.350
## LET_2 1.046 0.122 1.046 0.623
## LET_4 0.400 0.153 0.400 0.300
## LET_6 1.025 0.073 1.025 0.851
## Negative 0.973 0.133 1.000 1.000
## Positive 0.633 0.133 1.000 1.000
##
## R-Square:
##
## LET_1 0.409
## LET_3 0.858
## LET_5 0.650
## LET_2 0.377
## LET_4 0.700
## LET_6 0.149
summary(one.fit, standardized = TRUE, rsquare=TRUE)
## lavaan (0.5-18) converged normally after 26 iterations
##
## Used Total
## Number of observations 474 757
##
## Estimator ML
## Minimum Function Test Statistic 294.824
## Degrees of freedom 9
## P-value (Chi-square) 0.000
##
## Parameter estimates:
##
## Information Expected
## Standard Errors Standard
##
## Estimate Std.err Z-value P(>|z|) Std.lv Std.all
## Latent variables:
## LET =~
## LET_1 1.000 0.985 0.638
## LET_2 -0.096 0.064 -1.509 0.131 -0.095 -0.073
## LET_3 1.419 0.099 14.292 0.000 1.397 0.927
## LET_4 -0.114 0.057 -1.993 0.046 -0.112 -0.097
## LET_5 1.252 0.087 14.362 0.000 1.233 0.808
## LET_6 0.417 0.056 7.456 0.000 0.410 0.374
##
## Variances:
## LET_1 1.410 0.102 1.410 0.593
## LET_2 1.669 0.109 1.669 0.995
## LET_3 0.321 0.086 0.321 0.141
## LET_4 1.323 0.086 1.323 0.991
## LET_5 0.808 0.084 0.808 0.347
## LET_6 1.037 0.069 1.037 0.860
## LET 0.970 0.132 1.000 1.000
##
## R-Square:
##
## LET_1 0.407
## LET_2 0.005
## LET_3 0.859
## LET_4 0.009
## LET_5 0.653
## LET_6 0.140
correl = residuals(two.fit, type="cor")
correl
## $type
## [1] "cor.bollen"
##
## $cor
## LET_1 LET_3 LET_5 LET_2 LET_4 LET_6
## LET_1 0.000
## LET_3 0.001 0.000
## LET_5 0.002 -0.001 0.000
## LET_2 0.194 -0.035 -0.137 0.000
## LET_4 0.113 -0.062 -0.073 -0.004 0.000
## LET_6 0.286 0.373 0.329 0.020 0.002 0.000
##
## $mean
## LET_1 LET_3 LET_5 LET_2 LET_4 LET_6
## 0 0 0 0 0 0
View(correl$cor)
correl1 = residuals(one.fit, type="cor")
correl1
## $type
## [1] "cor.bollen"
##
## $cor
## LET_1 LET_2 LET_3 LET_4 LET_5 LET_6
## LET_1 0.000
## LET_2 0.213 0.000
## LET_3 0.002 -0.007 0.000
## LET_4 0.138 0.502 -0.026 0.000
## LET_5 0.002 -0.112 -0.003 -0.041 0.000
## LET_6 0.030 0.285 0.002 0.361 0.005 0.000
##
## $mean
## LET_1 LET_2 LET_3 LET_4 LET_5 LET_6
## 0 0 0 0 0 0
View(correl1$cor)
zcorrels = residuals(two.fit, type = "standardized")
View(zcorrels$cov)
zcorrels1 = residuals(one.fit, type = "standardized")
View(zcorrels1$cov)
modindices(two.fit, sort. = TRUE, minimum.value = 3.84)
## lhs op rhs mi epc sepc.lv sepc.all sepc.nox
## 1 Negative =~ LET_6 88.623 0.485 0.479 0.436 0.436
## 2 LET_2 ~~ LET_4 88.614 -12.461 -12.461 -8.322 -8.322
## 3 Positive =~ LET_1 27.610 0.433 0.344 0.223 0.223
## 4 LET_3 ~~ LET_5 27.610 -8.613 -8.613 -3.745 -3.745
## 5 LET_1 ~~ LET_2 23.908 0.306 0.306 0.153 0.153
## 6 LET_5 ~~ LET_2 20.622 -0.232 -0.232 -0.118 -0.118
## 7 LET_3 ~~ LET_6 16.732 0.182 0.182 0.110 0.110
## 8 LET_2 ~~ LET_6 16.119 1.255 1.255 0.882 0.882
## 9 Negative =~ LET_4 16.118 -0.254 -0.250 -0.217 -0.217
## 10 LET_3 ~~ LET_4 8.872 -0.129 -0.129 -0.074 -0.074
## 11 LET_5 ~~ LET_6 5.708 0.114 0.114 0.068 0.068
## 12 LET_1 ~~ LET_3 4.800 1.905 1.905 0.819 0.819
## 13 Positive =~ LET_5 4.799 -0.149 -0.118 -0.078 -0.078
modindices(one.fit, sort. = TRUE, minimum.value = 3.84)
## lhs op rhs mi epc sepc.lv sepc.all sepc.nox
## 1 LET_2 ~~ LET_4 121.651 0.754 0.754 0.503 0.503
## 2 LET_4 ~~ LET_6 74.153 0.468 0.468 0.369 0.369
## 3 LET_2 ~~ LET_6 45.901 0.413 0.413 0.291 0.291
## 4 LET_1 ~~ LET_2 39.681 0.463 0.463 0.232 0.232
## 5 LET_2 ~~ LET_5 21.979 -0.283 -0.283 -0.143 -0.143
## 6 LET_1 ~~ LET_4 16.635 0.267 0.267 0.150 0.150
## 7 LET_3 ~~ LET_4 7.648 -0.149 -0.149 -0.086 -0.086
## 8 LET_3 ~~ LET_5 6.441 -0.611 -0.611 -0.266 -0.266
fitmeasures(two.fit)
## npar fmin chisq
## 13.000 0.173 163.816
## df pvalue baseline.chisq
## 8.000 0.000 967.563
## baseline.df baseline.pvalue cfi
## 15.000 0.000 0.836
## tli nnfi rfi
## 0.693 0.693 0.683
## nfi pnfi ifi
## 0.831 0.443 0.838
## rni logl unrestricted.logl
## 0.836 -4469.424 -4387.516
## aic bic ntotal
## 8964.848 9018.944 474.000
## bic2 rmsea rmsea.ci.lower
## 8977.684 0.203 0.176
## rmsea.ci.upper rmsea.pvalue rmr
## 0.230 0.000 0.241
## rmr_nomean srmr srmr_bentler
## 0.241 0.140 0.140
## srmr_bentler_nomean srmr_bollen srmr_bollen_nomean
## 0.140 0.140 0.140
## srmr_mplus srmr_mplus_nomean cn_05
## 0.140 0.140 45.870
## cn_01 gfi agfi
## 59.131 0.906 0.754
## pgfi mfi ecvi
## 0.345 0.848 0.400
fitmeasures(one.fit)
## npar fmin chisq
## 12.000 0.311 294.824
## df pvalue baseline.chisq
## 9.000 0.000 967.563
## baseline.df baseline.pvalue cfi
## 15.000 0.000 0.700
## tli nnfi rfi
## 0.500 0.500 0.492
## nfi pnfi ifi
## 0.695 0.417 0.702
## rni logl unrestricted.logl
## 0.700 -4534.928 -4387.516
## aic bic ntotal
## 9093.856 9143.791 474.000
## bic2 rmsea rmsea.ci.lower
## 9105.705 0.259 0.234
## rmsea.ci.upper rmsea.pvalue rmr
## 0.285 0.000 0.243
## rmr_nomean srmr srmr_bentler
## 0.243 0.161 0.161
## srmr_bentler_nomean srmr_bollen srmr_bollen_nomean
## 0.161 0.161 0.161
## srmr_mplus srmr_mplus_nomean cn_05
## 0.161 0.161 28.201
## cn_01 gfi agfi
## 35.833 0.819 0.578
## pgfi mfi ecvi
## 0.351 0.740 0.673
LETTR<-select(LET, LET_1 , LET_2 , LET_3, LET_4 , LET_5 , LET_6)
colnames(LETTR) <- c("1","2", "3", "4", "5", "6")
#Target Roration
Targ_key <- make.keys(6,list(f1=1:6))
Targ_key <- scrub(Targ_key,isvalue=1) #fix the 0s, allow the NAs to be estimated
Targ_key <- list(Targ_key)
out_targetQ <- fa(LETTR,1,rotate="TargetQ",Target=Targ_key) #TargetT for orthogonal rotation
out_targetQ[c("loadings", "score.cor", "TLI", "RMSEA","uniquenesses")]
## $loadings
##
## Loadings:
## MR1
## 1 0.638
## 2
## 3 0.927
## 4
## 5 0.808
## 6 0.374
##
## MR1
## SS loadings 2.074
## Proportion Var 0.346
##
## $<NA>
## NULL
##
## $TLI
## [1] 0.4970074
##
## $RMSEA
## RMSEA lower upper confidence
## 0.2603601 0.2398360 0.2798651 0.1000000
##
## $uniquenesses
## 1 2 3 4 5 6
## 0.5925276 0.9946447 0.1410899 0.9906358 0.3471651 0.8601698
out_targetQ
## Factor Analysis using method = minres
## Call: fa(r = LETTR, nfactors = 1, rotate = "TargetQ", Target = Targ_key)
## Standardized loadings (pattern matrix) based upon correlation matrix
## MR1 h2 u2 com
## 1 0.64 0.4075 0.59 1
## 2 -0.07 0.0054 0.99 1
## 3 0.93 0.8589 0.14 1
## 4 -0.10 0.0094 0.99 1
## 5 0.81 0.6528 0.35 1
## 6 0.37 0.1398 0.86 1
##
## MR1
## SS loadings 2.07
## Proportion Var 0.35
##
## 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 2.04 with Chi Square of 1537.42
## The degrees of freedom for the model are 9 and the objective function was 0.62
##
## The root mean square of the residuals (RMSR) is 0.19
## The df corrected root mean square of the residuals is 0.25
##
## The harmonic number of observations is 474 with the empirical chi square 516.31 with prob < 1.8e-105
## The total number of observations was 757 with MLE Chi Square = 468.05 with prob < 4e-95
##
## Tucker Lewis Index of factoring reliability = 0.497
## RMSEA index = 0.26 and the 90 % confidence intervals are 0.24 0.28
## BIC = 408.38
## Fit based upon off diagonal values = 0.73
## Measures of factor score adequacy
## MR1
## Correlation of scores with factors 0.95
## Multiple R square of scores with factors 0.90
## Minimum correlation of possible factor scores 0.80
CFI
1-((out_targetQ$STATISTIC - out_targetQ$dof)/(out_targetQ$null.chisq- out_targetQ$null.dof))
## [1] 0.6984742
As two Factor
LETTR2<-select(LET, LET_1 , LET_3 , LET_5, LET_2 , LET_4 , LET_6)
colnames(LETTR2) <- c("1","2", "3", "4", "5", "6")
Targ_key <- make.keys(6,list(f1=1:3, f2=4:6))
Targ_key <- scrub(Targ_key,isvalue=1) #fix the 0s, allow the NAs to be estimated
Targ_key <- list(Targ_key)
out_targetQ <- fa(LETTR2,2,rotate="TargetQ",Target=Targ_key) #TargetT for orthogonal rotation
out_targetQ[c("loadings", "score.cor", "TLI", "RMSEA","uniquenesses")]
## $loadings
##
## Loadings:
## MR1 MR2
## 1 0.642 0.211
## 2 0.910
## 3 0.829 -0.112
## 4 0.729
## 5 -0.117 0.714
## 6 0.374 0.423
##
## MR1 MR2
## SS loadings 2.093 1.279
## Proportion Var 0.349 0.213
## Cumulative Var 0.349 0.562
##
## $score.cor
## [,1] [,2]
## [1,] 1.0000000 0.1161984
## [2,] 0.1161984 1.0000000
##
## $TLI
## [1] 0.8880282
##
## $RMSEA
## RMSEA lower upper confidence
## 0.12286601 0.09326734 0.15405218 0.10000000
##
## $uniquenesses
## 1 2 3 4 5 6
## 0.5294933 0.1736433 0.3089211 0.4666129 0.4844581 0.6645471
out_targetQ
## Factor Analysis using method = minres
## Call: fa(r = LETTR2, nfactors = 2, rotate = "TargetQ", Target = Targ_key)
## Standardized loadings (pattern matrix) based upon correlation matrix
## MR1 MR2 h2 u2 com
## 1 0.64 0.21 0.47 0.53 1.2
## 2 0.91 -0.04 0.83 0.17 1.0
## 3 0.83 -0.11 0.69 0.31 1.0
## 4 -0.10 0.73 0.53 0.47 1.0
## 5 -0.12 0.71 0.52 0.48 1.1
## 6 0.37 0.42 0.34 0.66 2.0
##
## MR1 MR2
## SS loadings 2.09 1.28
## Proportion Var 0.35 0.21
## Cumulative Var 0.35 0.56
## Proportion Explained 0.62 0.38
## Cumulative Proportion 0.62 1.00
##
## With factor correlations of
## MR1 MR2
## MR1 1.00 0.05
## MR2 0.05 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 2.04 with Chi Square of 1537.42
## The degrees of freedom for the model are 4 and the objective function was 0.07
##
## The root mean square of the residuals (RMSR) is 0.03
## The df corrected root mean square of the residuals is 0.07
##
## The harmonic number of observations is 474 with the empirical chi square 16.18 with prob < 0.0028
## The total number of observations was 757 with MLE Chi Square = 49.38 with prob < 4.9e-10
##
## Tucker Lewis Index of factoring reliability = 0.888
## RMSEA index = 0.123 and the 90 % confidence intervals are 0.093 0.154
## BIC = 22.86
## Fit based upon off diagonal values = 0.99
## Measures of factor score adequacy
## MR1 MR2
## Correlation of scores with factors 0.94 0.85
## Multiple R square of scores with factors 0.89 0.72
## Minimum correlation of possible factor scores 0.78 0.44
CFI
1-((out_targetQ$STATISTIC - out_targetQ$dof)/(out_targetQ$null.chisq- out_targetQ$null.dof))
## [1] 0.9701942