Create a Zero df Model
In the first chapter, we created a model of text-speed from the HolzingerSwineford1939 dataset with six manifest variables
x4, x5, x6, x7, x8, x9. Create a model of only reading comprehension with x4, x5, and x6.

This model should have zero degrees of freedom because it is not identified, as we are estimating as many parameters as we
have options with only three manifest variables. Analyze the model to find zero degrees of freedom. lavaan and the
HolzingerSwineford1939 dataset have been loaded for you.

* Name your model text.model and your latent variable text.
* Use variables x4, x5, and x6 as the manifest variables.
* Use the cfa() function to analyze the model.
* Use the summary() function to view the degrees of freedom.

# Create your text model specification
text.model <- 'text =~ x4 + x5 + x6'

# Analyze the model and include data argument
text.fit <- cfa(model = text.model, data = HolzingerSwineford1939)

# Summarize the model
summary(text.fit, standardized = TRUE, fit.measures = TRUE)


lavaan 0.6-11 ended normally after 15 iterations

  Estimator                                         ML
  Optimization method                           NLMINB
  Number of model parameters                         6
                                                      
  Number of observations                           301
                                                      
Model Test User Model:
                                                      
  Test statistic                                 0.000
  Degrees of freedom                                 0

Model Test Baseline Model:

  Test statistic                               497.430
  Degrees of freedom                                 3
  P-value                                        0.000

User Model versus Baseline Model:

  Comparative Fit Index (CFI)                    1.000
  Tucker-Lewis Index (TLI)                       1.000

Loglikelihood and Information Criteria:

  Loglikelihood user model (H0)              -1181.065
  Loglikelihood unrestricted model (H1)      -1181.065
                                                      
  Akaike (AIC)                                2374.130
  Bayesian (BIC)                              2396.372
  Sample-size adjusted Bayesian (BIC)         2377.344

Root Mean Square Error of Approximation:

  RMSEA                                          0.000
  90 Percent confidence interval - lower         0.000
  90 Percent confidence interval - upper         0.000
  P-value RMSEA <= 0.05                             NA

Standardized Root Mean Square Residual:

  SRMR                                           0.000

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
  text =~                                                               
    x4                1.000                               0.984    0.847
    x5                1.133    0.067   16.906    0.000    1.115    0.866
    x6                0.924    0.056   16.391    0.000    0.910    0.832

Variances:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
   .x4                0.382    0.049    7.805    0.000    0.382    0.283
   .x5                0.416    0.059    7.038    0.000    0.416    0.251
   .x6                0.369    0.044    8.367    0.000    0.369    0.308
    text              0.969    0.112    8.640    0.000    1.000    1.000


This model has zero degrees of freedom, which means we need to fix the model identification.

------------------------------------------------------------------------------------------------------------------------

Fix the Zero df Model
Identification is a core component in SEM models and you should have at least one degree of freedom for any model.
Using the text.model from the last exercise, update the model to create a degree of freedom.

You should label two of the coefficient paths to a to set them equal to each other. lavaan and the
HolzingerSwineford1939 dataset have been loaded for you.

* Update the text.model to have x5 and x6 paths both set to a.
* Analyze the model with the cfa() function.
* View the updated model with the summary() function.

# Update the model specification by setting two paths to the label a
text.model <- 'text =~ x4 + a*x5 + a*x6'

# Analyze the model
text.fit <- cfa(model = text.model, data = HolzingerSwineford1939)

# Summarize the model
summary(text.fit, standardized = TRUE, fit.measures = TRUE)


lavaan 0.6-11 ended normally after 14 iterations

  Estimator                                         ML
  Optimization method                           NLMINB
  Number of model parameters                         6
  Number of equality constraints                     1
                                                      
  Number of observations                           301
                                                      
Model Test User Model:
                                                      
  Test statistic                                11.227
  Degrees of freedom                                 1
  P-value (Chi-square)                           0.001

Model Test Baseline Model:

  Test statistic                               497.430
  Degrees of freedom                                 3
  P-value                                        0.000

User Model versus Baseline Model:

  Comparative Fit Index (CFI)                    0.979
  Tucker-Lewis Index (TLI)                       0.938

Loglikelihood and Information Criteria:

  Loglikelihood user model (H0)              -1186.678
  Loglikelihood unrestricted model (H1)      -1181.065
                                                      
  Akaike (AIC)                                2383.357
  Bayesian (BIC)                              2401.892
  Sample-size adjusted Bayesian (BIC)         2386.035

Root Mean Square Error of Approximation:

  RMSEA                                          0.184
  90 Percent confidence interval - lower         0.098
  90 Percent confidence interval - upper         0.288
  P-value RMSEA <= 0.05                          0.007

