1 Modelo de Ecuaciones Estructurales (SEM)

# Instalar paquetes y llamar librerías
library(lavaan)
## Warning: package 'lavaan' was built under R version 4.5.2
## This is lavaan 0.6-21
## lavaan is FREE software! Please report any bugs.
library(lavaanPlot)
## Warning: package 'lavaanPlot' was built under R version 4.5.2
# Importar base de datos
bd <- HolzingerSwineford1939

# Entender la base de datos
summary(bd)
##        id             sex            ageyr        agemo       
##  Min.   :  1.0   Min.   :1.000   Min.   :11   Min.   : 0.000  
##  1st Qu.: 82.0   1st Qu.:1.000   1st Qu.:12   1st Qu.: 2.000  
##  Median :163.0   Median :2.000   Median :13   Median : 5.000  
##  Mean   :176.6   Mean   :1.515   Mean   :13   Mean   : 5.375  
##  3rd Qu.:272.0   3rd Qu.:2.000   3rd Qu.:14   3rd Qu.: 8.000  
##  Max.   :351.0   Max.   :2.000   Max.   :16   Max.   :11.000  
##                                                               
##          school        grade             x1               x2       
##  Grant-White:145   Min.   :7.000   Min.   :0.6667   Min.   :2.250  
##  Pasteur    :156   1st Qu.:7.000   1st Qu.:4.1667   1st Qu.:5.250  
##                    Median :7.000   Median :5.0000   Median :6.000  
##                    Mean   :7.477   Mean   :4.9358   Mean   :6.088  
##                    3rd Qu.:8.000   3rd Qu.:5.6667   3rd Qu.:6.750  
##                    Max.   :8.000   Max.   :8.5000   Max.   :9.250  
##                    NA's   :1                                       
##        x3              x4              x5              x6        
##  Min.   :0.250   Min.   :0.000   Min.   :1.000   Min.   :0.1429  
##  1st Qu.:1.375   1st Qu.:2.333   1st Qu.:3.500   1st Qu.:1.4286  
##  Median :2.125   Median :3.000   Median :4.500   Median :2.0000  
##  Mean   :2.250   Mean   :3.061   Mean   :4.341   Mean   :2.1856  
##  3rd Qu.:3.125   3rd Qu.:3.667   3rd Qu.:5.250   3rd Qu.:2.7143  
##  Max.   :4.500   Max.   :6.333   Max.   :7.000   Max.   :6.1429  
##                                                                  
##        x7              x8               x9       
##  Min.   :1.304   Min.   : 3.050   Min.   :2.778  
##  1st Qu.:3.478   1st Qu.: 4.850   1st Qu.:4.750  
##  Median :4.087   Median : 5.500   Median :5.417  
##  Mean   :4.186   Mean   : 5.527   Mean   :5.374  
##  3rd Qu.:4.913   3rd Qu.: 6.100   3rd Qu.:6.083  
##  Max.   :7.435   Max.   :10.000   Max.   :9.250  
## 
# Tipos de fórmulas
  # Regresión ~ 
  # Definición de variable latente =~
  # Varianzas y covarianzas ~~
  # Intercepto ~1

# Estructura del modelo
modelo <- ' # Definiciones de variables latentes
              visual =~ x1 + x2 + x3
              textual =~ x4 + x5 + x6
              speed =~ x7 + x8 + x9 
