#Loading library
library(lavaan)
## This is lavaan 0.6-15
## lavaan is FREE software! Please report any bugs.
# Read in data reg1 example

modelData <- read.table("reg1.dat.csv", header = TRUE)
View(modelData)
# Creating model regression example 1

model1<-"
! regressions 
   E=~1.0*Y_c
   Y_c ~ b*X_c
! residuals, variances and covariances
   X_c ~~ VAR_X_c*X_c
   E ~~ VAR_E*E
! means
   X_c~Mu_X*1
   Y_c~a*1
   E~0*1;
";
result<-lavaan(model1, data=modelData, fixed.x=FALSE, missing="FIML");
summary(result, fit.measures=TRUE)
## lavaan 0.6.15 ended normally after 13 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         5
## 
##   Number of observations                            10
##   Number of missing patterns                         1
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Model Test Baseline Model:
## 
##   Test statistic                                 0.833
##   Degrees of freedom                                 1
##   P-value                                        0.361
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000
##   Tucker-Lewis Index (TLI)                       1.000
##                                                       
##   Robust Comparative Fit Index (CFI)             1.000
##   Robust Tucker-Lewis Index (TLI)                1.000
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)                -50.804
##   Loglikelihood unrestricted model (H1)        -50.804
##                                                       
##   Akaike (AIC)                                 111.609
##   Bayesian (BIC)                               113.122
##   Sample-size adjusted Bayesian (SABIC)         98.143
## 
## 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 H_0: RMSEA <= 0.050                       NA
##   P-value H_0: RMSEA >= 0.080                       NA
##                                                       
##   Robust RMSEA                                   0.000
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.000
##   P-value H_0: Robust RMSEA <= 0.050                NA
##   P-value H_0: Robust RMSEA >= 0.080                NA
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.000
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Observed
##   Observed information based on                Hessian
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   E =~                                                
##     Y_c               1.000                           
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   Y_c ~                                               
##     X_c        (b)    0.536    0.575    0.932    0.351
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     X_c     (Mu_X)    0.000    0.719    0.000    1.000
##    .Y_c        (a)   -0.009    1.309   -0.007    0.994
##     E                 0.000                           
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     X_c    (VAR_X)    5.175    2.314    2.236    0.025
##     E      (VAR_E)   17.139    7.665    2.236    0.025
##    .Y_c               0.000
# Creating model regression example 2
model2<-"
! regressions 
   Y_c ~ b*X_c
! residuals, variances and covariances
   X_c ~~ VAR_E*X_c
   Y_c ~~ VAR_Y_c*Y_c
! means
   X_c~Mu_X*1
   Y_c~a*1
";
result<-lavaan(model2, data=modelData, fixed.x=FALSE, missing="FIML");
summary(result, fit.measures=TRUE)
## lavaan 0.6.15 ended normally after 8 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         5
## 
##   Number of observations                            10
##   Number of missing patterns                         1
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Model Test Baseline Model:
## 
##   Test statistic                                 0.833
##   Degrees of freedom                                 1
##   P-value                                        0.361
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000
##   Tucker-Lewis Index (TLI)                       1.000
##                                                       
##   Robust Comparative Fit Index (CFI)             1.000
##   Robust Tucker-Lewis Index (TLI)                1.000
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)                -50.804
##   Loglikelihood unrestricted model (H1)        -50.804
##                                                       
##   Akaike (AIC)                                 111.609
##   Bayesian (BIC)                               113.122
##   Sample-size adjusted Bayesian (SABIC)         98.143
## 
## 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 H_0: RMSEA <= 0.050                       NA
##   P-value H_0: RMSEA >= 0.080                       NA
##                                                       
##   Robust RMSEA                                   0.000
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.000
##   P-value H_0: Robust RMSEA <= 0.050                NA
##   P-value H_0: Robust RMSEA >= 0.080                NA
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.000
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Observed
##   Observed information based on                Hessian
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   Y_c ~                                               
##     X_c        (b)    0.536    0.575    0.932    0.351
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     X_c     (Mu_X)    0.000    0.719    0.000    1.000
##    .Y_c        (a)   -0.009    1.309   -0.007    0.994
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     X_c    (VAR_E)    5.175    2.314    2.236    0.025
##    .Y_c    (VAR_Y)   17.139    7.665    2.236    0.025
#variance get picked up by the Y variable 
# Read in new data: ModelData1

ModelData1 <- read.csv("DataModel1.csv", header = TRUE, sep = ",")
View(ModelData1)
# Path Model 1 in Lavaan example

model3 <- "
! regressions
  C ~ A
  C ~ B
  C ~ X

! Covariances
  A ~~ B

! Variances
  C ~~ C
  A ~~ A
  B ~~ B
  X ~~ X;
";
#we are estimating all the parameters 
#fit.measures give you all the fix indices 
#goodness of fit - comparing implied and observed  -  do not want the p value to be significantly different
  #want it to match #chi-square is sensitive to sample size .. 
result1 <- lavaan(model3, data = ModelData1, fixed.x = FALSE, missing = "FIML");
## Warning in lav_partable_check(lavpartable, categorical = lavoptions$.categorical, : lavaan WARNING: automatically added intercepts are set to zero:
##     [C A B X]
summary(result1, fit.measures = TRUE)
## Warning in lav_partable_check(lavpartable, categorical = lavoptions$.categorical, : lavaan WARNING: automatically added intercepts are set to zero:
##     [C A B X]