Standardized Root Mean Square Residual:

  SRMR                                           0.073

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
  text =~                                                               
    x4                1.000                               0.983    0.846
    x5         (a)    1.009    0.054   18.747    0.000    0.992    0.815
    x6         (a)    1.009    0.054   18.747    0.000    0.992    0.866

Variances:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
   .x4                0.383    0.050    7.631    0.000    0.383    0.284
   .x5                0.499    0.054    9.164    0.000    0.499    0.336
   .x6                0.328    0.045    7.285    0.000    0.328    0.250
    text              0.967    0.113    8.585    0.000    1.000    1.000

Our text model is now identified with one degree of freedom and two equal parameter estimates for x5 and x6.

------------------------------------------------------------------------------------------------------------------------

Build a Multi-Factor Model
Another way to improve our text-speed model would be to create a two-factor model with text and speed as latent
variables, rather than split the model into two one-factor models. In this exercise, write the model specification code
for a two-factor model.

If you want to view the dataset to see the columns, you can use head(HolzingerSwineford1939).

* Name your model twofactor.model.
* Use x4, x5, and x6 to create a text latent variable.
* Use x7, x8, and x9 to create a speed latent variable.

# Create a two-factor model of text and speed variables
twofactor.model <- 'text =~ x4 + x5 + x6
                    speed =~ x7 + x8 + x9'

------------------------------------------------------------------------------------------------------------------------

Summarize the Multi-Factor Model
You have now created a two-factor model of the reading comprehension and speeded addition factors. Is that better than
a one-factor model? Use the cfa() and summary() functions on your new two-factor model of the HolzingerSwineford1939
dataset to show the fit indices.

# Previous one-factor model output
summary(text.fit, standardized = TRUE, fit.measures = TRUE)

# Two-factor model specification
twofactor.model <- 'text =~ x4 + x5 + x6
speed =~ x7 + x8 + x9'

# Use cfa() to analyze the model and include data argument
twofactor.fit <- cfa(model = twofactor.model, data = HolzingerSwineford1939)

# Use summary() to view the fitted model
summary(twofactor.fit, standardized = TRUE, fit.measures = TRUE)


lavaan 0.6-11 ended normally after 24 iterations

  Estimator                                         ML
  Optimization method                           NLMINB
  Number of model parameters                        13
                                                      
  Number of observations                           301
                                                      
Model Test User Model:
                                                      
  Test statistic                                14.354
  Degrees of freedom                                 8
  P-value (Chi-square)                           0.073

Model Test Baseline Model:

  Test statistic                               681.336
  Degrees of freedom                                15
  P-value                                        0.000

User Model versus Baseline Model:

  Comparative Fit Index (CFI)                    0.990
  Tucker-Lewis Index (TLI)                       0.982

Loglikelihood and Information Criteria:

  Loglikelihood user model (H0)              -2408.414
  Loglikelihood unrestricted model (H1)      -2401.237
                                                      
  Akaike (AIC)                                4842.828
  Bayesian (BIC)                              4891.021
  Sample-size adjusted Bayesian (BIC)         4849.792

Root Mean Square Error of Approximation:

  RMSEA                                          0.051
  90 Percent confidence interval - lower         0.000
  90 Percent confidence interval - upper         0.093
  P-value RMSEA <= 0.05                          0.425

Standardized Root Mean Square Residual:

  SRMR                                           0.039

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
  text =~                                                               
    x4                1.000                               0.984    0.847
    x5                1.132    0.067   16.954    0.000    1.114    0.865
    x6                0.925    0.056   16.438    0.000    0.911    0.833
  speed =~                                                              
    x7                1.000                               0.674    0.619
    x8                1.150    0.165    6.990    0.000    0.775    0.766
    x9                0.878    0.123    7.166    0.000    0.592    0.587

Covariances:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
  text ~~                                                               
    speed             0.173    0.052    3.331    0.001    0.261    0.261

Variances:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
   .x4                0.382    0.049    7.854    0.000    0.382    0.283
   .x5                0.418    0.059    7.113    0.000    0.418    0.252
   .x6                0.367    0.044    8.374    0.000    0.367    0.307
   .x7                0.729    0.084    8.731    0.000    0.729    0.616
   .x8                0.422    0.084    5.039    0.000    0.422    0.413
   .x9                0.665    0.071    9.383    0.000    0.665    0.655
    text              0.969    0.112    8.647    0.000    1.000    1.000
    speed             0.454    0.096    4.728    0.000    1.000    1.000

In comparing the one- and two-factor models, you should see that the fit indices
are improved in the two-factor model.

------------------------------------------------------------------------------------------------------------------------

Three-Factor Model with Zero Correlation
In this exercise, you will use the Eysenck Personality Inventory dataset from the psychTools library to create a
three-factor model of personality. This inventory includes 57 questions that measure extraversion, neuroticism, 
and lying.

