Extracting information

The summary() function gives a nice overview of a fitted model, but is for display only. If you need the actual numbers for further processing, you may prefer to use one of several ‘extractor’ functions. We have already seen the coef() function which extracts the estimated parameters of a fitted model. Other extractor functions are discussed below.

parameterEstimates

The parameterEstimates() function returns a data.frame containing all the model parameters in the rows:

fit <- cfa(HS.model, data=HolzingerSwineford1939)
parameterEstimates(fit)
       lhs op     rhs   est    se      z pvalue ci.lower ci.upper
1   visual =~      x1 1.000 0.000     NA     NA    1.000    1.000
2   visual =~      x2 0.554 0.100  5.554      0    0.358    0.749
3   visual =~      x3 0.729 0.109  6.685      0    0.516    0.943
4  textual =~      x4 1.000 0.000     NA     NA    1.000    1.000
5  textual =~      x5 1.113 0.065 17.014      0    0.985    1.241
6  textual =~      x6 0.926 0.055 16.703      0    0.817    1.035
7    speed =~      x7 1.000 0.000     NA     NA    1.000    1.000
8    speed =~      x8 1.180 0.165  7.152      0    0.857    1.503
9    speed =~      x9 1.082 0.151  7.155      0    0.785    1.378
10      x1 ~~      x1 0.549 0.114  4.833      0    0.326    0.772
11      x2 ~~      x2 1.134 0.102 11.146      0    0.934    1.333
12      x3 ~~      x3 0.844 0.091  9.317      0    0.667    1.022
13      x4 ~~      x4 0.371 0.048  7.779      0    0.278    0.465
14      x5 ~~      x5 0.446 0.058  7.642      0    0.332    0.561
15      x6 ~~      x6 0.356 0.043  8.277      0    0.272    0.441
16      x7 ~~      x7 0.799 0.081  9.823      0    0.640    0.959
17      x8 ~~      x8 0.488 0.074  6.573      0    0.342    0.633
18      x9 ~~      x9 0.566 0.071  8.003      0    0.427    0.705
19  visual ~~  visual 0.809 0.145  5.564      0    0.524    1.094
20 textual ~~ textual 0.979 0.112  8.737      0    0.760    1.199
21   speed ~~   speed 0.384 0.086  4.451      0    0.215    0.553
22  visual ~~ textual 0.408 0.074  5.552      0    0.264    0.552
23  visual ~~   speed 0.262 0.056  4.660      0    0.152    0.373
24 textual ~~   speed 0.173 0.049  3.518      0    0.077    0.270

The lhs (left-hand side), op (operator) and rhs (right-hand side) columns define the parameter. The est, se, z and pvalue columns provide the point estimate, the standard error, the z-value and the p-value for this parameter. The last two columns are the lower and upper bounds of a 95% confidence interval around the point estimate.

standardizedSolution

The standardizedSolution() function is similar to the parameterEstimates() function, but only shows the standardized parameter estimates and corresponding standard errors, z-values, p-values and confidence intervals.

fitted.values

The fitted() and fitted.values() functions return the model-implied (fitted) covariance matrix (and mean vector) of a fitted model:

fit <- cfa(HS.model, data = HolzingerSwineford1939)
fitted(fit)
$cov
      x1    x2    x3    x4    x5    x6    x7    x8    x9
x1 1.358                                                
x2 0.448 1.382                                          
x3 0.590 0.327 1.275                                    
x4 0.408 0.226 0.298 1.351                              
x5 0.454 0.252 0.331 1.090 1.660                        
x6 0.378 0.209 0.276 0.907 1.010 1.196                  
x7 0.262 0.145 0.191 0.173 0.193 0.161 1.183            
x8 0.309 0.171 0.226 0.205 0.228 0.190 0.453 1.022      
x9 0.284 0.157 0.207 0.188 0.209 0.174 0.415 0.490 1.015

residuals

The resid() or residuals() functions return (unstandardized) residuals of a fitted model. This is simply the difference between the observed and implied covariance matrix and mean vector.

fit <- cfa(HS.model, data = HolzingerSwineford1939)
resid(fit)
$type
[1] "raw"

