Bibliotecas

  library(knitr)
  library(semPlot)
  library(tidyverse)
  library(psych)
  library(lavaan)

Dados

# abra o arquivo direto da internet
  con<-url("http://www.labape.com.br/rprimi/SEM/exerc18/ex3b.RData") 
  load(con) 
CFA
# Modelo
    senna1o<-"
      C =~ C_0_c + C_1_c
      A =~ A_0_c + A_1_c
      O =~ O_0_c + O_1_c
      N =~ N_0_c + N_1_c
      E =~ E_0_c + E_1_c
    "
    senna1o.fit <- cfa(senna1o, data = scores, std.lv = TRUE)
    summary(senna1o.fit , fit.measure=TRUE, standardized=TRUE, rsquare=TRUE)
## lavaan (0.5-23.1097) converged normally after  40 iterations
## 
##   Number of observations                           168
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic               48.790
##   Degrees of freedom                                25
##   P-value (Chi-square)                           0.003
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic              618.233
##   Degrees of freedom                                45
##   P-value                                        0.000
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    0.958
##   Tucker-Lewis Index (TLI)                       0.925
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -1540.325
##   Loglikelihood unrestricted model (H1)      -1515.930
## 
##   Number of free parameters                         30
##   Akaike (AIC)                                3140.651
##   Bayesian (BIC)                              3234.370
##   Sample-size adjusted Bayesian (BIC)         3139.383
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.075
##   90 Percent Confidence Interval          0.043  0.106
##   P-value RMSEA <= 0.05                          0.092
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.044
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   C =~                                                                  
##     C_0_c             0.549    0.059    9.298    0.000    0.549    0.685
##     C_1_c             0.734    0.054   13.668    0.000    0.734    0.959
##   A =~                                                                  
##     A_0_c             0.438    0.045    9.634    0.000    0.438    0.739
##     A_1_c             0.478    0.049    9.773    0.000    0.478    0.749
##   O =~                                                                  
##     O_0_c             0.452    0.052    8.721    0.000    0.452    0.675
##     O_1_c             0.586    0.061    9.608    0.000    0.586    0.744
##   N =~                                                                  
##     N_0_c             0.630    0.064    9.848    0.000    0.630    0.770
##     N_1_c             0.688    0.065   10.577    0.000    0.688    0.828
##   E =~                                                                  
##     E_0_c             0.339    0.064    5.267    0.000    0.339    0.538
##     E_1_c             0.286    0.064    4.488    0.000    0.286    0.417
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   C ~~                                                                  
##     A                 0.714    0.065   10.942    0.000    0.714    0.714
##     O                 0.704    0.070   10.088    0.000    0.704    0.704
##     N                 0.505    0.074    6.793    0.000    0.505    0.505
##     E                 0.844    0.133    6.354    0.000    0.844    0.844
##   A ~~                                                                  
##     O                 0.646    0.085    7.572    0.000    0.646    0.646
##     N                 0.551    0.083    6.660    0.000    0.551    0.551
##     E                 0.884    0.146    6.068    0.000    0.884    0.884
##   O ~~                                                                  
##     N                 0.677    0.077    8.799    0.000    0.677    0.677
##     E                 0.980    0.153    6.412    0.000    0.980    0.980
##   N ~~                                                                  
##     E                 0.600    0.137    4.378    0.000    0.600    0.600
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .C_0_c             0.341    0.045    7.516    0.000    0.341    0.531
##    .C_1_c             0.047    0.047    1.011    0.312    0.047    0.081
##    .A_0_c             0.160    0.027    5.944    0.000    0.160    0.454
##    .A_1_c             0.179    0.031    5.725    0.000    0.179    0.439
##    .O_0_c             0.244    0.035    6.990    0.000    0.244    0.544
##    .O_1_c             0.276    0.048    5.704    0.000    0.276    0.446
##    .N_0_c             0.273    0.054    5.051    0.000    0.273    0.408
##    .N_1_c             0.217    0.059    3.687    0.000    0.217    0.314
##    .E_0_c             0.282    0.044    6.442    0.000    0.282    0.711
##    .E_1_c             0.388    0.048    8.117    0.000    0.388    0.826
##     C                 1.000                               1.000    1.000
##     A                 1.000                               1.000    1.000
##     O                 1.000                               1.000    1.000
##     N                 1.000                               1.000    1.000
##     E                 1.000                               1.000    1.000
## 
## R-Square:
##                    Estimate
##     C_0_c             0.469
##     C_1_c             0.919
##     A_0_c             0.546
##     A_1_c             0.561
##     O_0_c             0.456
##     O_1_c             0.554
##     N_0_c             0.592
##     N_1_c             0.686
##     E_0_c             0.289
##     E_1_c             0.174
    fitMeasures( senna1o.fit, 
      fit.measures = c("chisq", "df", "cfi", "tli", "rmsea","srmr")
      )
##  chisq     df    cfi    tli  rmsea   srmr 
## 48.790 25.000  0.958  0.925  0.075  0.044
CFA
  names(scores)