Let's create a three factor model using the latent variables: extraversion, neuroticism, and lying with four manifest
variables on each item. Remember when you create multiple latent variables, these endogenous variables are automatically
correlated. Set the correlation between the extraversion latent variable and neuroticism latent variable to zero, by 
using the ~~ in model specification code.

* Create a lying factor including items V6, V12, V18, and V24.
* Set the correlation between extraversion and neuroticism to zero (in that order).
* Analyze the three-factor model with the cfa() function.
* Summarize the three-factor model and look for the zero-correlation you set in the model specification.

# Load the library and data
library(psych)
data(epi)

# Specify a three-factor model with one correlation set to zero
epi.model <- 'extraversion =~ V1 + V3 + V5 + V8
neuroticism =~ V2 + V4 + V7 + V9
lying =~ V6 + V12 + V18 + V24
extraversion ~~ 0*neuroticism'

# Run the model
epi.fit <- cfa(model = epi.model, data = epi)

# Examine the output 
summary(epi.fit, standardized = TRUE, fit.measures = TRUE)


lavaan 0.6-11 ended normally after 106 iterations

  Estimator                                         ML
  Optimization method                           NLMINB
  Number of model parameters                        26
                                                      
                                                  Used       Total
  Number of observations                          3193        3570
                                                                  
Model Test User Model:
                                                      
  Test statistic                               584.718
  Degrees of freedom                                52
  P-value (Chi-square)                           0.000

Model Test Baseline Model:

  Test statistic                              2196.019
  Degrees of freedom                                66
  P-value                                        0.000

User Model versus Baseline Model:

  Comparative Fit Index (CFI)                    0.750
  Tucker-Lewis Index (TLI)                       0.683

Loglikelihood and Information Criteria:

  Loglikelihood user model (H0)             -23208.145
  Loglikelihood unrestricted model (H1)     -22915.787
                                                      
  Akaike (AIC)                               46468.291
  Bayesian (BIC)                             46626.077
  Sample-size adjusted Bayesian (BIC)        46543.464

Root Mean Square Error of Approximation:

  RMSEA                                          0.057
  90 Percent confidence interval - lower         0.053
  90 Percent confidence interval - upper         0.061
  P-value RMSEA <= 0.05                          0.004

Standardized Root Mean Square Residual:

  SRMR                                           0.058

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
  extraversion =~                                                       
    V1                1.000                               0.052    0.115
    V3                1.360    0.329    4.127    0.000    0.070    0.141
    V5               -2.829    0.554   -5.109    0.000   -0.146   -0.391
    V8                7.315    1.832    3.992    0.000    0.377    0.797
  neuroticism =~                                                        
    V2                1.000                               0.228    0.457
    V4                0.424    0.053    8.004    0.000    0.097    0.196
    V7                1.395    0.093   15.023    0.000    0.318    0.648
    V9                1.205    0.078   15.506    0.000    0.275    0.553
  lying =~                                                              
    V6                1.000                               0.135    0.272
    V12              -0.851    0.132   -6.435    0.000   -0.115   -0.291
    V18              -0.785    0.122   -6.421    0.000   -0.106   -0.289
    V24               1.086    0.161    6.734    0.000    0.147    0.339

Covariances:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
  extraversion ~~                                                       
    neuroticism       0.000                               0.000    0.000
    lying            -0.002    0.001   -3.313    0.001   -0.258   -0.258
  neuroticism ~~                                                        
    lying            -0.014    0.002   -6.867    0.000   -0.469   -0.469

Variances:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
   .V1                0.198    0.005   39.567    0.000    0.198    0.987
   .V3                0.243    0.006   39.278    0.000    0.243    0.980
   .V5                0.118    0.005   23.900    0.000    0.118    0.847
   .V8                0.082    0.026    3.084    0.002    0.082    0.364
   .V2                0.197    0.006   32.516    0.000    0.197    0.791
   .V4                0.235    0.006   38.906    0.000    0.235    0.962
   .V7                0.140    0.007   19.412    0.000    0.140    0.580
   .V9                0.172    0.006   26.591    0.000    0.172    0.694
   .V6                0.228    0.007   34.520    0.000    0.228    0.926
   .V12               0.143    0.004   33.670    0.000    0.143    0.916
   .V18               0.124    0.004   33.753    0.000    0.124    0.917
   .V24               0.166    0.005   31.021    0.000    0.166    0.885
    extraversion      0.003    0.001    2.480    0.013    1.000    1.000
    neuroticism       0.052    0.005   10.010    0.000    1.000    1.000
    lying             0.018    0.004    4.500    0.000    1.000    1.000

In the summary output, you can see the correlation between extraversion and neuroticism is set to zero

------------------------------------------------------------------------------------------------------------------------