$cov
       x1     x2     x3     x4     x5     x6     x7     x8     x9
x1  0.000                                                        
x2 -0.041  0.000                                                 
x3 -0.010  0.124  0.000                                          
x4  0.097 -0.017 -0.090  0.000                                   
x5 -0.014 -0.040 -0.219  0.008  0.000                            
x6  0.077  0.038 -0.032 -0.012  0.005  0.000                     
x7 -0.177 -0.242 -0.103  0.046 -0.050 -0.017  0.000              
x8 -0.046 -0.062 -0.013 -0.079 -0.047 -0.024  0.082  0.000       
x9  0.175  0.087  0.167  0.056  0.086  0.062 -0.042 -0.032  0.000

The lavResiduals() gives more extensive information about the residuals. Per default, it will print both raw and standardized residuals, as well as several summary statistics (including the SRMR and the unbiased SRMR).

vcov

The function vcov() returns the estimated covariance matrix of the parameter estimates.

AIC and BIC

The AIC() and BIC() functions return the AIC and BIC values of a fitted model.

fitMeasures

The fitMeasures() function returns all the fit measures computed by lavaan as a named numeric vector.

fit <- cfa(HS.model, data=HolzingerSwineford1939)
fitMeasures(fit)
                 npar                  fmin                 chisq 
               21.000                 0.142                85.306 
                   df                pvalue        baseline.chisq 
               24.000                 0.000               918.852 
          baseline.df       baseline.pvalue                   cfi 
               36.000                 0.000                 0.931 
                  tli                  nnfi                   rfi 
                0.896                 0.896                 0.861 
                  nfi                  pnfi                   ifi 
                0.907                 0.605                 0.931 
                  rni                  logl     unrestricted.logl 
                0.931             -3737.745             -3695.092 
                  aic                   bic                ntotal 
             7517.490              7595.339               301.000 
                 bic2                 rmsea        rmsea.ci.lower 
             7528.739                 0.092                 0.071 
       rmsea.ci.upper        rmsea.ci.level          rmsea.pvalue 
                0.114                 0.900                 0.001 
       rmsea.close.h0 rmsea.notclose.pvalue     rmsea.notclose.h0 
                0.050                 0.840                 0.080 
                  rmr            rmr_nomean                  srmr 
                0.082                 0.082                 0.065 
         srmr_bentler   srmr_bentler_nomean                  crmr 
                0.065                 0.065                 0.073 
          crmr_nomean            srmr_mplus     srmr_mplus_nomean 
                0.073                 0.065                 0.065 
                cn_05                 cn_01                   gfi 
              129.490               152.654                 0.943 
                 agfi                  pgfi                   mfi 
                0.894                 0.503                 0.903 
                 ecvi 
                0.423 

If you only want the value of a single fit measure, say, the CFI, you give the name (in lower case) as the second argument:

fit <- cfa(HS.model, data=HolzingerSwineford1939)
fitMeasures(fit, "cfi")
  cfi 
0.931 

Or you can provide a vector of fit measures, as in

fitMeasures(fit, c("cfi","rmsea","srmr"))
  cfi rmsea  srmr 
0.931 0.092 0.065 

lavInspect

If you want to peek inside a fitted lavaan object (the object that is returned by a call to cfa(), sem()or growth()), you can use the lavInspect() function, with a variety of options. By default, calling lavInspect() on a fitted lavaan object returns a list of the model matrices that are used internally to represent the model. The free parameters are nonzero integers.

fit <- cfa(HS.model, data=HolzingerSwineford1939)
lavInspect(fit)
$lambda
   visual textul speed
x1      0      0     0
x2      1      0     0
x3      2      0     0
x4      0      0     0
x5      0      3     0
x6      0      4     0
x7      0      0     0
x8      0      0     5
x9      0      0     6

$theta
   x1 x2 x3 x4 x5 x6 x7 x8 x9
x1  7                        
x2  0  8                     
x3  0  0  9                  
x4  0  0  0 10               
x5  0  0  0  0 11            
x6  0  0  0  0  0 12         
x7  0  0  0  0  0  0 13      
x8  0  0  0  0  0  0  0 14   
x9  0  0  0  0  0  0  0  0 15