##   [1] "banco"            "cod_suj"          "Data de nasc."   
##   [4] "data.aplic"       "Idade0"           "idade1"          
##   [7] "Termo"            "sujeito"          "serie"           
##  [10] "escola"           "turma"            "Sexo"            
##  [13] "Esc. Mãe"         "port"             "mat"             
##  [16] "cloze"            "A_o"              "C_o"             
##  [19] "E_o"              "N_o"              "O_o"             
##  [22] "A_c"              "C_c"              "E_c"             
##  [25] "N_c"              "O_c"              "A_z"             
##  [28] "C_z"              "E_z"              "N_z"             
##  [31] "O_z"              "antonym.rc"       "antonym.cntrst_A"
##  [34] "antonym.cntrst_C" "antonym.cntrst_E" "antonym.cntrst_N"
##  [37] "antonym.cntrst_O" "mean_A"           "mean_C"          
##  [40] "mean_E"           "mean_N"           "mean_O"          
##  [43] "sd_A"             "sd_C"             "sd_E"            
##  [46] "sd_N"             "sd_O"             "antonym.rc_A"    
##  [49] "antonym.rc_C"     "antonym.rc_E"     "antonym.rc_N"    
##  [52] "antonym.rc_O"     "nse"              "A_1_o"           
##  [55] "C_1_o"            "E_1_o"            "N_1_o"           
##  [58] "O_1_o"            "A_1_c"            "C_1_c"           
##  [61] "E_1_c"            "N_1_c"            "O_1_c"           
##  [64] "A_1_z"            "C_1_z"            "E_1_z"           
##  [67] "N_1_z"            "O_1_z"            "A_0_o"           
##  [70] "C_0_o"            "E_0_o"            "N_0_o"           
##  [73] "O_0_o"            "A_0_c"            "C_0_c"           
##  [76] "E_0_c"            "N_0_c"            "O_0_c"           
##  [79] "A_0_z"            "C_0_z"            "E_0_z"           
##  [82] "N_0_z"            "O_0_z"            "cexp_o"          
##  [85] "comlrn_o"         "coplrn_o"         "cstrat_o"        
##  [88] "effper_o"         "elab_o"           "insmot_o"        
##  [91] "intmat_o"         "intrea_o"         "memor_o"         
##  [94] "scacad_o"         "scmath_o"         "scverb_o"        
##  [97] "selfef_o"         "cexp_c"           "comlrn_c"        
## [100] "coplrn_c"         "cstrat_c"         "effper_c"        
## [103] "elab_c"           "insmot_c"         "intmat_c"        
## [106] "intrea_c"         "memor_c"          "scacad_c"        
## [109] "scmath_c"         "scverb_c"         "selfef_c"        
## [112] "cexp_z"           "comlrn_z"         "coplrn_z"        
## [115] "cstrat_z"         "effper_z"         "elab_z"          
## [118] "insmot_z"         "intmat_z"         "intrea_z"        
## [121] "memor_z"          "scacad_z"         "scmath_z"        
## [124] "scverb_z"         "selfef_z"         "means"           
## [127] "sd"
  v <- names(scores)[c(14:15)]
  corr.test(scores[ , v])
## Call:corr.test(x = scores[, v])
## Correlation matrix 
##      port  mat
## port 1.00 0.51
## mat  0.51 1.00
## Sample Size 
## [1] 76
## Probability values (Entries above the diagonal are adjusted for multiple tests.) 
##      port mat
## port    0   0
## mat     0   0
## 
##  To see confidence intervals of the correlations, print with the short=FALSE option
  describe(scores[ , v])
##      vars  n  mean    sd median trimmed   mad min max range  skew kurtosis
## port    1 76 69.74 18.55     75   70.81 22.24  25 100    75 -0.46    -0.64
## mat     2 76 61.05 23.31     65   62.02 22.24   0 100   100 -0.48    -0.10
##        se
## port 2.13
## mat  2.67
  source("http://www.labape.com.br/rprimi/R/cria_quartis.R")
  
  
  ggplot(data = scores, aes(y = port, x = mat)) + geom_point(alpha=.5) +
    geom_smooth(method = "lm")

   senna1o<-"
      C =~ C_0_c + C_1_c
      A =~ A_0_c + A_1_c
      O =~ O_0_c + O_1_c
      N =~ N_0_c + N_1_c
      E =~ E_0_c + E_1_c
      gc =~ port +  mat
      gc ~ C + A + O + N + O
     
    "
    senna1o.fit <- sem(senna1o, data = scores, std.lv = TRUE)
    summary(senna1o.fit , fit.measure=TRUE, standardized=TRUE, rsquare=TRUE)
