# ===============================
# PAQUETES
# ===============================
library(lavaan)
## This is lavaan 0.6-21
## lavaan is FREE software! Please report any bugs.
library(lavaanPlot)
library(cSEM)
## 
## Adjuntando el paquete: 'cSEM'
## The following object is masked from 'package:lavaan':
## 
##     predict
## The following object is masked from 'package:stats':
## 
##     predict
## The following object is masked from 'package:grDevices':
## 
##     savePlot
# ===============================
# MODELO 1 - HOLZINGER
# ===============================
bd <- HolzingerSwineford1939

modelo <- '
visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed =~ x7 + x8 + x9
'

fit <- cfa(modelo, data = bd)
summary(fit, fit.measures = TRUE, standardized = TRUE)
## lavaan 0.6-21 ended normally after 35 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        21
## 
##   Number of observations                           301
## 
## Model Test User Model:
##                                                       
##   Test statistic                                85.306
##   Degrees of freedom                                24
##   P-value (Chi-square)                           0.000
## 
## Model Test Baseline Model:
## 
##   Test statistic                               918.852
##   Degrees of freedom                                36
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.931
##   Tucker-Lewis Index (TLI)                       0.896
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -3737.745
##   Loglikelihood unrestricted model (H1)      -3695.092
##                                                       
##   Akaike (AIC)                                7517.490
##   Bayesian (BIC)                              7595.339
##   Sample-size adjusted Bayesian (SABIC)       7528.739
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.092
##   90 Percent confidence interval - lower         0.071
##   90 Percent confidence interval - upper         0.114
##   P-value H_0: RMSEA <= 0.050                    0.001
##   P-value H_0: RMSEA >= 0.080                    0.840
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.065
## 
## 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
##   visual =~                                                             
##     x1                1.000                               0.900    0.772
##     x2                0.554    0.100    5.554    0.000    0.498    0.424
##     x3                0.729    0.109    6.685    0.000    0.656    0.581
##   textual =~                                                            
##     x4                1.000                               0.990    0.852
##     x5                1.113    0.065   17.014    0.000    1.102    0.855
##     x6                0.926    0.055   16.703    0.000    0.917    0.838
##   speed =~                                                              
##     x7                1.000                               0.619    0.570
##     x8                1.180    0.165    7.152    0.000    0.731    0.723
##     x9                1.082    0.151    7.155    0.000    0.670    0.665
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   visual ~~                                                             
##     textual           0.408    0.074    5.552    0.000    0.459    0.459
##     speed             0.262    0.056    4.660    0.000    0.471    0.471
##   textual ~~                                                            
##     speed             0.173    0.049    3.518    0.000    0.283    0.283
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .x1                0.549    0.114    4.833    0.000    0.549    0.404
##    .x2                1.134    0.102   11.146    0.000    1.134    0.821
##    .x3                0.844    0.091    9.317    0.000    0.844    0.662
##    .x4                0.371    0.048    7.779    0.000    0.371    0.275
##    .x5                0.446    0.058    7.642    0.000    0.446    0.269
##    .x6                0.356    0.043    8.277    0.000    0.356    0.298
##    .x7                0.799    0.081    9.823    0.000    0.799    0.676
##    .x8                0.488    0.074    6.573    0.000    0.488    0.477
##    .x9                0.566    0.071    8.003    0.000    0.566    0.558
##     visual            0.809    0.145    5.564    0.000    1.000    1.000
##     textual           0.979    0.112    8.737    0.000    1.000    1.000
##     speed             0.384    0.086    4.451    0.000    1.000    1.000
lavaanPlot(model = fit, coefs = TRUE, covs = TRUE)
# ===============================
# MODELO 2 - PoliticalDemocracy (CFA)
# ===============================
data("PoliticalDemocracy")
bd2 <- PoliticalDemocracy

modelo2 <- '
democracy_1960 =~ y1 + y2 + y3 + y4
democracy_1965 =~ y5 + y6 + y7 + y8
industrialization_1960 =~ x1 + x2 + x3
'

