CFA Lab - Part 1

install the package and then activate

install.packages("lavaan")
## 
## The downloaded binary packages are in
##  /var/folders/md/5lvwdwvn7hg86vrfg8bs447r0000gp/T//Rtmp3GEMNn/downloaded_packages
library(lavaan)
## This is lavaan 0.5-17
## lavaan is BETA software! Please report any bugs.

Read the dataset into R variable using the read.csv(file) function

# the path of the data file needs to be changed to match the path of your data file
data = read.csv("~/Desktop/UMBC Fall 2015/Measurement/CFA Lab/CFAlab.txt")
# Check the dimension of data and the first six rows;

dim(data)
## [1] 130   6
head(data)
##   a b c d e f
## 1 1 1 1 2 1 1
## 2 3 1 3 1 3 1
## 3 2 2 2 2 3 1
## 4 1 1 1 1 1 1
## 5 2 2 2 2 2 1
## 6 3 1 3 3 2 2

Use the result from EFA to specify a two-factor CFA model

model = '
f1 =~ a+c+e
f2 =~ b+d+f
'
#run the CFA model; 
fit=cfa(model,data=data);

#check the model results; 
summary(fit,standardized=T,rsquare=T,fit.measures=T); 
## lavaan (0.5-17) converged normally after  29 iterations
## 
##   Number of observations                           130
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic               10.465
##   Degrees of freedom                                 8
##   P-value (Chi-square)                           0.234
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic              356.303
##   Degrees of freedom                                15
##   P-value                                        0.000
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    0.993
##   Tucker-Lewis Index (TLI)                       0.986
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)               -806.965
##   Loglikelihood unrestricted model (H1)       -801.732
## 
##   Number of free parameters                         13
##   Akaike (AIC)                                1639.929
##   Bayesian (BIC)                              1677.207
##   Sample-size adjusted Bayesian (BIC)         1636.091
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.049
##   90 Percent Confidence Interval          0.000  0.120
##   P-value RMSEA <= 0.05                          0.448
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.050
## 
## Parameter estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
##                    Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
## Latent variables:
##   f1 =~
##     a                 1.000                               0.865    0.818
##     c                 1.194    0.096   12.451    0.000    1.032    0.975
##     e                 0.966    0.088   10.951    0.000    0.835    0.807
##   f2 =~
##     b                 1.000                               0.423    0.629
##     d                 1.109    0.202    5.500    0.000    0.470    0.676
##     f                 1.307    0.246    5.305    0.000    0.553    0.795
## 
## Covariances:
##   f1 ~~
##     f2                0.002    0.038    0.064    0.949    0.007    0.007
## 
## Variances:
##     a                 0.370    0.059                      0.370    0.331
##     c                 0.054    0.053                      0.054    0.049
##     e                 0.374    0.058                      0.374    0.349
##     b                 0.274    0.045                      0.274    0.605
##     d                 0.263    0.048                      0.263    0.543
##     f                 0.178    0.054                      0.178    0.367
##     f1                0.747    0.136                      1.000    1.000
##     f2                0.179    0.053                      1.000    1.000
## 
## R-Square:
## 
##     a                 0.669
##     c                 0.951
##     e                 0.651
##     b                 0.395
##     d                 0.457
##     f                 0.633

Check multivarity normality

install.packages("MVN")
## 
##   There is a binary version available (and will be installed) but
##   the source version is later:
##     binary source
## MVN    3.9    4.0
## 
## 
## The downloaded binary packages are in
##  /var/folders/md/5lvwdwvn7hg86vrfg8bs447r0000gp/T//Rtmp3GEMNn/downloaded_packages
library(MVN) 
mardiaTest(data,qqplot=T)
##    Mardia's Multivariate Normality Test 
## --------------------------------------- 
##    data : data 
## 
##    g1p            : 5.391 
##    chi.skew       : 116.8 
##    p.value.skew   : 3.568e-06 
## 
##    g2p            : 54.68 
##    z.kurtosis     : 3.885 
##    p.value.kurt   : 0.0001024 
## 
##    chi.small.skew : 120.3 
##    p.value.small  : 1.352e-06 
## 
##    Result          : Data are not multivariate normal. 
## ---------------------------------------
hzTest(data,qqplot=T)