Create a Direct Path
In this exercise, edit the epi.model to include a direct regression path between lying and neuroticism. We might expect
that a person's level of neuroticism would predict their level of lying.

You can edit the model specification provided to include this path, and you can use the same model specification code
that you might use when defining a regression in lm.

* Add a new line to the model specification code where lying is predicted by neuroticism.
* Complete the cfa() function to analyze your new model.
* Summarize the new model to see that that output has changed to a regression.

# Load the library and data
library(psych)
data(epi)

# Specify a three-factor model where lying is predicted by neuroticism
epi.model <- 'extraversion =~ V1 + V3 + V5 + V8
neuroticism =~ V2 + V4 + V7 + V9
lying =~ V6 + V12 + V18 + V24
lying ~ neuroticism'

# Run the model
epi.fit <- cfa(model = epi.model, data = epi)

# Examine the output 
summary(epi.fit, standardized = TRUE, fit.measures = TRUE)


lavaan 0.6-11 ended normally after 103 iterations

  Estimator                                         ML
  Optimization method                           NLMINB
  Number of model parameters                        26
                                                      
                                                  Used       Total
  Number of observations                          3193        3570
                                                                  
Model Test User Model:
                                                      
  Test statistic                               534.426
  Degrees of freedom                                52
  P-value (Chi-square)                           0.000

Model Test Baseline Model:

  Test statistic                              2196.019
  Degrees of freedom                                66
  P-value                                        0.000

User Model versus Baseline Model:

  Comparative Fit Index (CFI)                    0.774
  Tucker-Lewis Index (TLI)                       0.713

Loglikelihood and Information Criteria:

  Loglikelihood user model (H0)             -23183.000
  Loglikelihood unrestricted model (H1)     -22915.787
                                                      
  Akaike (AIC)                               46417.999
  Bayesian (BIC)                             46575.786
  Sample-size adjusted Bayesian (BIC)        46493.173

Root Mean Square Error of Approximation:

  RMSEA                                          0.054
  90 Percent confidence interval - lower         0.050
  90 Percent confidence interval - upper         0.058
  P-value RMSEA <= 0.05                          0.058

Standardized Root Mean Square Residual:

  SRMR                                           0.053

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
  extraversion =~                                                       
    V1                1.000                               0.052    0.115
    V3                1.135    0.268    4.230    0.000    0.059    0.118
    V5               -2.497    0.443   -5.638    0.000   -0.129   -0.346
    V8                8.223    2.008    4.096    0.000    0.425    0.898
  neuroticism =~                                                        
    V2                1.000                               0.223    0.447
    V4                0.462    0.054    8.493    0.000    0.103    0.209
    V7                1.435    0.093   15.368    0.000    0.320    0.652
    V9                1.214    0.078   15.570    0.000    0.271    0.545
  lying =~                                                              
    V6                1.000                               0.125    0.252
    V12              -0.943    0.150   -6.274    0.000   -0.118   -0.298
    V18              -0.905    0.143   -6.339    0.000   -0.113   -0.308
    V24               1.187    0.182    6.509    0.000    0.148    0.342

Regressions:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
  lying ~                                                               
    neuroticism      -0.298    0.043   -6.943    0.000   -0.532   -0.532

Covariances:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
  extraversion ~~                                                       
    neuroticism       0.003    0.001    3.761    0.000    0.240    0.240

Variances:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
   .V1                0.198    0.005   39.671    0.000    0.198    0.987
   .V3                0.244    0.006   39.651    0.000    0.244    0.986
   .V5                0.123    0.004   28.256    0.000    0.123    0.881
   .V8                0.043    0.033    1.302    0.193    0.043    0.193
   .V2                0.200    0.006   33.262    0.000    0.200    0.800
   .V4                0.233    0.006   38.804    0.000    0.233    0.956
   .V7                0.139    0.007   20.087    0.000    0.139    0.575
   .V9                0.174    0.006   27.907    0.000    0.174    0.703
   .V6                0.231    0.007   35.398    0.000    0.231    0.936
   .V12               0.143    0.004   33.349    0.000    0.143    0.911
   .V18               0.122    0.004   32.825    0.000    0.122    0.905
   .V24               0.166    0.005   30.854    0.000    0.166    0.883
    extraversion      0.003    0.001    2.643    0.008    1.000    1.000
    neuroticism       0.050    0.005    9.947    0.000    1.000    1.000
   .lying             0.011    0.003    3.970    0.000    0.717    0.717

You can see that the correlation between lying and neuroticism is now a direct path listed under 
regressions in the output

------------------------------------------------------------------------------------------------------------------------

Check Model Variance
In order to evaluate your three-factor model of the epi, you can examine the variance of the manifest variables to 
check for potential problems with the model. Very large variances can indicate potential issues; however, this value 
should be compared to the original scale of the data.

