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/allsurveysYT1.csv")
data<-tbl_df(data)
LS<-select(data, LS_1, LS_2, LS_3, LS_4, LS_5)
LS<-tbl_df(LS)
LS
## Source: local data frame [1,160 x 5]
##
## LS_1 LS_2 LS_3 LS_4 LS_5
## 1 5 6 6 6 2
## 2 5 5 5 4 6
## 3 5 6 5 4 3
## 4 3 6 5 5 3
## 5 4 6 4 5 3
## 6 5 4 5 5 3
## 7 5 6 3 3 6
## 8 5 6 6 6 4
## 9 7 7 6 6 6
## 10 2 2 6 2 3
## .. ... ... ... ... ...
As only one factor
one.factor = 'LS =~ LS_1 + LS_2 + LS_3 + LS_4 + LS_5
'
Two Factor - diffecult to justify from the theoretical standpoint but will try to see the fit anyway
first.model= ' F1 =~ LS_1 + LS_2
F2 =~ LS_3 + LS_4 + LS_5
'
Running the models
one.fit=cfa(one.factor, data=LS, missing = "fiml", std.lv = T)
## Warning in lav_data_full(data = data, group = group, group.label = group.label, : lavaan WARNING: some cases are empty and will be removed:
## 17 22 23 24 28 29 43 45 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 110 111 112 116 121 122 123 124 125 128 129 130 131 133 135 137 138 140 147 151 152 155 156 162 166 169 170 171 172 173 174 176 177 179 180 183 184 186 187 188 189 192 194 195 197 200 202 203 204 207 208 210 212 214 215 217 220 222 223 224 226 227 228 229 230 234 238 240 243 245 246 247 249 252 255 256 265 266 267 268 270 271 274 275 280 281 282 284 286 287 289 291 292 298 300 304 309 310 311 312 315 316 317 320 322 325 327 330 333 334 336 339 340 344 348 350 351 352 354 355 357 360 361 362 364 365 366 367 368 369 370 371 372 373 374 375 376 377 379 380 381 384 385 386 389 390 397 398 399 400 401 402 403 404 405 406 407 408 410 416 417 418 419 420 421 422 423 424 425 427 428 429 430 431 432 434 436 444 445 446 447 448 452 453 454 455 456 457 459 460 462 463 464 465 467 468 470 472 473 474 475 476 478 481 482 485 486 490 491 493 495 539 540 541 542 543 544 545 546 548 549 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 581 582 584 585 586 587 588 589 590 591 592 593 594 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 662 679 687 723 729 756 760 781 782 783 784 785 809 810 829 903 904 905 906 907 908 909 910 911 916 933 938 941 943 945 948 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
two.fit=cfa(first.model, data=LS, missing = "fiml", std.lv = T)
## Warning in lav_data_full(data = data, group = group, group.label = group.label, : lavaan WARNING: some cases are empty and will be removed:
## 17 22 23 24 28 29 43 45 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 110 111 112 116 121 122 123 124 125 128 129 130 131 133 135 137 138 140 147 151 152 155 156 162 166 169 170 171 172 173 174 176 177 179 180 183 184 186 187 188 189 192 194 195 197 200 202 203 204 207 208 210 212 214 215 217 220 222 223 224 226 227 228 229 230 234 238 240 243 245 246 247 249 252 255 256 265 266 267 268 270 271 274 275 280 281 282 284 286 287 289 291 292 298 300 304 309 310 311 312 315 316 317 320 322 325 327 330 333 334 336 339 340 344 348 350 351 352 354 355 357 360 361 362 364 365 366 367 368 369 370 371 372 373 374 375 376 377 379 380 381 384 385 386 389 390 397 398 399 400 401 402 403 404 405 406 407 408 410 416 417 418 419 420 421 422 423 424 425 427 428 429 430 431 432 434 436 444 445 446 447 448 452 453 454 455 456 457 459 460 462 463 464 465 467 468 470 472 473 474 475 476 478 481 482 485 486 490 491 493 495 539 540 541 542 543 544 545 546 548 549 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 581 582 584 585 586 587 588 589 590 591 592 593 594 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 662 679 687 723 729 756 760 781 782 783 784 785 809 810 829 903 904 905 906 907 908 909 910 911 916 933 938 941 943 945 948 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
## Warning in lavaan::lavaan(model = first.model, data = LS, std.lv = T,
## missing = "fiml", : lavaan WARNING: covariance matrix of latent variables
## is not positive definite; use inspect(fit,"cov.lv") to investigate.
Create pictures of models
#One Factor
semPaths(one.fit, whatLabels = "std", layout = "tree", title=T)
#Two Factors
semPaths(two.fit, whatLabels = "std", layout = "tree")
## Warning in lavaan(slotOptions = object@Options, slotParTable =
## object@ParTable, : lavaan WARNING: covariance matrix of latent variables is
## not positive definite; use inspect(fit,"cov.lv") to investigate.
Summaries
summary(one.fit, standardized = TRUE, rsquare=TRUE)
## lavaan (0.5-18) converged normally after 28 iterations
##
## Used Total
## Number of observations 772 1160
##
## Number of missing patterns 10
##
## Estimator ML
## Minimum Function Test Statistic 22.789
## Degrees of freedom 5
## P-value (Chi-square) 0.000
##
## Parameter estimates:
##
## Information Observed
## Standard Errors Standard
##
## Estimate Std.err Z-value P(>|z|) Std.lv Std.all
## Latent variables:
## LS =~
## LS_1 1.385 0.052 26.867 0.000 1.385 0.846
## LS_2 1.308 0.056 23.374 0.000 1.308 0.769
## LS_3 1.409 0.053 26.764 0.000 1.409 0.838
## LS_4 1.264 0.055 22.970 0.000 1.264 0.764
## LS_5 1.200 0.071 16.869 0.000 1.200 0.607
##
## Intercepts:
## LS_1 4.406 0.060 73.942 0.000 4.406 2.692
## LS_2 4.880 0.062 78.751 0.000 4.880 2.868
## LS_3 4.925 0.061 80.574 0.000 4.925 2.928
## LS_4 4.456 0.060 73.842 0.000 4.456 2.694
## LS_5 3.873 0.072 54.088 0.000 3.873 1.961
## LS 0.000 0.000 0.000
##
## Variances:
## LS_1 0.760 0.059 0.760 0.284
## LS_2 1.184 0.075 1.184 0.409
## LS_3 0.843 0.063 0.843 0.298
## LS_4 1.139 0.072 1.139 0.416
## LS_5 2.461 0.138 2.461 0.631
## LS 1.000 1.000 1.000
##
## R-Square:
##
## LS_1 0.716
## LS_2 0.591
## LS_3 0.702
## LS_4 0.584
## LS_5 0.369
summary(two.fit, standardized = TRUE, rsquare=TRUE)
## lavaan (0.5-18) converged normally after 33 iterations
##
## Used Total
## Number of observations 772 1160
##
## Number of missing patterns 10
##
## Estimator ML
## Minimum Function Test Statistic 22.252
## Degrees of freedom 4
## P-value (Chi-square) 0.000
##
## Parameter estimates:
##
## Information Observed
## Standard Errors Standard
##
## Estimate Std.err Z-value P(>|z|) Std.lv Std.all
## Latent variables:
## F1 =~
## LS_1 1.378 0.053 26.202 0.000 1.378 0.842
## LS_2 1.304 0.056 23.168 0.000 1.304 0.766
## F2 =~
## LS_3 1.404 0.053 26.475 0.000 1.404 0.835
## LS_4 1.261 0.055 22.844 0.000 1.261 0.762
## LS_5 1.196 0.071 16.780 0.000 1.196 0.606
##
## Covariances:
## F1 ~~
## F2 1.011 0.015 65.703 0.000 1.011 1.011
##
## Intercepts:
## LS_1 4.406 0.060 73.944 0.000 4.406 2.692
## LS_2 4.880 0.062 78.742 0.000 4.880 2.868
## LS_3 4.925 0.061 80.575 0.000 4.925 2.928
## LS_4 4.456 0.060 73.850 0.000 4.456 2.694
## LS_5 3.873 0.072 54.093 0.000 3.873 1.961
## F1 0.000 0.000 0.000
## F2 0.000 0.000 0.000
##
## Variances:
## LS_1 0.780 0.065 0.780 0.291
## LS_2 1.195 0.077 1.195 0.413
## LS_3 0.856 0.066 0.856 0.303
## LS_4 1.147 0.072 1.147 0.419
## LS_5 2.470 0.139 2.470 0.633
## F1 1.000 1.000 1.000
## F2 1.000 1.000 1.000
##
## R-Square:
##
## LS_1 0.709
## LS_2 0.587
## LS_3 0.697
## LS_4 0.581
## LS_5 0.367
Residual correlations
correl.1 = residuals(one.fit, type="cor")
correl.1
## $type
## [1] "cor.bollen"
##
## $cor
## LS_1 LS_2 LS_3 LS_4 LS_5
## LS_1 0.000
## LS_2 -0.006 0.000
## LS_3 -0.014 0.032 0.000
## LS_4 0.012 -0.010 -0.010 0.000
## LS_5 0.029 -0.058 0.000 0.008 0.000
##
## $mean
## LS_1 LS_2 LS_3 LS_4 LS_5
## 0.000 0.001 -0.001 0.001 0.002
#View(correl.1$cor)
correl = residuals(two.fit, type="cor")
correl
## $type
## [1] "cor.bollen"
##
## $cor
## LS_1 LS_2 LS_3 LS_4 LS_5
## LS_1 0.000
## LS_2 0.000 0.000
## LS_3 -0.016 0.029 0.000
## LS_4 0.010 -0.013 -0.006 0.000
## LS_5 0.028 -0.060 0.004 0.011 0.000
##
## $mean
## LS_1 LS_2 LS_3 LS_4 LS_5
## 0.000 0.001 -0.001 0.001 0.002
#View(correl$cor)
zscore correlation anything over 1.96 is going to be statistically significant at the .05 level
zcorrels10 = residuals(one.fit, type = "standardized")
zcorrels10$cov
## LS_1 LS_2 LS_3 LS_4 LS_5
## LS_1 NA
## LS_2 NA NA
## LS_3 NA NA NA
## LS_4 NA NA NA NA
## LS_5 9.051 -6.901 0.070 0.678 NA
zcorrels = residuals(two.fit, type = "standardized")
zcorrels$cov
## LS_1 LS_2 LS_3 LS_4 LS_5
## LS_1 NA
## LS_2 NA NA
## LS_3 NA NA NA
## LS_4 NA NA NA NA
## LS_5 17.976 -8.147 1.524 0.944 NA
Modification indicies
modindices(one.fit, sort. = TRUE, minimum.value = 3.84)
## lhs op rhs mi epc sepc.lv sepc.all sepc.nox
## 1 LS_2 ~~ LS_3 14.810 0.222 0.222 0.078 0.078
## 2 LS_2 ~~ LS_5 12.672 -0.263 -0.263 -0.078 -0.078
## 3 LS_1 ~~ LS_3 7.161 -0.158 -0.158 -0.057 -0.057
## 4 LS_1 ~~ LS_5 6.277 0.167 0.167 0.052 0.052
modindices(two.fit, sort. = TRUE, minimum.value = 3.84)
## Warning in lavaan(slotOptions = object@Options, slotParTable =
## object@ParTable, : lavaan WARNING: covariance matrix of latent variables is
## not positive definite; use inspect(fit,"cov.lv") to investigate.
## lhs op rhs mi epc sepc.lv sepc.all sepc.nox
## 1 LS_2 ~~ LS_3 15.459 0.261 0.261 0.091 0.091
## 2 LS_2 ~~ LS_5 14.673 -0.294 -0.294 -0.088 -0.088
## 3 LS_1 ~~ LS_3 10.276 -0.210 -0.210 -0.076 -0.076
## 4 LS_1 ~~ LS_5 5.492 0.163 0.163 0.050 0.050
Fit Measures
fitmeasures(one.fit)
## npar fmin chisq
## 15.000 0.015 22.789
## df pvalue baseline.chisq
## 5.000 0.000 1755.560
## baseline.df baseline.pvalue cfi
## 10.000 0.000 0.990
## tli nnfi rfi
## 0.980 0.980 0.974
## nfi pnfi ifi
## 0.987 0.494 0.990
## rni logl unrestricted.logl
## 0.990 -6448.960 -6437.565
## aic bic ntotal
## 12927.919 12997.654 772.000
## bic2 rmsea rmsea.ci.lower
## 12950.022 0.068 0.041
## rmsea.ci.upper rmsea.pvalue rmr
## 0.097 0.126 0.055
## rmr_nomean srmr srmr_bentler
## 0.063 0.017 0.017
## srmr_bentler_nomean srmr_bollen srmr_bollen_nomean
## 0.020 0.017 0.020
## srmr_mplus srmr_mplus_nomean cn_05
## 0.017 0.020 376.022
## cn_01 gfi agfi
## 512.059 0.997 0.987
## pgfi mfi ecvi
## 0.249 0.989 NA
fitmeasures(two.fit)
## npar fmin chisq
## 16.000 0.014 22.252
## df pvalue baseline.chisq
## 4.000 0.000 1755.560
## baseline.df baseline.pvalue cfi
## 10.000 0.000 0.990
## tli nnfi rfi
## 0.974 0.974 0.968
## nfi pnfi ifi
## 0.987 0.395 0.990
## rni logl unrestricted.logl
## 0.990 -6448.691 -6437.565
## aic bic ntotal
## 12929.382 13003.766 772.000
## bic2 rmsea rmsea.ci.lower
## 12952.958 0.077 0.048
## rmsea.ci.upper rmsea.pvalue rmr
## 0.109 0.064 0.055
## rmr_nomean srmr srmr_bentler
## 0.064 0.017 0.017
## srmr_bentler_nomean srmr_bollen srmr_bollen_nomean
## 0.020 0.017 0.020
## srmr_mplus srmr_mplus_nomean cn_05
## 0.017 0.020 330.163
## cn_01 gfi agfi
## 461.617 0.997 0.985
## pgfi mfi ecvi
## 0.199 0.988 NA
LSTR<-select(data, LS_1, LS_2, LS_3, LS_4, LS_5)
colnames( LSTR) <- c("1","2", "3", "4", "5")
LSTR<-tbl_df( LSTR)
LSTR
## Source: local data frame [1,160 x 5]
##
## 1 2 3 4 5
## 1 5 6 6 6 2
## 2 5 5 5 4 6
## 3 5 6 5 4 3
## 4 3 6 5 5 3
## 5 4 6 4 5 3
## 6 5 4 5 5 3
## 7 5 6 3 3 6
## 8 5 6 6 6 4
## 9 7 7 6 6 6
## 10 2 2 6 2 3
## .. . . . . .
#Target Roration
Targ_key <- make.keys(5,list(f1=1:5))
Targ_key <- scrub(Targ_key,isvalue=1) #fix the 0s, allow the NAs to be estimated
Targ_key <- list(Targ_key)
LSTR_cor <- corFiml(LSTR) # convert the raw data to correlation matrix uisng FIML
out_targetQ <- fa(LSTR_cor,1,rotate="TargetQ", n.obs = 772, Target=Targ_key) #TargetT for orthogonal rotation
out_targetQ[c("loadings", "score.cor", "TLI", "RMSEA","uniquenesses")]
## $loadings
##
## Loadings:
## MR1
## 1 0.847
## 2 0.769
## 3 0.839
## 4 0.763
## 5 0.606
##
## MR1
## SS loadings 2.962
## Proportion Var 0.592
##
## $<NA>
## NULL
##
## $TLI
## [1] 0.9801957
##
## $RMSEA
## RMSEA lower upper confidence
## 0.07020598 0.04340924 0.09928156 0.10000000
##
## $uniquenesses
## 1 2 3 4 5
## 0.2820108 0.4091865 0.2966312 0.4172796 0.6328908
out_targetQ
## Factor Analysis using method = minres
## Call: fa(r = LSTR_cor, nfactors = 1, n.obs = 772, rotate = "TargetQ",
## Target = Targ_key)
## Standardized loadings (pattern matrix) based upon correlation matrix
## MR1 h2 u2 com
## 1 0.85 0.72 0.28 1
## 2 0.77 0.59 0.41 1
## 3 0.84 0.70 0.30 1
## 4 0.76 0.58 0.42 1
## 5 0.61 0.37 0.63 1
##
## MR1
## SS loadings 2.96
## Proportion Var 0.59
##
## Mean item complexity = 1
## Test of the hypothesis that 1 factor is sufficient.
##
## The degrees of freedom for the null model are 10 and the objective function was 2.5 with Chi Square of 1920.57
## The degrees of freedom for the model are 5 and the objective function was 0.03
##
## The root mean square of the residuals (RMSR) is 0.02
## The df corrected root mean square of the residuals is 0.03
##
## The harmonic number of observations is 772 with the empirical chi square 8.9 with prob < 0.11
## The total number of observations was 772 with MLE Chi Square = 23.9 with prob < 0.00023
##
## Tucker Lewis Index of factoring reliability = 0.98
## RMSEA index = 0.07 and the 90 % confidence intervals are 0.043 0.099
## BIC = -9.34
## Fit based upon off diagonal values = 1
## Measures of factor score adequacy
## MR1
## Correlation of scores with factors 0.95
## Multiple R square of scores with factors 0.89
## Minimum correlation of possible factor scores 0.79
CFI
1-((out_targetQ$STATISTIC - out_targetQ$dof)/(out_targetQ$null.chisq- out_targetQ$null.dof))
## [1] 0.9901065