$psi
        visual textul speed
visual      16             
textual     19     17      
speed       20     21    18

To see the starting values of parameters in each model matrix, type

lavInspect(fit, what = "start")
$lambda
   visual textul speed
x1  1.000  0.000 0.000
x2  0.778  0.000 0.000
x3  1.107  0.000 0.000
x4  0.000  1.000 0.000
x5  0.000  1.133 0.000
x6  0.000  0.924 0.000
x7  0.000  0.000 1.000
x8  0.000  0.000 1.225
x9  0.000  0.000 0.854

$theta
      x1    x2    x3    x4    x5    x6    x7    x8    x9
x1 0.679                                                
x2 0.000 0.691                                          
x3 0.000 0.000 0.637                                    
x4 0.000 0.000 0.000 0.675                              
x5 0.000 0.000 0.000 0.000 0.830                        
x6 0.000 0.000 0.000 0.000 0.000 0.598                  
x7 0.000 0.000 0.000 0.000 0.000 0.000 0.592            
x8 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.511      
x9 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.508

$psi
        visual textul speed
visual    0.05             
textual   0.00   0.05      
speed     0.00   0.00  0.05

To see how lavaan internally represents a model, you can type

lavInspect(fit, what = "list")
   id     lhs op     rhs user block group free ustart exo label plabel start
1   1  visual =~      x1    1     1     1    0      1   0         .p1. 1.000
2   2  visual =~      x2    1     1     1    1     NA   0         .p2. 0.778
3   3  visual =~      x3    1     1     1    2     NA   0         .p3. 1.107
4   4 textual =~      x4    1     1     1    0      1   0         .p4. 1.000
5   5 textual =~      x5    1     1     1    3     NA   0         .p5. 1.133
6   6 textual =~      x6    1     1     1    4     NA   0         .p6. 0.924
7   7   speed =~      x7    1     1     1    0      1   0         .p7. 1.000
8   8   speed =~      x8    1     1     1    5     NA   0         .p8. 1.225
9   9   speed =~      x9    1     1     1    6     NA   0         .p9. 0.854
10 10      x1 ~~      x1    0     1     1    7     NA   0        .p10. 0.679
11 11      x2 ~~      x2    0     1     1    8     NA   0        .p11. 0.691
12 12      x3 ~~      x3    0     1     1    9     NA   0        .p12. 0.637
13 13      x4 ~~      x4    0     1     1   10     NA   0        .p13. 0.675
14 14      x5 ~~      x5    0     1     1   11     NA   0        .p14. 0.830
15 15      x6 ~~      x6    0     1     1   12     NA   0        .p15. 0.598
16 16      x7 ~~      x7    0     1     1   13     NA   0        .p16. 0.592
17 17      x8 ~~      x8    0     1     1   14     NA   0        .p17. 0.511
18 18      x9 ~~      x9    0     1     1   15     NA   0        .p18. 0.508
19 19  visual ~~  visual    0     1     1   16     NA   0        .p19. 0.050
20 20 textual ~~ textual    0     1     1   17     NA   0        .p20. 0.050
21 21   speed ~~   speed    0     1     1   18     NA   0        .p21. 0.050
22 22  visual ~~ textual    0     1     1   19     NA   0        .p22. 0.000
23 23  visual ~~   speed    0     1     1   20     NA   0        .p23. 0.000
24 24 textual ~~   speed    0     1     1   21     NA   0        .p24. 0.000
     est    se
1  1.000 0.000
2  0.554 0.100
3  0.729 0.109
4  1.000 0.000
5  1.113 0.065
6  0.926 0.055
7  1.000 0.000
8  1.180 0.165
9  1.082 0.151
10 0.549 0.114
11 1.134 0.102
12 0.844 0.091
13 0.371 0.048
14 0.446 0.058
15 0.356 0.043
16 0.799 0.081
17 0.488 0.074
18 0.566 0.071
19 0.809 0.145
20 0.979 0.112
21 0.384 0.086
22 0.408 0.074
23 0.262 0.056
24 0.173 0.049

This is equivalent to the parTable(fit) function. The table that is returned here is called the ‘parameter table’.

For more lavInspect options, see the help page:

?lavInspect