'
# Análisis Factorial Confirmatorio
fit <- cfa(modelo,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)
# Ejercicio
bd2 <- PoliticalDemocracy
# Explorar
summary(bd2)
##        y1               y2               y3               y4        
##  Min.   : 1.250   Min.   : 0.000   Min.   : 0.000   Min.   : 0.000  
##  1st Qu.: 2.900   1st Qu.: 0.000   1st Qu.: 3.767   1st Qu.: 1.581  
##  Median : 5.400   Median : 3.333   Median : 6.667   Median : 3.333  
##  Mean   : 5.465   Mean   : 4.256   Mean   : 6.563   Mean   : 4.453  
##  3rd Qu.: 7.500   3rd Qu.: 8.283   3rd Qu.:10.000   3rd Qu.: 6.667  
##  Max.   :10.000   Max.   :10.000   Max.   :10.000   Max.   :10.000  
##        y5               y6               y7               y8        
##  Min.   : 0.000   Min.   : 0.000   Min.   : 0.000   Min.   : 0.000  
##  1st Qu.: 3.692   1st Qu.: 0.000   1st Qu.: 3.478   1st Qu.: 1.301  
##  Median : 5.000   Median : 2.233   Median : 6.667   Median : 3.333  
##  Mean   : 5.136   Mean   : 2.978   Mean   : 6.196   Mean   : 4.043  
##  3rd Qu.: 7.500   3rd Qu.: 4.207   3rd Qu.:10.000   3rd Qu.: 6.667  
##  Max.   :10.000   Max.   :10.000   Max.   :10.000   Max.   :10.000  
##        x1              x2              x3       
##  Min.   :3.784   Min.   :1.386   Min.   :1.002  
##  1st Qu.:4.477   1st Qu.:3.663   1st Qu.:2.300  
##  Median :5.075   Median :4.963   Median :3.568  
##  Mean   :5.054   Mean   :4.792   Mean   :3.558  
##  3rd Qu.:5.515   3rd Qu.:5.830   3rd Qu.:4.523  
##  Max.   :6.737   Max.   :7.872   Max.   :6.425
modelo2 <- '
  # Variables latentes (medición)
  ind60 =~ x1 + x2 + x3
  dem60 =~ y1 + y2 + y3 + y4
  dem65 =~ y5 + y6 + y7 + y8

  # Relaciones estructurales (SEM)
  dem60 ~ ind60
  dem65 ~ ind60 + dem60

  # Covarianzas de errores (como en el modelo clásico)
  y1 ~~ y5
  y2 ~~ y4 + y6
  y3 ~~ y7
  y4 ~~ y8
  y6 ~~ y8
'
fit2 <- sem(modelo2, data = bd2)
lavaanPlot(model = fit2, coefs = TRUE, covs = TRUE)
summary(fit2)
## lavaan 0.6-21 ended normally after 68 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        31
## 
##   Number of observations                            75
## 
## Model Test User Model:
##                                                       
##   Test statistic                                38.125
##   Degrees of freedom                                35
##   P-value (Chi-square)                           0.329
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   ind60 =~                                            
##     x1                1.000                           
##     x2                2.180    0.139   15.742    0.000
##     x3                1.819    0.152   11.967    0.000
##   dem60 =~                                            
##     y1                1.000                           
##     y2                1.257    0.182    6.889    0.000
##     y3                1.058    0.151    6.987    0.000
##     y4                1.265    0.145    8.722    0.000
##   dem65 =~                                            
##     y5                1.000                           
##     y6                1.186    0.169    7.024    0.000
##     y7                1.280    0.160    8.002    0.000
##     y8                1.266    0.158    8.007    0.000
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   dem60 ~                                             
##     ind60             1.483    0.399    3.715    0.000
##   dem65 ~                                             
##     ind60             0.572    0.221    2.586    0.010
##     dem60             0.837    0.098    8.514    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##  .y1 ~~                                               
##    .y5                0.624    0.358    1.741    0.082
##  .y2 ~~                                               
##    .y4                1.313    0.702    1.871    0.061
##    .y6                2.153    0.734    2.934    0.003
##  .y3 ~~                                               
##    .y7                0.795    0.608    1.308    0.191
##  .y4 ~~                                               
##    .y8                0.348    0.442    0.787    0.431
##  .y6 ~~                                               
##    .y8                1.356    0.568    2.386    0.017
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .x1                0.082    0.019    4.184    0.000
##    .x2                0.120    0.070    1.718    0.086
##    .x3                0.467    0.090    5.177    0.000
##    .y1                1.891    0.444    4.256    0.000
##    .y2                7.373    1.374    5.366    0.000
##    .y3                5.067    0.952    5.324    0.000
##    .y4                3.148    0.739    4.261    0.000
##    .y5                2.351    0.480    4.895    0.000
##    .y6                4.954    0.914    5.419    0.000
##    .y7                3.431    0.713    4.814    0.000
##    .y8                3.254    0.695    4.685    0.000
##     ind60             0.448    0.087    5.173    0.000
##    .dem60             3.956    0.921    4.295    0.000
##    .dem65             0.172    0.215    0.803    0.422