Use the var() function on V1 to compare the variance of the original manifest variable to the estimated variance in your
summary output. The libraries, model, and datasets have been loaded for you.

* Use var() on V1 to calculate the variance of the original manifest variable in the epi dataset.
* Compare this value to the V1 variance estimate of the model.

# Run the model
epi.fit <- cfa(model = epi.model, data = epi)

# Examine the output 
summary(epi.fit, standardized = TRUE, fit.measures = TRUE)

# Calculate the variance of V1 in the epi data
var(epi$V1)

[1] 0.2017972


lavaan 0.6-11 ended normally after 108 iterations

  Estimator                                         ML
  Optimization method                           NLMINB
  Number of model parameters                        27
                                                      
  Number of observations                          2897
                                                      
Model Test User Model:
                                                      
  Test statistic                               462.095
  Degrees of freedom                                51
  P-value (Chi-square)                           0.000

Model Test Baseline Model:

  Test statistic                              1994.632
  Degrees of freedom                                66
  P-value                                        0.000

User Model versus Baseline Model:

  Comparative Fit Index (CFI)                    0.787
  Tucker-Lewis Index (TLI)                       0.724

Loglikelihood and Information Criteria:

  Loglikelihood user model (H0)             -21035.420
  Loglikelihood unrestricted model (H1)     -20804.372
                                                      
  Akaike (AIC)                               42124.840
  Bayesian (BIC)                             42286.068
  Sample-size adjusted Bayesian (BIC)        42200.280

Root Mean Square Error of Approximation:

  RMSEA                                          0.053
  90 Percent confidence interval - lower         0.048
  90 Percent confidence interval - upper         0.057
  P-value RMSEA <= 0.05                          0.147

Standardized Root Mean Square Residual:

  SRMR                                           0.052

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
  extraversion =~                                                       
    V1                1.000                               0.052    0.115
    V3                1.299    0.308    4.217    0.000    0.067    0.135
    V5               -2.614    0.494   -5.295    0.000   -0.135   -0.359
    V8                8.108    1.975    4.105    0.000    0.418    0.884
  neuroticism =~                                                        
    V2                1.000                               0.225    0.450
    V4                0.443    0.056    7.871    0.000    0.100    0.202
    V7                1.439    0.099   14.501    0.000    0.323    0.659
    V9                1.197    0.081   14.848    0.000    0.269    0.541
  lying =~                                                              
    V6                1.000                               0.140    0.281
    V12              -0.817    0.129   -6.341    0.000   -0.114   -0.287
    V18              -0.782    0.122   -6.424    0.000   -0.109   -0.297
    V24               0.995    0.151    6.592    0.000    0.139    0.321

Covariances:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
  extraversion ~~                                                       
    neuroticism       0.002    0.001    3.629    0.000    0.210    0.210
    lying            -0.002    0.001   -3.402    0.001   -0.283   -0.283
  neuroticism ~~                                                        
    lying            -0.016    0.002   -6.998    0.000   -0.521   -0.521

Variances:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
   .V1                0.199    0.005   37.811    0.000    0.199    0.987
   .V3                0.243    0.006   37.654    0.000    0.243    0.982
   .V5                0.123    0.005   27.226    0.000    0.123    0.871
   .V8                0.049    0.030    1.615    0.106    0.049    0.218
   .V2                0.199    0.006   31.450    0.000    0.199    0.797
   .V4                0.234    0.006   37.030    0.000    0.234    0.959
   .V7                0.136    0.007   18.273    0.000    0.136    0.566
   .V9                0.174    0.007   26.548    0.000    0.174    0.707
   .V6                0.227    0.007   32.757    0.000    0.227    0.921
   .V12               0.145    0.004   32.495    0.000    0.145    0.918
   .V18               0.123    0.004   32.041    0.000    0.123    0.912
   .V24               0.168    0.005   30.875    0.000    0.168    0.897
    extraversion      0.003    0.001    2.541    0.011    1.000    1.000
    neuroticism       0.051    0.005    9.508    0.000    1.000    1.000
    lying             0.019    0.004    4.491    0.000    1.000    1.000

You can see that your variance from the model (0.199) is very similar to the real variance (0.201)
which indicates our model does not have variance issues. 

------------------------------------------------------------------------------------------------------------------------

Examine Modification Indices
The fit indices for our epi.model are low (in the .70s) for CFI and TLI. You can use modification indices to find 
potential parameters (paths) to add to the model specification to improve model fit.

Examine the modification indices for the epi.model and add the path with the largest mi to your model. The libraries and
model have been loaded for you. After you edit your model, rerun the model summary to examine the change in fit indices.

