library(lavaan)
setwd(mypath)  # change it to the path of your own data folder
dat <- read.table("proficiencyraw-female.csv", sep=",", header=F)

# give variable names
colnames(dat) <- c(paste0("goals",1:6), 
                    paste0("rsc",1:5), 
                    paste0("hsc",1:5), 
                    paste0("msc",1:5), 
                    paste0("ssc",1:5), 
                    "SATvoc", 
                    "SATcomp", 
                    "SATlang", 
                    "SATmath", 
                    "SATprob", 
                    "SATproc")

# model syntax
cfa.Model <- '

# measurement model

RSC =~ rsc1 + rsc2 + rsc3
MSC =~ msc1 + msc2 + msc3

# mean structure
rsc1~1
rsc2~1
rsc3~1
msc1~1
msc2~1
msc3~1

# residual covariances

rsc1~~msc1
rsc2~~msc2
rsc3~~msc3


# covariances between the latent variables
RSC ~~ MSC 

'

# Fit the model
cfa.Fit <- sem(cfa.Model, data = dat)
summary(cfa.Fit)
## lavaan 0.6.15 ended normally after 29 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        22
## 
##   Number of observations                          1000
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 7.359
##   Degrees of freedom                                 5
##   P-value (Chi-square)                           0.195
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   RSC =~                                              
##     rsc1              1.000                           
##     rsc2              1.265    0.090   14.003    0.000
##     rsc3              0.964    0.068   14.253    0.000
##   MSC =~                                              
##     msc1              1.000                           
##     msc2              0.941    0.038   24.752    0.000
##     msc3              0.890    0.037   24.153    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##  .rsc1 ~~                                             
##    .msc1             -0.037    0.034   -1.107    0.268
##  .rsc2 ~~                                             
##    .msc2             -0.006    0.032   -0.194    0.846
##  .rsc3 ~~                                             
##    .msc3              0.024    0.033    0.733    0.464
##   RSC ~~                                              
##     MSC              -0.209    0.040   -5.197    0.000
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .rsc1              3.308    0.040   82.220    0.000
##    .rsc2              3.657    0.043   85.643    0.000
##    .rsc3              3.645    0.041   89.863    0.000
##    .msc1              3.349    0.044   75.918    0.000
##    .msc2              3.162    0.041   76.509    0.000
##    .msc3              3.528    0.041   85.903    0.000
##     RSC               0.000                           
##     MSC               0.000                           
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .rsc1              0.937    0.060   15.596    0.000
##    .rsc2              0.733    0.076    9.661    0.000
##    .rsc3              1.012    0.061   16.689    0.000
##    .msc1              0.654    0.048   13.552    0.000
##    .msc2              0.565    0.042   13.331    0.000
##    .msc3              0.664    0.043   15.591    0.000
##     RSC               0.681    0.073    9.332    0.000
##     MSC               1.292    0.090   14.288    0.000

© Copyright 2024 @Yi Feng and @Gregory R. Hancock.