Bibliotecas

  library(knitr)
  library(semPlot)
  library(tidyverse)

Exemplo do Cap 3 do livro de (Beaujean, 2014)


#### Dados

# Ative o lavaan
  library(lavaan)

# Cria uma matriz de correlação
    wisc4.cor <- lav_matrix_lower2full(c(1,0.72,1,0.64,0.63,1,0.51,0.48,0.37,1,0.37,0.38,0.38,0.38,1))
# Cria vetor com desvios padr
    wisc4.sd <- c(3.01 , 3.03 , 2.99 , 2.89 , 2.98)
# nomeia variáveis
    colnames(wisc4.cor) <- rownames(wisc4.cor) <- c("Information", "Similarities", 
        "Word.Reasoning", "Matrix.Reasoning", "Picture.Concepts")
    
    names(wisc4.sd) <-  c("Information", "Similarities", "Word.Reasoning", "Matrix.Reasoning", 
        "Picture.Concepts")

# Converte matriz de correlação em covariâncias
    wisc4.cov <- cor2cov(wisc4.cor,wisc4.sd)

   wisc4.cor %>% kable(digits = 2)
Information Similarities Word.Reasoning Matrix.Reasoning Picture.Concepts
Information 1.00 0.72 0.64 0.51 0.37
Similarities 0.72 1.00 0.63 0.48 0.38
Word.Reasoning 0.64 0.63 1.00 0.37 0.38
Matrix.Reasoning 0.51 0.48 0.37 1.00 0.38
Picture.Concepts 0.37 0.38 0.38 0.38 1.00
   wisc4.cov %>% kable(digits = 2)
Information Similarities Word.Reasoning Matrix.Reasoning Picture.Concepts
Information 9.06 6.57 5.76 4.44 3.32
Similarities 6.57 9.18 5.71 4.20 3.43
Word.Reasoning 5.76 5.71 8.94 3.20 3.39
Matrix.Reasoning 4.44 4.20 3.20 8.35 3.27
Picture.Concepts 3.32 3.43 3.39 3.27 8.88
#### Estimação do m odelo e result ados
# Especifica modelo
    wisc4.model<-"
     g =~ a*Information + b*Similarities + c*Word.Reasoning + d*Matrix.Reasoning + 
    e*Picture.Concepts
    "
# Roda o modelo
    wisc4.fit <- cfa(model=wisc4.model, sample.cov=wisc4.cov, sample.nobs=550,  std.lv=FALSE)

# Examina resultados
    summary(wisc4.fit,standardized=TRUE)
## lavaan (0.5-23.1097) converged normally after  30 iterations
## 
##   Number of observations                           550
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic               26.775
##   Degrees of freedom                                 5
##   P-value (Chi-square)                           0.000
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   g =~                                                                  
##     Informatin (a)    1.000                               2.578    0.857
##     Similarits (b)    0.985    0.045   21.708    0.000    2.541    0.839
##     Word.Rsnng (c)    0.860    0.045   18.952    0.000    2.217    0.742
##     Mtrx.Rsnng (d)    0.647    0.047   13.896    0.000    1.669    0.578
##     Pctr.Cncpt (e)    0.542    0.050   10.937    0.000    1.398    0.470
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Information       2.395    0.250    9.587    0.000    2.395    0.265
##    .Similarities      2.709    0.258   10.482    0.000    2.709    0.296
##    .Word.Reasoning    4.009    0.295   13.600    0.000    4.009    0.449
##    .Matrix.Reasnng    5.551    0.360   15.400    0.000    5.551    0.666
##    .Picture.Cncpts    6.909    0.434   15.922    0.000    6.909    0.779
##     g                 6.648    0.564   11.788    0.000    1.000    1.000
    parameterEstimates(wisc4.fit,standardized=TRUE)
##                 lhs op              rhs label   est    se      z pvalue
## 1                 g =~      Information     a 1.000 0.000     NA     NA
## 2                 g =~     Similarities     b 0.985 0.045 21.708      0
## 3                 g =~   Word.Reasoning     c 0.860 0.045 18.952      0
## 4                 g =~ Matrix.Reasoning     d 0.647 0.047 13.896      0
## 5                 g =~ Picture.Concepts     e 0.542 0.050 10.937      0
## 6       Information ~~      Information       2.395 0.250  9.587      0
## 7      Similarities ~~     Similarities       2.709 0.258 10.482      0
## 8    Word.Reasoning ~~   Word.Reasoning       4.009 0.295 13.600      0
## 9  Matrix.Reasoning ~~ Matrix.Reasoning       5.551 0.360 15.400      0
## 10 Picture.Concepts ~~ Picture.Concepts       6.909 0.434 15.922      0
## 11                g ~~                g       6.648 0.564 11.788      0
##    ci.lower ci.upper std.lv std.all std.nox
## 1     1.000    1.000  2.578   0.857   0.857
## 2     0.896    1.074  2.541   0.839   0.839
## 3     0.771    0.949  2.217   0.742   0.742
## 4     0.556    0.739  1.669   0.578   0.578
## 5     0.445    0.640  1.398   0.470   0.470
## 6     1.906    2.885  2.395   0.265   0.265
## 7     2.202    3.215  2.709   0.296   0.296
## 8     3.431    4.587  4.009   0.449   0.449
## 9     4.845    6.258  5.551   0.666   0.666
## 10    6.058    7.759  6.909   0.779   0.779
## 11    5.543    7.754  1.000   1.000   1.000
# Covariâncias implicadas pelo modelo 
    fitted(wisc4.fit)