* Use modificationindices() on the epi.fit to find the mi values.
* Use sort = TRUE to see the highest values first.
* Update the model specification code to include the largest mi value.
* Reanalyze the model with the updated model specification to see how your fit indices improved.
* Use epi.model2 from the previous step as your model name.

# Original model summary
summary(epi.fit, standardized = TRUE, fit.measures = TRUE)

# Examine the modification indices
modificationindices(epi.fit, sort = TRUE)

             lhs op rhs      mi    epc sepc.lv sepc.all sepc.nox
40   neuroticism =~  V3 168.998 -0.691  -0.155   -0.312   -0.312
39   neuroticism =~  V1 122.899  0.531   0.119    0.266    0.266
48         lying =~  V3 122.762  1.315   0.180    0.361    0.361
70            V3 ~~  V7  76.778 -0.034  -0.034   -0.185   -0.185
58            V1 ~~  V2  76.542  0.032   0.032    0.163    0.163
47         lying =~  V1  60.229 -0.830  -0.114   -0.253   -0.253
59            V1 ~~  V4  27.315  0.020   0.020    0.093    0.093
67            V3 ~~  V8  20.964  0.042   0.042    0.366    0.366
75            V3 ~~ V24  19.003  0.016   0.016    0.081    0.081
32  extraversion =~  V4  18.508  0.831   0.045    0.091    0.091
103           V4 ~~ V12  17.706  0.014   0.014    0.078    0.078
113           V9 ~~ V18  15.994  0.012   0.012    0.083    0.083
51         lying =~  V2  15.368  0.613   0.084    0.168    0.168
52         lying =~  V4  13.179 -0.552  -0.076   -0.153   -0.153
35  extraversion =~  V6  11.524 -0.792  -0.043   -0.086   -0.086
57            V1 ~~  V8  10.574 -0.027  -0.027   -0.261   -0.261
86            V8 ~~  V4   9.716  0.012   0.012    0.106    0.106
68            V3 ~~  V2   9.516 -0.013  -0.013   -0.058   -0.058
66            V3 ~~  V5   9.467 -0.010  -0.010   -0.061   -0.061
116           V6 ~~ V18   9.309  0.011   0.011    0.066    0.066
45   neuroticism =~ V18   8.918  0.199   0.045    0.122    0.122
64            V1 ~~ V18   8.768  0.008   0.008    0.054    0.054
74            V3 ~~ V18   8.347 -0.009  -0.009   -0.053   -0.053
84            V5 ~~ V24   8.334  0.008   0.008    0.056    0.056
46   neuroticism =~ V24   7.824  0.240   0.054    0.125    0.125
89            V8 ~~  V6   7.197 -0.011  -0.011   -0.103   -0.103
71            V3 ~~  V9   6.986 -0.011  -0.011   -0.051   -0.051
99            V2 ~~ V24   6.977  0.010   0.010    0.053    0.053
111           V9 ~~  V6   6.588  0.011   0.011    0.053    0.053
61            V1 ~~  V9   6.361  0.009   0.009    0.049    0.049
76            V5 ~~  V8   5.668  0.062   0.062    0.760    0.760
87            V8 ~~  V7   5.270  0.009   0.009    0.109    0.109
117           V6 ~~ V24   4.652  0.010   0.010    0.050    0.050
107           V7 ~~  V6   4.451 -0.009  -0.009   -0.049   -0.049
60            V1 ~~  V7   4.238  0.007   0.007    0.044    0.044
69            V3 ~~  V4   4.212  0.009   0.009    0.037    0.037
31  extraversion =~  V2   4.066 -0.393  -0.021   -0.042   -0.042
56            V1 ~~  V5   3.741  0.006   0.006    0.038    0.038
85            V8 ~~  V2   3.729 -0.007  -0.007   -0.071   -0.071
100           V4 ~~  V7   3.174 -0.008  -0.008   -0.047   -0.047
96            V2 ~~  V6   2.705  0.007   0.007    0.032    0.032
88            V8 ~~  V9   2.438 -0.006  -0.006   -0.062   -0.062
37  extraversion =~ V18   2.373 -0.270  -0.015   -0.040   -0.040
95            V2 ~~  V9   2.309  0.009   0.009    0.048    0.048
106           V7 ~~  V9   1.956 -0.013  -0.013   -0.084   -0.084
83            V5 ~~ V18   1.909  0.003   0.003    0.026    0.026
34  extraversion =~  V9   1.852 -0.272  -0.015   -0.029   -0.029
92            V8 ~~ V24   1.837  0.005   0.005    0.056    0.056
94            V2 ~~  V7   1.815  0.010   0.010    0.058    0.058
49         lying =~  V5   1.688  0.155   0.021    0.057    0.057
33  extraversion =~  V7   1.661  0.281   0.015    0.031    0.031
55            V1 ~~  V3   1.304 -0.004  -0.004   -0.020   -0.020
38  extraversion =~ V24   1.183  0.239   0.013    0.030    0.030
114           V9 ~~ V24   1.131 -0.004  -0.004   -0.023   -0.023
81            V5 ~~  V6   1.131  0.003   0.003    0.020    0.020
42   neuroticism =~  V8   1.060  0.155   0.035    0.074    0.074
110           V7 ~~ V24   1.032  0.004   0.004    0.025    0.025
120          V18 ~~ V24   1.003 -0.003  -0.003   -0.024   -0.024
63            V1 ~~ V12   0.997  0.003   0.003    0.018    0.018
65            V1 ~~ V24   0.969  0.003   0.003    0.018    0.018
91            V8 ~~ V18   0.954 -0.003  -0.003   -0.038   -0.038
101           V4 ~~  V9   0.932 -0.004  -0.004   -0.021   -0.021
98            V2 ~~ V18   0.909 -0.003  -0.003   -0.019   -0.019
115           V6 ~~ V12   0.906  0.004   0.004    0.021    0.021
53         lying =~  V7   0.903 -0.175  -0.024   -0.049   -0.049
54         lying =~  V9   0.892 -0.156  -0.021   -0.043   -0.043
77            V5 ~~  V2   0.700  0.002   0.002    0.016    0.016
43   neuroticism =~  V6   0.642  0.070   0.016    0.032    0.032
44   neuroticism =~ V12   0.584  0.055   0.012    0.031    0.031
72            V3 ~~  V6   0.561 -0.003  -0.003   -0.014   -0.014
62            V1 ~~  V6   0.557  0.003   0.003    0.014    0.014
119          V12 ~~ V24   0.520 -0.003  -0.003   -0.017   -0.017
78            V5 ~~  V4   0.379  0.002   0.002    0.011    0.011
90            V8 ~~ V12   0.325 -0.002  -0.002   -0.022   -0.022
36  extraversion =~ V12   0.267 -0.098  -0.005   -0.013   -0.013
105           V4 ~~ V24   0.228  0.002   0.002    0.009    0.009
93            V2 ~~  V4   0.175  0.002   0.002    0.008    0.008
79            V5 ~~  V7   0.141  0.001   0.001    0.008    0.008
80            V5 ~~  V9   0.119 -0.001  -0.001   -0.007   -0.007
118          V12 ~~ V18   0.118 -0.001  -0.001   -0.008   -0.008
41   neuroticism =~  V5   0.116  0.017   0.004    0.010    0.010
102           V4 ~~  V6   0.106 -0.001  -0.001   -0.006   -0.006
97            V2 ~~ V12   0.104 -0.001  -0.001   -0.006   -0.006
73            V3 ~~ V12   0.077 -0.001  -0.001   -0.005   -0.005
108           V7 ~~ V12   0.054 -0.001  -0.001   -0.005   -0.005
112           V9 ~~ V12   0.022  0.000   0.000   -0.003   -0.003
82            V5 ~~ V12   0.013  0.000   0.000    0.002    0.002
50         lying =~  V8   0.012 -0.041  -0.006   -0.012   -0.012
104           V4 ~~ V18   0.012  0.000   0.000   -0.002   -0.002
109           V7 ~~ V18   0.003  0.000   0.000    0.001    0.001