plot of chunk unnamed-chunk-3

##   Henze-Zirkler's Multivariate Normality Test 
## --------------------------------------------- 
##   data : data 
## 
##   HZ      : 1.735 
##   p-value : 0 
## 
##   Result  : Data are not multivariate normal. 
## ---------------------------------------------
roystonTest(data, qqplot=T)

plot of chunk unnamed-chunk-3

##   Royston's Multivariate Normality Test 
## --------------------------------------------- 
##   data : data 
## 
##   H       : 235.1 
##   p-value : 2.753e-48 
## 
##   Result  : Data are not multivariate normal. 
## ---------------------------------------------

Run the CFA models with robust standard errors and statistic

# run the CFA model with robust standard errors and Satorra-Bentler statistic; 
fit2=cfa(model,data=data,se="robust.sem",test="satorra.bentler");
summary(fit2,standardized=T,rsquare=T,fit.measures=T);
## lavaan (0.5-17) converged normally after  29 iterations
## 
##   Number of observations                           130
## 
##   Estimator                                         ML      Robust
##   Minimum Function Test Statistic               10.465       8.497
##   Degrees of freedom                                 8           8
##   P-value (Chi-square)                           0.234       0.386
##   Scaling correction factor                                  1.232
##     for the Satorra-Bentler correction
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic              356.303     309.842
##   Degrees of freedom                                15          15
##   P-value                                        0.000       0.000
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    0.993       0.998
##   Tucker-Lewis Index (TLI)                       0.986       0.997
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)               -806.965    -806.965
##   Loglikelihood unrestricted model (H1)       -801.732    -801.732
## 
##   Number of free parameters                         19          19
##   Akaike (AIC)                                1651.929    1651.929
##   Bayesian (BIC)                              1706.412    1706.412
##   Sample-size adjusted Bayesian (BIC)         1646.319    1646.319
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.049       0.022
##   90 Percent Confidence Interval          0.000  0.120       0.000  0.100
##   P-value RMSEA <= 0.05                          0.448       0.634
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.044       0.044
## 
## Parameter estimates:
## 
##   Information                                 Expected
##   Standard Errors                           Robust.sem
## 
##                    Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
## Latent variables:
##   f1 =~
##     a                 1.000                               0.865    0.818
##     c                 1.194    0.086   13.942    0.000    1.032    0.975
##     e                 0.966    0.081   11.932    0.000    0.835    0.807
##   f2 =~
##     b                 1.000                               0.423    0.629
##     d                 1.109    0.207    5.360    0.000    0.470    0.676
##     f                 1.307    0.229    5.712    0.000    0.553    0.795
## 
## Covariances:
##   f1 ~~
##     f2                0.002    0.042    0.057    0.955    0.007    0.007
## 
## Intercepts:
##     a                 2.600    0.093   27.942    0.000    2.600    2.460
##     c                 2.731    0.093   29.309    0.000    2.731    2.581
##     e                 2.400    0.091   26.343    0.000    2.400    2.319
##     b                 1.823    0.059   30.754    0.000    1.823    2.708
##     d                 2.038    0.061   33.309    0.000    2.038    2.933
##     f                 1.846    0.061   30.139    0.000    1.846    2.654
##     f1                0.000                               0.000    0.000
##     f2                0.000                               0.000    0.000
## 
## Variances:
##     a                 0.370    0.070                      0.370    0.331
##     c                 0.054    0.054                      0.054    0.049
##     e                 0.374    0.076                      0.374    0.349
##     b                 0.274    0.052                      0.274    0.605
##     d                 0.263    0.050                      0.263    0.543
##     f                 0.178    0.057                      0.178    0.367
##     f1                0.747    0.109                      1.000    1.000
##     f2                0.179    0.059                      1.000    1.000
## 
## R-Square:
## 
##     a                 0.669
##     c                 0.951
##     e                 0.651
##     b                 0.395
##     d                 0.457
##     f                 0.633