## $cov
##                  Infrmt Smlrts Wrd.Rs Mtrx.R Pctr.C
## Information      9.044                             
## Similarities     6.551  9.164                      
## Word.Reasoning   5.716  5.633  8.924               
## Matrix.Reasoning 4.303  4.241  3.700  8.337        
## Picture.Concepts 3.606  3.553  3.100  2.334  8.864 
## 
## $mean
##      Information     Similarities   Word.Reasoning Matrix.Reasoning 
##                0                0                0                0 
## Picture.Concepts 
##                0
# Transforma Covariâncias implicadas pelo modelo  em correlaçòes
    wisc4Fit.cov <- fitted(wisc4.fit)$cov
    wisc4Fit.cor <- cov2cor(wisc4Fit.cov)

# Resíduos
    residuals(wisc4.fit,type="cor")
## $type
## [1] "cor.bollen"
## 
## $cor
##                  Infrmt Smlrts Wrd.Rs Mtrx.R Pctr.C
## Information       0.000                            
## Similarities      0.000  0.000                     
## Word.Reasoning    0.004  0.007  0.000              
## Matrix.Reasoning  0.014 -0.005 -0.059  0.000       
## Picture.Concepts -0.033 -0.014  0.031  0.109  0.000
## 
## $mean
##      Information     Similarities   Word.Reasoning Matrix.Reasoning 
##                0                0                0                0 
## Picture.Concepts 
##                0
# Medidas de ajuste do modelo 
    fitMeasures(wisc4.fit)
##                npar                fmin               chisq 
##              10.000               0.024              26.775 
##                  df              pvalue      baseline.chisq 
##               5.000               0.000            1073.427 
##         baseline.df     baseline.pvalue                 cfi 
##              10.000               0.000               0.980 
##                 tli                nnfi                 rfi 
##               0.959               0.959               0.950 
##                 nfi                pnfi                 ifi 
##               0.975               0.488               0.980 
##                 rni                logl   unrestricted.logl 
##               0.980           -6378.678           -6365.291 
##                 aic                 bic              ntotal 
##           12777.357           12820.456             550.000 
##                bic2               rmsea      rmsea.ci.lower 
##           12788.712               0.089               0.058 
##      rmsea.ci.upper        rmsea.pvalue                 rmr 
##               0.123               0.022               0.298 
##          rmr_nomean                srmr        srmr_bentler 
##               0.298               0.034               0.034 
## srmr_bentler_nomean         srmr_bollen  srmr_bollen_nomean 
##               0.034               0.034               0.034 
##          srmr_mplus   srmr_mplus_nomean               cn_05 
##               0.034               0.034             228.408 
##               cn_01                 gfi                agfi 
##             310.899               0.982               0.947 
##                pgfi                 mfi                ecvi 
##               0.327               0.980               0.085
# Indices de modoficação
    modificationIndices(wisc4.fit) 
##                 lhs op              rhs     mi    epc sepc.lv sepc.all
## 12      Information ~~     Similarities  0.010  0.034   0.034    0.004
## 13      Information ~~   Word.Reasoning  0.279  0.147   0.147    0.016
## 14      Information ~~ Matrix.Reasoning  1.447  0.280   0.280    0.032
## 15      Information ~~ Picture.Concepts  5.493 -0.565  -0.565   -0.063
## 16     Similarities ~~   Word.Reasoning  0.791  0.242   0.242    0.027
## 17     Similarities ~~ Matrix.Reasoning  0.147 -0.089  -0.089   -0.010
## 18     Similarities ~~ Picture.Concepts  0.838 -0.223  -0.223   -0.025
## 19   Word.Reasoning ~~ Matrix.Reasoning  8.931 -0.710  -0.710   -0.082
## 20   Word.Reasoning ~~ Picture.Concepts  2.029  0.365   0.365    0.041
## 21 Matrix.Reasoning ~~ Picture.Concepts 14.157  1.058   1.058    0.123
##    sepc.nox
## 12    0.004
## 13    0.016
## 14    0.032
## 15   -0.063
## 16    0.027
## 17   -0.010
## 18   -0.025
## 19   -0.082
## 20    0.041
## 21    0.123

Exercício

Referências

Beaujean, A. A. (2014). Latent Variable Modeling Using R: A Step-By-Step Guide (Edição: 1.). New York: Routledge.