# Edit the model specification
epi.model2 <- 'extraversion =~ V1 + V3 + V5 + V8
neuroticism =~ V2 + V4 + V7 + V9
lying =~ V6 + V12 + V18 + V24
neuroticism =~ V3'

# Reanalyze the model
epi.fit <- cfa(model = epi.model2, data = epi)

# Summarize the updated model
summary(epi.fit, standardized = TRUE, fit.measures = TRUE)


lavaan 0.6-11 ended normally after 116 iterations

  Estimator                                         ML
  Optimization method                           NLMINB
  Number of model parameters                        28
                                                      
                                                  Used       Total
  Number of observations                          3193        3570
                                                                  
Model Test User Model:
                                                      
  Test statistic                               332.891
  Degrees of freedom                                50
  P-value (Chi-square)                           0.000

Model Test Baseline Model:

  Test statistic                              2196.019
  Degrees of freedom                                66
  P-value                                        0.000

User Model versus Baseline Model:

  Comparative Fit Index (CFI)                    0.867
  Tucker-Lewis Index (TLI)                       0.825

Loglikelihood and Information Criteria:

  Loglikelihood user model (H0)             -23082.232
  Loglikelihood unrestricted model (H1)     -22915.787
                                                      
  Akaike (AIC)                               46220.465
  Bayesian (BIC)                             46390.389
  Sample-size adjusted Bayesian (BIC)        46301.421