fit2 <- cfa(modelo2, data = bd2)
summary(fit2, fit.measures = TRUE, standardized = TRUE)
## lavaan 0.6-21 ended normally after 47 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        25
## 
##   Number of observations                            75
## 
## Model Test User Model:
##                                                       
##   Test statistic                                72.462
##   Degrees of freedom                                41
##   P-value (Chi-square)                           0.002
## 
## Model Test Baseline Model:
## 
##   Test statistic                               730.654
##   Degrees of freedom                                55
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.953
##   Tucker-Lewis Index (TLI)                       0.938
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -1564.959
##   Loglikelihood unrestricted model (H1)      -1528.728
##                                                       
##   Akaike (AIC)                                3179.918
##   Bayesian (BIC)                              3237.855
##   Sample-size adjusted Bayesian (SABIC)       3159.062
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.101
##   90 Percent confidence interval - lower         0.061
##   90 Percent confidence interval - upper         0.139
##   P-value H_0: RMSEA <= 0.050                    0.021
##   P-value H_0: RMSEA >= 0.080                    0.827
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.055
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                             Estimate  Std.Err  z-value  P(>|z|)   Std.lv
##   democracy_1960 =~                                                     
##     y1                         1.000                               2.201
##     y2                         1.354    0.175    7.755    0.000    2.980
##     y3                         1.044    0.150    6.961    0.000    2.298
##     y4                         1.300    0.138    9.412    0.000    2.860
##   democracy_1965 =~                                                     
##     y5                         1.000                               2.084
##     y6                         1.258    0.164    7.651    0.000    2.623
##     y7                         1.282    0.158    8.137    0.000    2.673
##     y8                         1.310    0.154    8.529    0.000    2.730
##   industrialization_1960 =~                                             
##     x1                         1.000                               0.669
##     x2                         2.182    0.139   15.714    0.000    1.461
##     x3                         1.819    0.152   11.956    0.000    1.218
##   Std.all
##          
##     0.845
##     0.760
##     0.705
##     0.860
##          
##     0.803
##     0.783
##     0.819
##     0.847
##          
##     0.920
##     0.973
##     0.872
## 
## Covariances:
##                     Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   democracy_1960 ~~                                                      
##     democracy_1965     4.487    0.911    4.924    0.000    0.978    0.978
##     indstrlzt_1960     0.660    0.206    3.202    0.001    0.448    0.448
##   democracy_1965 ~~                                                      
##     indstrlzt_1960     0.774    0.208    3.715    0.000    0.555    0.555
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .y1                1.942    0.395    4.910    0.000    1.942    0.286
##    .y2                6.490    1.185    5.479    0.000    6.490    0.422
##    .y3                5.340    0.943    5.662    0.000    5.340    0.503
##    .y4                2.887    0.610    4.731    0.000    2.887    0.261
##    .y5                2.390    0.447    5.351    0.000    2.390    0.355
##    .y6                4.343    0.796    5.456    0.000    4.343    0.387
##    .y7                3.510    0.668    5.252    0.000    3.510    0.329
##    .y8                2.940    0.586    5.019    0.000    2.940    0.283
##    .x1                0.082    0.020    4.180    0.000    0.082    0.154
##    .x2                0.118    0.070    1.689    0.091    0.118    0.053
##    .x3                0.467    0.090    5.174    0.000    0.467    0.240
##     democracy_1960    4.845    1.088    4.453    0.000    1.000    1.000
##     democracy_1965    4.345    1.051    4.134    0.000    1.000    1.000
##     indstrlzt_1960    0.448    0.087    5.169    0.000    1.000    1.000
lavaanPlot(model = fit2, coefs = TRUE, covs = TRUE)
# ===============================
# MODELO 3 - SEM (REGRESIÓN ESTRUCTURAL)
# ===============================