## lavaan (0.5-23.1097) converged normally after 137 iterations
## 
##                                                   Used       Total
##   Number of observations                            76         168
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic               54.741
##   Degrees of freedom                                40
##   P-value (Chi-square)                           0.060
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic              384.450
##   Degrees of freedom                                66
##   P-value                                        0.000
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    0.954
##   Tucker-Lewis Index (TLI)                       0.924
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -1300.792
##   Loglikelihood unrestricted model (H1)      -1273.421
## 
##   Number of free parameters                         38
##   Akaike (AIC)                                2677.584
##   Bayesian (BIC)                              2766.152
##   Sample-size adjusted Bayesian (BIC)         2646.373
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.070
##   90 Percent Confidence Interval          0.000  0.112
##   P-value RMSEA <= 0.05                          0.236
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.062
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   C =~                                                                  
##     C_0_c             0.582    0.091    6.431    0.000    0.582    0.700
##     C_1_c             0.762    0.071   10.789    0.000    0.762    1.081
##   A =~                                                                  
##     A_0_c             0.391    0.059    6.625    0.000    0.391    0.738
##     A_1_c             0.458    0.068    6.734    0.000    0.458    0.749
##   O =~                                                                  
##     O_0_c             0.395    0.073    5.381    0.000    0.395    0.605
##     O_1_c             0.477    0.078    6.125    0.000    0.477    0.674
##   N =~                                                                  
##     N_0_c             0.700    0.106    6.602    0.000    0.700    0.833
##     N_1_c             0.675    0.099    6.804    0.000    0.675    0.864
##   E =~                                                                  
##     E_0_c             0.427    0.079    5.411    0.000    0.427    0.727
##     E_1_c             0.359    0.083    4.310    0.000    0.359    0.543
##   gc =~                                                                 
##     port              8.352    2.506    3.333    0.001   12.120    0.658
##     mat              12.491    3.916    3.189    0.001   18.126    0.783
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   gc ~                                                                  
##     C                 0.307    0.324    0.947    0.344    0.212    0.212
##     A                -1.109    1.006   -1.102    0.271   -0.764   -0.764
##     O                 1.653    1.220    1.355    0.176    1.139    1.139
##     N                -0.139    0.364   -0.382    0.702   -0.096   -0.096
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   C ~~                                                                  
##     A                 0.632    0.097    6.498    0.000    0.632    0.632
##     O                 0.666    0.105    6.331    0.000    0.666    0.666
##     N                 0.318    0.106    2.995    0.003    0.318    0.318
##     E                 0.426    0.124    3.428    0.001    0.426    0.426
##   A ~~                                                                  
##     O                 0.834    0.105    7.924    0.000    0.834    0.834
##     N                 0.386    0.133    2.906    0.004    0.386    0.386
##     E                 0.677    0.141    4.809    0.000    0.677    0.677
##   O ~~                                                                  
##     N                 0.541    0.133    4.080    0.000    0.541    0.541
##     E                 0.861    0.129    6.684    0.000    0.861    0.861
##   N ~~                                                                  
##     E                 0.261    0.155    1.686    0.092    0.261    0.261
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .C_0_c             0.352    0.071    4.950    0.000    0.352    0.510
##    .C_1_c            -0.084    0.074   -1.137    0.255   -0.084   -0.169
##    .A_0_c             0.128    0.030    4.232    0.000    0.128    0.456
##    .A_1_c             0.164    0.040    4.077    0.000    0.164    0.439
##    .O_0_c             0.271    0.049    5.489    0.000    0.271    0.634
##    .O_1_c             0.272    0.053    5.108    0.000    0.272    0.545
##    .N_0_c             0.217    0.107    2.036    0.042    0.217    0.307
##    .N_1_c             0.155    0.097    1.604    0.109    0.155    0.254
##    .E_0_c             0.163    0.053    3.071    0.002    0.163    0.471
##    .E_1_c             0.308    0.060    5.169    0.000    0.308    0.705
##    .port            192.513   46.619    4.130    0.000  192.513    0.567
##    .mat             207.839   84.441    2.461    0.014  207.839    0.387
##     C                 1.000                               1.000    1.000
##     A                 1.000                               1.000    1.000
##     O                 1.000                               1.000    1.000
##     N                 1.000                               1.000    1.000
##     E                 1.000                               1.000    1.000
##    .gc                1.000                               0.475    0.475
## 
## R-Square:
##                    Estimate
##     C_0_c             0.490
##     C_1_c                NA
##     A_0_c             0.544
##     A_1_c             0.561
##     O_0_c             0.366
##     O_1_c             0.455
##     N_0_c             0.693
##     N_1_c             0.746
##     E_0_c             0.529
##     E_1_c             0.295
##     port              0.433
##     mat               0.613
##     gc                0.525
    fitMeasures( senna1o.fit, 
      fit.measures = c("chisq", "df", "cfi", "tli", "rmsea","srmr")
      )
##  chisq     df    cfi    tli  rmsea   srmr 
## 54.741 40.000  0.954  0.924  0.070  0.062
  semPaths(senna1o.fit,
      whatLabels = "std" 
      )

   semPaths(senna1o.fit,
      whatLabels = "std", 
     edge.label.cex=.6,
     node.width = 1.1
      )

  ggplot(data = scores,
    aes(y = port+mat, x = C_c, color = cria_quartis(A_c))) + 
    geom_point(alpha=.5) + geom_smooth(method = "lm")