Root Mean Square Error of Approximation:

  RMSEA                                          0.042
  90 Percent confidence interval - lower         0.038
  90 Percent confidence interval - upper         0.046
  P-value RMSEA <= 0.05                          0.999

Standardized Root Mean Square Residual:

  SRMR                                           0.040

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
  extraversion =~                                                       
    V1                1.000                               0.068    0.152
    V3                1.798    0.325    5.532    0.000    0.123    0.246
    V5               -2.268    0.360   -6.291    0.000   -0.155   -0.414
    V8                5.077    0.887    5.725    0.000    0.346    0.732
  neuroticism =~                                                        
    V2                1.000                               0.222    0.445
    V4                0.432    0.053    8.134    0.000    0.096    0.194
    V7                1.493    0.093   16.025    0.000    0.331    0.675
    V9                1.186    0.074   15.938    0.000    0.263    0.530
  lying =~                                                              
    V6                1.000                               0.135    0.272
    V12              -0.851    0.127   -6.699    0.000   -0.115   -0.290
    V18              -0.799    0.119   -6.728    0.000   -0.108   -0.294
    V24               1.115    0.157    7.087    0.000    0.151    0.347
  neuroticism =~                                                        
    V3               -0.732    0.066  -11.074    0.000   -0.163   -0.327

Covariances:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
  extraversion ~~                                                       
    neuroticism       0.004    0.001    4.953    0.000    0.283    0.283
    lying            -0.003    0.001   -4.380    0.000   -0.346   -0.346
  neuroticism ~~                                                        
    lying            -0.016    0.002   -7.337    0.000   -0.521   -0.521

Variances:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
   .V1                0.196    0.005   39.250    0.000    0.196    0.977
   .V3                0.217    0.006   34.642    0.000    0.217    0.878
   .V5                0.116    0.004   29.066    0.000    0.116    0.828
   .V8                0.104    0.014    7.603    0.000    0.104    0.465
   .V2                0.200    0.006   33.875    0.000    0.200    0.802
   .V4                0.235    0.006   39.046    0.000    0.235    0.962
   .V7                0.131    0.007   19.577    0.000    0.131    0.544
   .V9                0.178    0.006   29.830    0.000    0.178    0.720
   .V6                0.228    0.007   34.969    0.000    0.228    0.926
   .V12               0.144    0.004   34.186    0.000    0.144    0.916
   .V18               0.123    0.004   34.035    0.000    0.123    0.914
   .V24               0.166    0.005   31.188    0.000    0.166    0.879
    extraversion      0.005    0.001    3.265    0.001    1.000    1.000
    neuroticism       0.049    0.005   10.127    0.000    1.000    1.000
    lying             0.018    0.004    4.651    0.000    1.000    1.000

Your fit indices should improve to the .80s by including this one extra parameter to the model.

------------------------------------------------------------------------------------------------------------------------

Compare Two Models
In the last exercises, you created two models of the epi, and these models have been loaded for you.

The original model epi.model and the updated model with the modified path epi.model1 can now be compared using the anova()
function to determine if the change in fit indices was a large change. We can use the anova() function because these models
are nested, which means they are the same manifest variables with different parameters.

* Analyze both models with the cfa() function and the epi dataset.
* Use the anova() function to compare these models.

# Analyze the original model
epi.fit <- cfa(model = epi.model, data = epi)

# Analyze the updated model
epi.fit1 <- cfa(model = epi.model1, data = epi)

# Compare those models
anova(epi.fit, epi.fit1)


Chi-Squared Difference Test

         Df   AIC   BIC  Chisq Chisq diff Df diff Pr(>Chisq)    
epi.fit1 50 46220 46390 332.89                                  
epi.fit  51 46396 46560 510.40     177.51       1  < 2.2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

The updated model appears better, as the chi-square difference test is significant.

------------------------------------------------------------------------------------------------------------------------

Select Specific Fit Indices
You can also compare models by using the AIC or ECVI fit indices, rather than the anova() function. These fit indices are
very useful if your models include different manifest variables. When comparing sets of AIC or ECVI values, the best model
would have the smallest fit index. The saved models from the previous exercise have been loaded for you.

* Use fitmeasures() to calculate the fit indices for each model separately.
* Select only the aic and ecvi fit indices to shorten the output.

# Analyze the original model
epi.fit <- cfa(model = epi.model, data = epi)

# Find the fit indices for the original model
fitmeasures(epi.fit, c("aic", "ecvi"))

      aic      ecvi 
46395.976     0.177 

# Analyze the updated model
epi.fit1 <- cfa(model = epi.model1, data = epi)

# Find the fit indices for the updated model
fitmeasures(epi.fit1, c("aic", "ecvi"))

      aic      ecvi 
46220.465     0.122

For both AIC and ECVI, the updated model included the smaller fit indices and would be considered the better model.