#### Simple one-group confirmatory factor analysis

### First, set the working directory where R will find the data

setwd("H:/data/User/Classes/multiv_13/YTs/YTSplit/Week 10 wmv - 2016/Wk10.R")
getwd()
## [1] "H:/data/User/Classes/multiv_13/YTs/YTSplit/Week 10 wmv - 2016/Wk10.R"
### Read the data
cfa1<- read.csv("Class09b_practice_2014.csv")
install.packages("lavaan")
library(lavaan)
## This is lavaan 0.6-7
## lavaan is BETA software! Please report any bugs.
#Confirmatory factor model with raw data

cfa.mod<- ' eat =~ EatingDisorder + IntuitiveEating + Body
mood =~ Anxiety + DASS + Interpersonal
shame =~ Guilt + SexualShame + Embarrassment '

fit.cfa <- cfa(cfa.mod, data=cfa1[2:10])
summary(fit.cfa, fit.measures=TRUE, standardized=TRUE)
fitmeasures(fit.cfa)
## lavaan 0.6-7 ended normally after 144 iterations
##
## Estimator ML
## Optimization method NLMINB
## Number of free parameters 21
##
## Number of observations 500
##
## Model Test User Model:
##
## Test statistic 28.857
## Degrees of freedom 24
## P-value (Chi-square) 0.226
##
## Model Test Baseline Model:
##
## Test statistic 3007.754
## Degrees of freedom 36
## P-value 0.000
##
## User Model versus Baseline Model:
##
## Comparative Fit Index (CFI) 0.998
## Tucker-Lewis Index (TLI) 0.998
##
## Loglikelihood and Information Criteria:
##
## Loglikelihood user model (H0) -14111.921
## Loglikelihood unrestricted model (H1) -14097.492
##
## Akaike (AIC) 28265.841
## Bayesian (BIC) 28354.348
## Sample-size adjusted Bayesian (BIC) 28287.693
##
## Root Mean Square Error of Approximation:
##
## RMSEA 0.020
## 90 Percent confidence interval - lower 0.000
## 90 Percent confidence interval - upper 0.043
## P-value RMSEA <= 0.05 0.987
##
## Standardized Root Mean Square Residual:
##
## SRMR 0.021
##
## Parameter Estimates:
##
## Standard errors Standard
## Information Expected
## Information saturated (h1) model Structured
##
## Latent Variables:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## eat =~
## EatingDisorder 1.000 6.170 0.923
## IntuitiveEatng 1.378 0.054 25.511 0.000 8.501 0.850
## Body 0.990 0.039 25.536 0.000 6.110 0.851
## mood =~
## Anxiety 1.000 5.781 0.799
## DASS 1.005 0.043 23.233 0.000 5.809 0.933
## Interpersonal 1.567 0.070 22.492 0.000 9.060 0.880
## shame =~
## Guilt 1.000 9.127 0.886
## SexualShame 0.619 0.027 22.971 0.000 5.653 0.811
## Embarrassment 0.634 0.024 26.540 0.000 5.788 0.915
##
## Covariances:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## eat ~~
## mood 2.872 1.743 1.648 0.099 0.081 0.081
## shame -1.176 2.751 -0.427 0.669 -0.021 -0.021
## mood ~~
## shame 1.692 2.571 0.658 0.510 0.032 0.032
##
## Variances:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## .EatingDisorder 6.573 0.989 6.645 0.000 6.573 0.147
## .IntuitiveEatng 27.733 2.446 11.338 0.000 27.733 0.277
## .Body 14.260 1.261 11.312 0.000 14.260 0.276
## .Anxiety 18.973 1.448 13.103 0.000 18.973 0.362
## .DASS 5.050 0.878 5.754 0.000 5.050 0.130
## .Interpersonal 23.978 2.501 9.587 0.000 23.978 0.226
## .Guilt 22.756 2.486 9.155 0.000 22.756 0.215
## .SexualShame 16.634 1.308 12.716 0.000 16.634 0.342
## .Embarrassment 6.492 0.912 7.115 0.000 6.492 0.162
## eat 38.073 2.934 12.978 0.000 1.000 1.000
## mood 33.416 3.193 10.465 0.000 1.000 1.000
## shame 83.307 6.858 12.147 0.000 1.000 1.000

#install.packages("lavaanPlot")
library(lavaanPlot)
## Warning: package 'lavaanPlot' was built under R version 4.0.3
lavaanPlot(model = fit.cfa1, sig=.05,stars=c("regress","latent","covs"),
node_options = list(shape = "box", fontname = "Arial"),
edge_options = list(color = "black"),
coefs = TRUE, covs = TRUE, stand=TRUE)