## Warning in lav_partable_check(lavpartable, categorical = lavoptions$.categorical, : lavaan WARNING: automatically added intercepts are set to zero:
##     [C A B X]
## lavaan 0.6.15 ended normally after 27 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         8
## 
##   Number of observations                           207
##   Number of missing patterns                         1
## 
## Model Test User Model:
##                                                       
##   Test statistic                               566.164
##   Degrees of freedom                                 6
##   P-value (Chi-square)                           0.000
## 
## Model Test Baseline Model:
## 
##   Test statistic                                 7.970
##   Degrees of freedom                                 6
##   P-value                                        0.240
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.000
##   Tucker-Lewis Index (TLI)                    -283.301
##                                                       
##   Robust Comparative Fit Index (CFI)             0.000
##   Robust Tucker-Lewis Index (TLI)             -283.301
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -1020.728
##   Loglikelihood unrestricted model (H1)       -737.647
##                                                       
##   Akaike (AIC)                                2057.457
##   Bayesian (BIC)                              2084.119
##   Sample-size adjusted Bayesian (SABIC)       2058.771
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.672
##   90 Percent confidence interval - lower         0.625
##   90 Percent confidence interval - upper         0.719
##   P-value H_0: RMSEA <= 0.050                    0.000
##   P-value H_0: RMSEA >= 0.080                    1.000
##                                                       
##   Robust RMSEA                                   0.672
##   90 Percent confidence interval - lower         0.625
##   90 Percent confidence interval - upper         0.719
##   P-value H_0: Robust RMSEA <= 0.050             0.000
##   P-value H_0: Robust RMSEA >= 0.080             1.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           3.598
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Observed
##   Observed information based on                Hessian
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   C ~                                                 
##     A                 0.012    0.021    0.585    0.559
##     B                -0.319    0.013  -25.187    0.000
##     X                 0.005    0.017    0.306    0.760
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   A ~~                                                
##     B                -0.069    0.103   -0.668    0.504
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .C                 0.000                           
##     A                 0.000                           
##     B                 0.000                           
##     X                 0.000                           
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .C                 0.079    0.008   10.174    0.000
##     A                 0.914    0.090   10.174    0.000
##     B                 2.419    0.238   10.173    0.000
##     X                 1.295    0.127   10.173    0.000
# Read in new data: ModelData2

ModelData2 <- read.csv("DataModel2.csv", header = TRUE, sep = ",")
View(ModelData2)
# Path model example 2
#regressions are direct path 
model4 <- "
! regressions
  A ~ U
  A ~ T
  B ~ T
  B ~ V

! variances
  A ~~ A
  U ~~ U
  T ~~ T
  B ~~ B
  V ~~ V;
";
result2 <- lavaan(model4, data = ModelData2, fixed.x = FALSE, missing = "FIML");
## Warning in lav_partable_check(lavpartable, categorical = lavoptions$.categorical, : lavaan WARNING: automatically added intercepts are set to zero:
##     [A B U T V]
summary(result2, fit.measures = TRUE)
## Warning in lav_partable_check(lavpartable, categorical = lavoptions$.categorical, : lavaan WARNING: automatically added intercepts are set to zero:
##     [A B U T V]

## Warning in lav_partable_check(lavpartable, categorical = lavoptions$.categorical, : lavaan WARNING: automatically added intercepts are set to zero:
##     [A B U T V]
## lavaan 0.6.15 ended normally after 31 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         9
## 
##   Number of observations                           207
##   Number of missing patterns                         1
## 
## Model Test User Model:
##                                                       
##   Test statistic                               706.447
##   Degrees of freedom                                11
##   P-value (Chi-square)                           0.000
## 
## Model Test Baseline Model:
## 
##   Test statistic                                18.106
##   Degrees of freedom                                10
##   P-value                                        0.053
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.000
##   Tucker-Lewis Index (TLI)                     -76.999
##                                                       
##   Robust Comparative Fit Index (CFI)             0.000
##   Robust Tucker-Lewis Index (TLI)              -76.999
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -1093.331
##   Loglikelihood unrestricted model (H1)       -740.108
##                                                       
##   Akaike (AIC)                                2204.662
##   Bayesian (BIC)                              2234.657
##   Sample-size adjusted Bayesian (SABIC)       2206.141
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.553
##   90 Percent confidence interval - lower         0.518
##   90 Percent confidence interval - upper         0.588
##   P-value H_0: RMSEA <= 0.050                    0.000
##   P-value H_0: RMSEA >= 0.080                    1.000
##                                                       
##   Robust RMSEA                                   0.553
##   90 Percent confidence interval - lower         0.518
##   90 Percent confidence interval - upper         0.588
##   P-value H_0: Robust RMSEA <= 0.050             0.000
##   P-value H_0: Robust RMSEA >= 0.080             1.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           2.640
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Observed
##   Observed information based on                Hessian
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   A ~                                                 
##     U                 0.023    0.064    0.361    0.718
##     T                -2.403    0.106  -22.710    0.000
##   B ~                                                 
##     T                -0.464    0.185   -2.507    0.012
##     V                -0.265    0.367   -0.723    0.470
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .A                 0.000                           
##    .B                 0.000                           
##     U                 0.000                           
##     T                 0.000                           
##     V                 0.000                           
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .A                 0.760    0.075   10.173    0.000
##     U                 0.919    0.090   10.174    0.000
##     T                 0.334    0.033   10.173    0.000
##    .B                 1.342    0.132   10.174    0.000
##     V                 0.085    0.008   10.173    0.000