modelo_sem <- '
democracy_1960 =~ y1 + y2 + y3 + y4
democracy_1965 =~ y5 + y6 + y7 + y8
industrialization_1960 =~ x1 + x2 + x3

democracy_1960 ~ industrialization_1960
democracy_1965 ~ democracy_1960 + industrialization_1960
'

fit_sem <- sem(modelo_sem, data = bd2)

summary(fit_sem, fit.measures = TRUE, standardized = TRUE)
## lavaan 0.6-21 ended normally after 42 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        25
## 
##   Number of observations                            75
## 
## Model Test User Model:
##                                                       
##   Test statistic                                72.462
##   Degrees of freedom                                41
##   P-value (Chi-square)                           0.002
## 
## Model Test Baseline Model:
## 
##   Test statistic                               730.654
##   Degrees of freedom                                55
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.953
##   Tucker-Lewis Index (TLI)                       0.938
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -1564.959
##   Loglikelihood unrestricted model (H1)      -1528.728
##                                                       
##   Akaike (AIC)                                3179.918
##   Bayesian (BIC)                              3237.855
##   Sample-size adjusted Bayesian (SABIC)       3159.062
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.101
##   90 Percent confidence interval - lower         0.061
##   90 Percent confidence interval - upper         0.139
##   P-value H_0: RMSEA <= 0.050                    0.021
##   P-value H_0: RMSEA >= 0.080                    0.827
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.055
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                             Estimate  Std.Err  z-value  P(>|z|)   Std.lv
##   democracy_1960 =~                                                     
##     y1                         1.000                               2.201
##     y2                         1.354    0.175    7.755    0.000    2.980
##     y3                         1.044    0.150    6.961    0.000    2.298
##     y4                         1.300    0.138    9.412    0.000    2.860
##   democracy_1965 =~                                                     
##     y5                         1.000                               2.084
##     y6                         1.258    0.164    7.651    0.000    2.623
##     y7                         1.282    0.158    8.137    0.000    2.673
##     y8                         1.310    0.154    8.529    0.000    2.730
##   industrialization_1960 =~                                             
##     x1                         1.000                               0.669
##     x2                         2.182    0.139   15.714    0.000    1.461
##     x3                         1.819    0.152   11.956    0.000    1.218
##   Std.all
##          
##     0.845
##     0.760
##     0.705
##     0.860
##          
##     0.803
##     0.783
##     0.819
##     0.847
##          
##     0.920
##     0.973
##     0.872
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   democracy_1960 ~                                                      
##     indstrlzt_1960    1.474    0.392    3.763    0.000    0.448    0.448
##   democracy_1965 ~                                                      
##     democracy_1960    0.864    0.113    7.671    0.000    0.913    0.913
##     indstrlzt_1960    0.453    0.220    2.064    0.039    0.146    0.146
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .y1                1.942    0.395    4.910    0.000    1.942    0.286
##    .y2                6.490    1.185    5.479    0.000    6.490    0.422
##    .y3                5.340    0.943    5.662    0.000    5.340    0.503
##    .y4                2.887    0.610    4.731    0.000    2.887    0.261
##    .y5                2.390    0.447    5.351    0.000    2.390    0.355
##    .y6                4.343    0.796    5.456    0.000    4.343    0.387
##    .y7                3.510    0.668    5.252    0.000    3.510    0.329
##    .y8                2.940    0.586    5.019    0.000    2.940    0.283
##    .x1                0.082    0.020    4.180    0.000    0.082    0.154
##    .x2                0.118    0.070    1.689    0.091    0.118    0.053
##    .x3                0.467    0.090    5.174    0.000    0.467    0.240
##    .democracy_1960    3.872    0.893    4.338    0.000    0.799    0.799
##    .democracy_1965    0.115    0.200    0.575    0.565    0.026    0.026
##     indstrlzt_1960    0.448    0.087    5.169    0.000    1.000    1.000
lavaanPlot(model = fit_sem, coefs = TRUE, covs = TRUE)