Rose Maier
PSY610: Structural Equation Modeling (Prof. Sanjay Srivastava)
Spring 2014

Data from NCEDL's Multi-State Study of Pre-Kindergarten and Study of State-Wide Early Education Programs (SWEEP). Available for download here.

load("/Users/TARDIS/Dropbox/SEM/ICPSR_34877/DS0002/34877-0002-Data.rda")
# select only children for which we have both fall and spring participation in English
data_full <- subset(da34877.0002, as.numeric(ASMSTATPF)==2 & as.numeric(ASMSTATPS)==2)

# linear transformations on language measure variables to make the scales more similar, for easier estimation in lavaan.
data_full$PPVREPF <- data_full$PPVREPF/5
data_full$PPVREPS <- data_full$PPVREPS/5
data_full$OWLSEPF <- data_full$OWLSEPF/5
data_full$OWLSEPS <- data_full$OWLSEPS/5
data_full$CSLANGPF <- data_full$CSLANGPF*5
data_full$CSLANGPS <- data_full$CSLANGPS*5
data_full$WJ21AEPF <- data_full$WJ21AEPF*3
data_full$WJ21AEPS <- data_full$WJ21AEPS*3

# =====================================
# split the sample in half, for validity testing!

n <- nrow(data_full)
# shuffle the dataframe
data_full <- data_full[sample(nrow(data_full), n),]
# split halfway
split <- floor(n/2)
df1 <- data_full[1:split,]
df2 <- data_full[(split+1):n,]

# =====================================
# EDA on language measures
child_info <- subset(df1, select=c("ASMTAGEPF", "ASMTAGEPS", "CHMATEDNP"))

fall_lit <- subset(df1, select=c("LETTEREPF", "PPVREPF", "OWLSEPF",  "WJ21AEPF", "CSLANGPF", "EWTNMEPF"))
spring_lit <- subset(df1, select=c("LETTEREPS", "PPVREPS", "OWLSEPS", "WJ21AEPS", "CSLANGPS", "EWTNMEPS"))

fall_comb <- fall_lit
spring_comb <- spring_lit
colnames(fall_comb) <- c("LETTEREP", "PPVREP",  "OWLSEP", "WJ21AEP", "CSLANGP", "EWTNMEP")
colnames(spring_comb) <- c("LETTEREP", "PPVREP", "OWLSEP", "WJ21AEP", "CSLANGP", "EWTNMEP")
fall_comb$time <- 0
spring_comb$time <- 1
comb_measures <- rbind(fall_comb, spring_comb)
comb_measures$time <- as.factor(comb_measures$time)

# histograms
require(reshape2)
## Loading required package: reshape2
melt <- melt(comb_measures)
## Using time as id variables
require(ggplot2)
## Loading required package: ggplot2
ggplot(melt,aes(x = value, fill=time)) + 
  facet_wrap(~variable,scales = "free") + 
  geom_histogram(position="identity", alpha=.5)
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.

plot of chunk unnamed-chunk-1

# EDA: 
# The distributions for PPVT and OWLS look good, CSLANGP looks okay (a little closer to uniform than normal). LETTER (how many written letters the child can correctly identify, min=0 max=26) appears to have a floor effect in the fall and a ceiling effect in the spring. WJ21AEP (rhyming) is positively skewed at both timepoints, but more so in the fall. 
# The only super problematic variable is EWTNM (what percent of his or her name the child can write legibly). There is a pronounced ceiling effect: The mode is 100% in both fall and spring (and it's even more pronounced in the spring than in the fall)
# I'll drop that variable since it's going to mess with my covariance matrix, which will throw off everything. 
fall_lit <- subset(df1, select=c("LETTEREPF", "PPVREPF", "OWLSEPF",  "WJ21AEPF", "CSLANGPF"))
spring_lit <- subset(df1, select=c("LETTEREPS", "PPVREPS", "OWLSEPS", "WJ21AEPS", "CSLANGPS"))

# regenerate data for histograms
comb_measures <- subset(comb_measures, select=c("LETTEREP", "PPVREP", "OWLSEP", "WJ21AEP", "CSLANGP", "time"))

# histgrams
melt <- melt(comb_measures)
## Using time as id variables
ggplot(melt,aes(x = value, fill=time)) + 
  facet_wrap(~variable,scales = "free") + 
  geom_histogram(position="identity", alpha=.5)
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.

plot of chunk unnamed-chunk-2


# histograms for child info (SES, etc.)
melt <- melt(child_info, id.vars=NULL)
ggplot(melt,aes(x = value)) + 
  facet_wrap(~variable,scales = "free") + 
  geom_histogram()
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
## Warning: position_stack requires constant width: output may be incorrect

plot of chunk unnamed-chunk-2

df <- cbind(fall_lit, spring_lit, child_info)

# corr matrices
round(cor(fall_lit, use="pairwise.complete.obs"), 3)
##           LETTEREPF PPVREPF OWLSEPF WJ21AEPF CSLANGPF
## LETTEREPF     1.000   0.347   0.309    0.398    0.451
## PPVREPF       0.347   1.000   0.673    0.507    0.408
## OWLSEPF       0.309   0.673   1.000    0.518    0.390
## WJ21AEPF      0.398   0.507   0.518    1.000    0.373
## CSLANGPF      0.451   0.408   0.390    0.373    1.000
round(cor(spring_lit, use="pairwise.complete.obs"), 3)
##           LETTEREPS PPVREPS OWLSEPS WJ21AEPS CSLANGPS
## LETTEREPS     1.000   0.328   0.274    0.357    0.483
## PPVREPS       0.328   1.000   0.623    0.520    0.362
## OWLSEPS       0.274   0.623   1.000    0.494    0.376
## WJ21AEPS      0.357   0.520   0.494    1.000    0.387
## CSLANGPS      0.483   0.362   0.376    0.387    1.000
# the pattern looks very similar in fall and spring

round(cov(df, use="pairwise.complete.obs"), 3)
##           LETTEREPF PPVREPF OWLSEPF WJ21AEPF CSLANGPF LETTEREPS PPVREPS
## LETTEREPF    80.497  11.537   7.278   35.482   17.809    66.423  10.301
## PPVREPF      11.537  13.747   6.511   18.643    6.517    12.285  10.733
## OWLSEPF       7.278   6.511   6.872   13.500    4.383     7.085   5.940
## WJ21AEPF     35.482  18.643  13.500   98.438   16.177    32.435  17.866
## CSLANGPF     17.809   6.517   4.383   16.177   19.160    15.921   5.943
## LETTEREPS    66.423  12.285   7.085   32.435   15.921    87.875  11.007
## PPVREPS      10.301  10.733   5.940   17.866    5.943    11.007  12.819
## OWLSEPS       6.013   5.902   5.200   11.730    4.399     6.538   5.684
## WJ21AEPS     41.449  22.811  16.095   81.859   18.696    41.201  22.913
## CSLANGPS     20.190   6.569   4.489   16.285   12.366    21.260   6.009
## ASMTAGEPF     0.506   0.304  -0.012    0.578    0.234     0.465   0.276
## ASMTAGEPS     0.486   0.310  -0.003    0.621    0.217     0.494   0.299
## CHMATEDNP     5.748   3.198   2.140    6.903    2.657     5.496   3.003
##           OWLSEPS WJ21AEPS CSLANGPS ASMTAGEPF ASMTAGEPS CHMATEDNP
## LETTEREPF   6.013   41.449   20.190     0.506     0.486     5.748
## PPVREPF     5.902   22.811    6.569     0.304     0.310     3.198
## OWLSEPF     5.200   16.095    4.489    -0.012    -0.003     2.140
## WJ21AEPF   11.730   81.859   16.285     0.578     0.621     6.903
## CSLANGPF    4.399   18.696   12.366     0.234     0.217     2.657
## LETTEREPS   6.538   41.201   21.260     0.465     0.494     5.496
## PPVREPS     5.684   22.913    6.009     0.276     0.299     3.003
## OWLSEPS     6.486   15.491    4.443    -0.063    -0.057     2.020
## WJ21AEPS   15.491  151.535   22.276     0.571     0.683     9.367
## CSLANGPS    4.443   22.276   21.956     0.286     0.288     3.023
## ASMTAGEPF  -0.063    0.571    0.286     0.100     0.100    -0.020
## ASMTAGEPS  -0.057    0.683    0.288     0.100     0.104    -0.005
## CHMATEDNP   2.020    9.367    3.023    -0.020    -0.005     5.720
round(sapply(df, sd, na.rm=TRUE), 3)
## LETTEREPF   PPVREPF   OWLSEPF  WJ21AEPF  CSLANGPF LETTEREPS   PPVREPS 
##     8.972     3.708     2.621     9.922     4.377     9.374     3.580 
##   OWLSEPS  WJ21AEPS  CSLANGPS ASMTAGEPF ASMTAGEPS CHMATEDNP 
##     2.547    12.310     4.686     0.317     0.322     2.392
# =====================================
# CFA 
df <- cbind(fall_lit, spring_lit, child_info)

require(lavaan)
## Loading required package: lavaan
## This is lavaan 0.5-16
## lavaan is BETA software! Please report any bugs.
model <- '
fall_literacy =~ LETTEREPF + PPVREPF + OWLSEPF + WJ21AEPF + CSLANGPF
spring_literacy =~ LETTEREPS + PPVREPS + OWLSEPS + WJ21AEPS + CSLANGPS 
'

fit <- cfa(model, data=df)
## Warning: lavaan WARNING: covariance matrix of latent variables is not
## positive definite; use inspect(fit,"cov.lv") to investigate.
summary(fit, fit.measures=TRUE, standardized = TRUE)
## lavaan (0.5-16) converged normally after  77 iterations
## 
##                                                   Used       Total
##   Number of observations                           863        1059
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic             1529.950
##   Degrees of freedom                                34
##   P-value (Chi-square)                           0.000
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic             5056.967
##   Degrees of freedom                                45
##   P-value                                        0.000
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    0.702
##   Tucker-Lewis Index (TLI)                       0.605
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)             -24812.646
##   Loglikelihood unrestricted model (H1)     -24047.671
## 
##   Number of free parameters                         21
##   Akaike (AIC)                               49667.293
##   Bayesian (BIC)                             49767.261
##   Sample-size adjusted Bayesian (BIC)        49700.571
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.226
##   90 Percent Confidence Interval          0.216  0.236
##   P-value RMSEA <= 0.05                          0.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.114
## 
## Parameter estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
##                    Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
## Latent variables:
##   fall_literacy =~
##     LETTEREPF         1.000                               4.606    0.511
##     PPVREPF           0.616    0.040   15.534    0.000    2.837    0.792
##     OWLSEPF           0.423    0.028   15.275    0.000    1.948    0.764
##     WJ21AEPF          1.414    0.100   14.112    0.000    6.511    0.656
##     CSLANGPF          0.500    0.040   12.431    0.000    2.305    0.533
##   spring_literacy =~
##     LETTEREPS         1.000                               4.655    0.497
##     PPVREPS           0.579    0.039   15.036    0.000    2.697    0.776
##     OWLSEPS           0.393    0.027   14.723    0.000    1.829    0.741
##     WJ21AEPS          1.724    0.125   13.790    0.000    8.023    0.651
##     CSLANGPS          0.523    0.043   12.114    0.000    2.434    0.522
## 
## Covariances:
##   fall_literacy ~~
##     spring_litrcy    23.266    2.226   10.454    0.000    1.085    1.085
## 
## Variances:
##     LETTEREPF        60.018    2.940                     60.018    0.739
##     PPVREPF           4.784    0.263                      4.784    0.373
##     OWLSEPF           2.702    0.144                      2.702    0.416
##     WJ21AEPF         56.013    2.818                     56.013    0.569
##     CSLANGPF         13.393    0.658                     13.393    0.716
##     LETTEREPS        65.915    3.214                     65.915    0.753
##     PPVREPS           4.805    0.259                      4.805    0.398
##     OWLSEPS           2.744    0.143                      2.744    0.451
##     WJ21AEPS         87.598    4.379                     87.598    0.576
##     CSLANGPS         15.793    0.772                     15.793    0.727
##     fall_literacy    21.217    2.691                      1.000    1.000
##     spring_litrcy    21.665    2.823                      1.000    1.000
# The argument standardized=TRUE augments the output with standardized parameter values. Two extra columns of standardized parameter values are printed. In the first column (labeled Std.lv), only the latent variables are standardized. In the second column (labeled Std.all), both latent and observed variables are standardized. The latter is often called the 'completely standardized solution'.
# The fit is okay but not great (RMSEA=.223, CFI=.707). Check mod indices to see where the problem might be.

# Error message about not positive definite cov matrix of latent variables. Looks like it's because the corr between fall literacy and spring literacy is greater than 1. 
varTable(fit)
##         name idx nobs    type exo user   mean     var nlev lnam
## 1  LETTEREPF   1 1057 numeric   0    0  8.585  80.497    0     
## 2    PPVREPF   2 1059 numeric   0    0 10.684  13.747    0     
## 3    OWLSEPF   3 1054 numeric   0    0 18.471   6.872    0     
## 4   WJ21AEPF   4 1059 numeric   0    0  7.059  98.438    0     
## 5   CSLANGPF   5  992 numeric   0    0 11.755  19.160    0     
## 6  LETTEREPS   6 1059 numeric   0    0 14.143  87.875    0     
## 7    PPVREPS   7 1059 numeric   0    0 12.591  12.819    0     
## 8    OWLSEPS   8 1059 numeric   0    0 19.021   6.486    0     
## 9   WJ21AEPS   9 1059 numeric   0    0 11.720 151.535    0     
## 10  CSLANGPS  10  915 numeric   0    0 15.308  21.956    0
inspect(fit,"cov.lv")
##                 fll_lt sprng_
## fall_literacy   21.22        
## spring_literacy 23.27  21.66

resid(fit)$cov
##           LETTEREPF PPVREPF OWLSEPF WJ21AEPF CSLANGPF LETTEREPS PPVREPS
## LETTEREPF -0.001                                                       
## PPVREPF   -2.077     0.000                                             
## OWLSEPF   -2.320     0.450   0.000                                     
## WJ21AEPF   6.638    -0.892   0.201   0.000                             
## CSLANGPF   7.404    -0.469  -0.382   0.676    0.000                    
## LETTEREPS 43.371    -3.045  -3.014   0.511    4.175    0.001           
## PPVREPS   -3.375     1.563  -0.294  -1.908   -1.290   -2.038     0.000 
## OWLSEPS   -3.405    -0.255   0.985  -1.520   -0.636   -2.212     0.199 
## WJ21AEPS   1.571    -2.934  -1.705  24.666   -1.445    5.102     0.972 
## CSLANGPS   7.540    -1.417  -0.870  -1.744    6.249    9.084    -0.974 
##           OWLSEPS WJ21AEPS CSLANGPS
## LETTEREPF                          
## PPVREPF                            
## OWLSEPF                            
## WJ21AEPF                           
## CSLANGPF                           
## LETTEREPS                          
## PPVREPS                            
## OWLSEPS    0.000                   
## WJ21AEPS   0.209   0.000           
## CSLANGPS  -0.283   2.062    0.000


mi <- modindices(fit)
# show modification indices, sorting by modification index in decending order
subset(mi[with(mi, order(-mi)), ], mi>4)
##              lhs op       rhs      mi    epc sepc.lv sepc.all sepc.nox
## 1      LETTEREPF ~~ LETTEREPS 463.670 47.665  47.665    0.565    0.565
## 2        OWLSEPF ~~   OWLSEPS 181.850  1.573   1.573    0.250    0.250
## 3        PPVREPF ~~   PPVREPS 179.273  2.976   2.976    0.239    0.239
## 4       CSLANGPF ~~  CSLANGPS 168.324  6.680   6.680    0.331    0.331
## 5       WJ21AEPF ~~  WJ21AEPS 112.338 27.895  27.895    0.228    0.228
## 6      LETTEREPS ~~  CSLANGPS  70.931  9.419   9.419    0.216    0.216
## 7      LETTEREPF ~~  CSLANGPF  61.538  7.741   7.741    0.199    0.199
## 8      LETTEREPF ~~   OWLSEPS  59.740 -3.683  -3.683   -0.166   -0.166
## 9      LETTEREPF ~~  CSLANGPS  59.720  8.398   8.398    0.200    0.200
## 10       OWLSEPF ~~ LETTEREPS  46.924 -3.446  -3.446   -0.144   -0.144
## 11       PPVREPF ~~  WJ21AEPS  37.614 -5.158  -5.158   -0.117   -0.117
## 12     LETTEREPF ~~   OWLSEPF  32.549 -2.636  -2.636   -0.115   -0.115
## 13     LETTEREPF ~~   PPVREPS  31.862 -3.620  -3.620   -0.116   -0.116
## 14       PPVREPF ~~  CSLANGPS  31.134 -1.876  -1.876   -0.112   -0.112
## 15      CSLANGPF ~~   PPVREPS  28.475 -1.628  -1.628   -0.108   -0.108
## 16     LETTEREPS ~~   OWLSEPS  25.830 -2.447  -2.447   -0.106   -0.106
## 17       PPVREPF ~~ LETTEREPS  25.002 -3.407  -3.407   -0.102   -0.102
## 18       OWLSEPF ~~  WJ21AEPS  24.412 -3.042  -3.042   -0.097   -0.097
## 19       PPVREPF ~~   OWLSEPF  21.533  0.717   0.717    0.079    0.079
## 20       OWLSEPF ~~  CSLANGPS  21.215 -1.142  -1.142   -0.096   -0.096
## 21      CSLANGPF ~~ LETTEREPS  20.220  4.715   4.715    0.117    0.117
## 22      WJ21AEPF ~~   OWLSEPS  17.232 -2.021  -2.021   -0.083   -0.083
## 23      WJ21AEPF ~~   PPVREPS  16.858 -2.714  -2.714   -0.079   -0.079
## 24     LETTEREPF ~~   PPVREPF  15.120 -2.422  -2.422   -0.075   -0.075
## 25     LETTEREPS ~~   PPVREPS  12.827 -2.310  -2.310   -0.071   -0.071
## 26       PPVREPS ~~  CSLANGPS  12.368 -1.117  -1.117   -0.069   -0.069
## 27     LETTEREPF ~~  WJ21AEPF  12.166  7.139   7.139    0.080    0.080
## 28      CSLANGPF ~~   OWLSEPS  11.506 -0.768  -0.768   -0.072   -0.072
## 29       OWLSEPF ~~   PPVREPS   9.633 -0.498  -0.498   -0.056   -0.056
## 30 fall_literacy =~  WJ21AEPS   9.550  0.943   4.346    0.353    0.353
## 31       PPVREPF ~~   OWLSEPS   5.279 -0.369  -0.369   -0.042   -0.042
## 32      WJ21AEPF ~~  CSLANGPS   4.185 -2.209  -2.209   -0.048   -0.048
## 33     LETTEREPS ~~  WJ21AEPS   4.142  5.432   5.432    0.047    0.047
# The biggest mi's are all covariances between indicators from the same method (e.g. the Woodcock-Johnson measures, PPVT fall and spring, etc.)
# Add method factors: WJ, PPVT, OWLS, LETTER, Teacher Report (CSLANGP)
# =====================================
# Adding Method Factors
model <- '
# measurement model
fall_literacy =~ LETTEREPF + PPVREPF + OWLSEPF + WJ21AEPF + CSLANGPF
spring_literacy =~ LETTEREPS + PPVREPS + OWLSEPS + WJ21AEPS + CSLANGPS 

# method factors
WJ =~ WJ21AEPF + WJ21AEPS
PPVT =~ PPVREPF + PPVREPS
OWLS =~ OWLSEPF + OWLSEPS
LETTER =~ LETTEREPF + LETTEREPS
Teacher =~ CSLANGPF + CSLANGPS
'
fit <- sem(model, data=df)
## Warning: lavaan WARNING: model has NOT converged!
# It won't run. 
# =====================================
# Correlated errors by method instead
model <- '
# measurement model
fall_literacy =~ LETTEREPF + PPVREPF + OWLSEPF + WJ21AEPF + CSLANGPF
spring_literacy =~ LETTEREPS + PPVREPS + OWLSEPS + WJ21AEPS + CSLANGPS 

# correlated errors
WJ21AEPF ~~ WJ21AEPS
PPVREPF ~~ PPVREPS
OWLSEPF ~~ OWLSEPS
LETTEREPF ~~ LETTEREPS
CSLANGPF ~~ CSLANGPS
'
fit <- sem(model, data=df, std.lv=TRUE)
summary(fit, fit.measures=TRUE, standardized = TRUE)
## lavaan (0.5-16) converged normally after 116 iterations
## 
##                                                   Used       Total
##   Number of observations                           863        1059
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic              202.934
##   Degrees of freedom                                29
##   P-value (Chi-square)                           0.000
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic             5056.967
##   Degrees of freedom                                45
##   P-value                                        0.000
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    0.965
##   Tucker-Lewis Index (TLI)                       0.946
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)             -24149.138
##   Loglikelihood unrestricted model (H1)     -24047.671
## 
##   Number of free parameters                         26
##   Akaike (AIC)                               48350.276
##   Bayesian (BIC)                             48474.047
##   Sample-size adjusted Bayesian (BIC)        48391.478
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.083
##   90 Percent Confidence Interval          0.073  0.094
##   P-value RMSEA <= 0.05                          0.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.064
## 
## Parameter estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
##                    Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
## Latent variables:
##   fall_literacy =~
##     LETTEREPF         4.420    0.310   14.254    0.000    4.420    0.490
##     PPVREPF           2.823    0.111   25.426    0.000    2.823    0.789
##     OWLSEPF           1.963    0.080   24.691    0.000    1.963    0.771
##     WJ21AEPF          6.619    0.324   20.407    0.000    6.619    0.666
##     CSLANGPF          2.304    0.148   15.595    0.000    2.304    0.533
##   spring_literacy =~
##     LETTEREPS         4.467    0.324   13.788    0.000    4.467    0.477
##     PPVREPS           2.678    0.109   24.528    0.000    2.678    0.771
##     OWLSEPS           1.821    0.079   23.135    0.000    1.821    0.738
##     WJ21AEPS          8.346    0.402   20.767    0.000    8.346    0.677
##     CSLANGPS          2.437    0.160   15.207    0.000    2.437    0.523
## 
## Covariances:
##   WJ21AEPF ~~
##     WJ21AEPS         28.471    3.052    9.328    0.000   28.471    0.423
##   PPVREPF ~~
##     PPVREPS           2.501    0.288    8.681    0.000    2.501    0.515
##   OWLSEPF ~~
##     OWLSEPS           1.387    0.148    9.349    0.000    1.387    0.513
##   LETTEREPF ~~
##     LETTEREPS        47.577    2.903   16.389    0.000   47.577    0.736
##   CSLANGPF ~~
##     CSLANGPS          6.937    0.598   11.601    0.000    6.937    0.477
##   fall_literacy ~~
##     spring_litrcy     0.968    0.008  128.366    0.000    0.968    0.968
## 
## Variances:
##     LETTEREPF        61.738    3.158                     61.738    0.760
##     PPVREPF           4.840    0.350                      4.840    0.378
##     OWLSEPF           2.637    0.180                      2.637    0.406
##     WJ21AEPF         54.893    3.142                     54.893    0.556
##     CSLANGPF         13.402    0.698                     13.402    0.716
##     LETTEREPS        67.657    3.453                     67.657    0.772
##     PPVREPS           4.882    0.342                      4.882    0.405
##     OWLSEPS           2.775    0.179                      2.775    0.456
##     WJ21AEPS         82.480    4.800                     82.480    0.542
##     CSLANGPS         15.807    0.822                     15.807    0.727
##     fall_literacy     1.000                               1.000    1.000
##     spring_litrcy     1.000                               1.000    1.000
# The argument standardized=TRUE augments the output with standardized parameter values. Two extra columns of standardized parameter values are printed. In the first column (labeled Std.lv), only the latent variables are standardized. In the second column (labeled Std.all), both latent and observed variables are standardized. The latter is often called the 'completely standardized solution'.
# Way better fit! (RMSEA=.087, CFI=.962).
resid(fit)$cov
##           LETTEREPF PPVREPF OWLSEPF WJ21AEPF CSLANGPF LETTEREPS PPVREPS
## LETTEREPF -0.041                                                       
## PPVREPF   -1.485     0.027                                             
## OWLSEPF   -2.024     0.436   0.007                                     
## WJ21AEPF   7.377    -1.100  -0.107  -0.286                             
## CSLANGPF   7.834    -0.434  -0.415   0.431   -0.008                    
## LETTEREPS -0.050    -0.917  -1.661   4.786    5.853   -0.031           
## PPVREPS   -1.353     0.048   0.319  -0.009   -0.519   -1.449     0.022 
## OWLSEPS   -2.051     0.402   0.006  -0.259   -0.122   -1.832     0.255 
## WJ21AEPS   5.969    -1.033  -0.602  -0.577    0.006    5.164     0.255 
## CSLANGPS   9.282    -0.579  -0.354  -0.155   -0.035    9.529    -0.936 
##           OWLSEPS WJ21AEPS CSLANGPS
## LETTEREPF                          
## PPVREPF                            
## OWLSEPF                            
## WJ21AEPF                           
## CSLANGPF                           
## LETTEREPS                          
## PPVREPS                            
## OWLSEPS    0.000                   
## WJ21AEPS  -0.312  -0.169           
## CSLANGPS  -0.268   1.253   -0.026


mi <- modindices(fit)
# show modification indices, sorting by modification index in decending order (and only showing mi's > 4)
subset(mi[with(mi, order(-mi)), ], mi>4)
##         lhs op       rhs     mi    epc sepc.lv sepc.all sepc.nox
## 1 LETTEREPF ~~  CSLANGPF 21.154  2.807   2.807    0.072    0.072
## 2 LETTEREPS ~~  CSLANGPS 15.049  2.687   2.687    0.062    0.062
## 3 LETTEREPF ~~  WJ21AEPF  7.805  3.662   3.662    0.041    0.041
## 4   PPVREPS ~~  CSLANGPS  5.902 -0.625  -0.625   -0.039   -0.039
## 5   PPVREPF ~~   OWLSEPF  5.119  0.289   0.289    0.032    0.032
## 6 LETTEREPF ~~   PPVREPF  4.982 -0.889  -0.889   -0.028   -0.028
## 7 LETTEREPF ~~   OWLSEPF  4.722 -0.631  -0.631   -0.027   -0.027
## 8  CSLANGPF ~~ LETTEREPS  4.094 -1.286  -1.286   -0.032   -0.032
## 9   PPVREPF ~~  WJ21AEPF  4.004 -1.066  -1.066   -0.030   -0.030
# Interestingly, the biggest mi is now a covariance between how many letters the child can identify and teacher rating of literacy, which makes sense now that I think about it, but adding that covariance would be pretty posthoc. And it could easily be a fluke. Let's go ahead and throw it in, and we can double-check it by validating with the other half of the dataset.
model <- '
# measurement model
fall_literacy =~ LETTEREPF + PPVREPF + OWLSEPF + WJ21AEPF + CSLANGPF
spring_literacy =~ LETTEREPS + PPVREPS + OWLSEPS + WJ21AEPS + CSLANGPS 

# correlated errors
WJ21AEPF ~~ WJ21AEPS
PPVREPF ~~ PPVREPS
OWLSEPF ~~ OWLSEPS
LETTEREPF ~~ LETTEREPS
CSLANGPF ~~ CSLANGPS
CSLANGPF ~~ LETTEREPF
CSLANGPS ~~ LETTEREPS
'
fit <- sem(model, data=df, std.lv=TRUE)
summary(fit, fit.measures=TRUE, standardized = TRUE)
## lavaan (0.5-16) converged normally after 136 iterations
## 
##                                                   Used       Total
##   Number of observations                           863        1059
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic              175.526
##   Degrees of freedom                                27
##   P-value (Chi-square)                           0.000
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic             5056.967
##   Degrees of freedom                                45
##   P-value                                        0.000
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    0.970
##   Tucker-Lewis Index (TLI)                       0.951
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)             -24135.434
##   Loglikelihood unrestricted model (H1)     -24047.671
## 
##   Number of free parameters                         28
##   Akaike (AIC)                               48326.869
##   Bayesian (BIC)                             48460.160
##   Sample-size adjusted Bayesian (BIC)        48371.240
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.080
##   90 Percent Confidence Interval          0.069  0.091
##   P-value RMSEA <= 0.05                          0.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.060
## 
## Parameter estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
##                    Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
## Latent variables:
##   fall_literacy =~
##     LETTEREPF         4.318    0.309   13.952    0.000    4.318    0.482
##     PPVREPF           2.835    0.111   25.534    0.000    2.835    0.792
##     OWLSEPF           1.971    0.079   24.799    0.000    1.971    0.773
##     WJ21AEPF          6.595    0.325   20.322    0.000    6.595    0.664
##     CSLANGPF          2.269    0.147   15.450    0.000    2.269    0.529
##   spring_literacy =~
##     LETTEREPS         4.383    0.322   13.620    0.000    4.383    0.473
##     PPVREPS           2.688    0.109   24.616    0.000    2.688    0.774
##     OWLSEPS           1.827    0.079   23.226    0.000    1.827    0.740
##     WJ21AEPS          8.315    0.402   20.683    0.000    8.315    0.674
##     CSLANGPS          2.400    0.160   14.988    0.000    2.400    0.516
## 
## Covariances:
##   WJ21AEPF ~~
##     WJ21AEPS         28.637    3.061    9.355    0.000   28.637    0.423
##   PPVREPF ~~
##     PPVREPS           2.444    0.289    8.470    0.000    2.444    0.507
##   OWLSEPF ~~
##     OWLSEPS           1.357    0.148    9.154    0.000    1.357    0.506
##   LETTEREPF ~~
##     LETTEREPS        46.356    2.829   16.385    0.000   46.356    0.724
##   CSLANGPF ~~
##     CSLANGPS          6.572    0.583   11.276    0.000    6.572    0.453
##   LETTEREPF ~~
##     CSLANGPF          2.344    0.676    3.467    0.001    2.344    0.082
##   LETTEREPS ~~
##     CSLANGPS          1.882    0.764    2.463    0.014    1.882    0.058
##   fall_literacy ~~
##     spring_litrcy     0.972    0.008  126.545    0.000    0.972    0.972
## 
## Variances:
##     LETTEREPF        61.565    3.119                     61.565    0.768
##     PPVREPF           4.790    0.350                      4.790    0.373
##     OWLSEPF           2.612    0.180                      2.612    0.402
##     WJ21AEPF         55.204    3.148                     55.204    0.559
##     CSLANGPF         13.267    0.688                     13.267    0.720
##     LETTEREPS        66.663    3.384                     66.663    0.776
##     PPVREPS           4.847    0.342                      4.847    0.402
##     OWLSEPS           2.757    0.179                      2.757    0.452
##     WJ21AEPS         82.997    4.807                     82.997    0.546
##     CSLANGPS         15.841    0.820                     15.841    0.733
##     fall_literacy     1.000                               1.000    1.000
##     spring_litrcy     1.000                               1.000    1.000
resid(fit)$cov
##           LETTEREPF PPVREPF OWLSEPF WJ21AEPF CSLANGPF LETTEREPS PPVREPS
## LETTEREPF  1.025                                                       
## PPVREPF   -1.247     0.010                                             
## OWLSEPF   -1.857     0.390   0.001                                     
## WJ21AEPF   8.154    -1.112  -0.113  -0.289                             
## CSLANGPF   5.877    -0.362  -0.365   0.715    0.287                    
## LETTEREPS  1.885    -0.790  -1.571   5.301    6.147    1.707           
## PPVREPS   -1.177     0.016   0.258  -0.085   -0.475   -1.266     0.006 
## OWLSEPS   -1.932     0.341  -0.006  -0.311   -0.093   -1.708     0.220 
## WJ21AEPS   6.772    -1.144  -0.677  -0.591    0.276    5.998     0.257 
## CSLANGPS   9.634    -0.535  -0.322   0.069    0.470    8.013    -0.860 
##           OWLSEPS WJ21AEPS CSLANGPS
## LETTEREPF                          
## PPVREPF                            
## OWLSEPF                            
## WJ21AEPF                           
## CSLANGPF                           
## LETTEREPS                          
## PPVREPS                            
## OWLSEPS   -0.007                   
## WJ21AEPS  -0.312  -0.178           
## CSLANGPS  -0.217   1.634    0.118
# =====================================
# Measurement invariance in fall vs. spring
model <- '
# measurement model
fall_literacy =~ a*LETTEREPF + b*PPVREPF + c*OWLSEPF + d*WJ21AEPF + e*CSLANGPF
spring_literacy =~ a*LETTEREPS + b*PPVREPS + c*OWLSEPS + d*WJ21AEPS + e*CSLANGPS 

# correlated errors
WJ21AEPF ~~ WJ21AEPS
PPVREPF ~~ PPVREPS
OWLSEPF ~~ OWLSEPS
LETTEREPF ~~ LETTEREPS
CSLANGPF ~~ CSLANGPS
CSLANGPF ~~ LETTEREPF
CSLANGPS ~~ LETTEREPS
'
fit <- sem(model, data=df, std.lv=TRUE)
summary(fit, fit.measures=TRUE, standardized = TRUE)
## lavaan (0.5-16) converged normally after 114 iterations
## 
##                                                   Used       Total
##   Number of observations                           863        1059
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic              209.330
##   Degrees of freedom                                32
##   P-value (Chi-square)                           0.000
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic             5056.967
##   Degrees of freedom                                45
##   P-value                                        0.000
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    0.965
##   Tucker-Lewis Index (TLI)                       0.950
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)             -24152.336
##   Loglikelihood unrestricted model (H1)     -24047.671
## 
##   Number of free parameters                         23
##   Akaike (AIC)                               48350.672
##   Bayesian (BIC)                             48460.162
##   Sample-size adjusted Bayesian (BIC)        48387.120
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.080
##   90 Percent Confidence Interval          0.070  0.091
##   P-value RMSEA <= 0.05                          0.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.069
## 
## Parameter estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
##                    Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
## Latent variables:
##   fall_literacy =~
##     LETTEREPF (a)     4.352    0.295   14.772    0.000    4.352    0.485
##     PPVREPF   (b)     2.755    0.101   27.214    0.000    2.755    0.779
##     OWLSEPF   (c)     1.899    0.072   26.263    0.000    1.899    0.758
##     WJ21AEPF  (d)     7.119    0.311   22.895    0.000    7.119    0.696
##     CSLANGPF  (e)     2.323    0.133   17.435    0.000    2.323    0.539
##   spring_literacy =~
##     LETTEREPS (a)     4.352    0.295   14.772    0.000    4.352    0.470
##     PPVREPS   (b)     2.755    0.101   27.214    0.000    2.755    0.783
##     OWLSEPS   (c)     1.899    0.072   26.263    0.000    1.899    0.756
##     WJ21AEPS  (d)     7.119    0.311   22.895    0.000    7.119    0.605
##     CSLANGPS  (e)     2.323    0.133   17.435    0.000    2.323    0.503
## 
## Covariances:
##   WJ21AEPF ~~
##     WJ21AEPS         28.547    3.055    9.346    0.000   28.547    0.416
##   PPVREPF ~~
##     PPVREPS           2.452    0.291    8.420    0.000    2.452    0.506
##   OWLSEPF ~~
##     OWLSEPS           1.350    0.149    9.037    0.000    1.350    0.501
##   LETTEREPF ~~
##     LETTEREPS        46.349    2.832   16.368    0.000   46.349    0.723
##   CSLANGPF ~~
##     CSLANGPS          6.579    0.584   11.271    0.000    6.579    0.453
##   LETTEREPF ~~
##     CSLANGPF          2.275    0.672    3.386    0.001    2.275    0.080
##   LETTEREPS ~~
##     CSLANGPS          1.984    0.760    2.609    0.009    1.984    0.061
##   fall_literacy ~~
##     spring_litrcy     0.974    0.008  125.443    0.000    0.974    0.974
## 
## Variances:
##     LETTEREPF        61.483    3.117                     61.483    0.764
##     PPVREPF           4.919    0.346                      4.919    0.393
##     OWLSEPF           2.679    0.177                      2.679    0.426
##     WJ21AEPF         53.883    3.169                     53.883    0.515
##     CSLANGPF         13.207    0.686                     13.207    0.710
##     LETTEREPS        66.767    3.381                     66.767    0.779
##     PPVREPS           4.778    0.343                      4.778    0.386
##     OWLSEPS           2.706    0.180                      2.706    0.429
##     WJ21AEPS         87.600    4.753                     87.600    0.634
##     CSLANGPS         15.945    0.818                     15.945    0.747
##     fall_literacy     1.000                               1.000    1.000
##     spring_litrcy     1.000                               1.000    1.000
# looks like we can assume invariant loadings! yay.
resid(fit)$cov
##           LETTEREPF PPVREPF OWLSEPF WJ21AEPF CSLANGPF LETTEREPS PPVREPS
## LETTEREPF  0.807                                                       
## PPVREPF   -1.000     0.324                                             
## OWLSEPF   -1.612     0.745   0.212                                     
## WJ21AEPF   5.647    -2.032  -0.633  -6.148                             
## CSLANGPF   5.632    -0.331  -0.304  -0.857    0.100                    
## LETTEREPS  1.843    -0.389  -1.222   3.233    5.970    1.869           
## PPVREPS   -1.572     0.022   0.313  -1.951   -0.779   -1.478    -0.292 
## OWLSEPS   -2.312     0.281  -0.010  -1.761   -0.358   -1.966    -0.102 
## WJ21AEPS  11.505     2.670   2.093   3.467    2.516   11.460     2.992 
## CSLANGPS   9.863    -0.154  -0.019  -0.647    0.503    8.319    -0.810 
##           OWLSEPS WJ21AEPS CSLANGPS
## LETTEREPF                          
## PPVREPF                            
## OWLSEPF                            
## WJ21AEPF                           
## CSLANGPF                           
## LETTEREPS                          
## PPVREPS                            
## OWLSEPS   -0.224                   
## WJ21AEPS   1.361  13.686           
## CSLANGPS  -0.244   5.053    0.376

# =====================================
# SEM growth model, with mother education as a predictor of intercept and change
model <- '
# measurement model
fall_literacy =~ a*LETTEREPF + b*PPVREPF + c*OWLSEPF + d*WJ21AEPF + e*CSLANGPF
spring_literacy =~ a*LETTEREPS + b*PPVREPS + c*OWLSEPS + d*WJ21AEPS + e*CSLANGPS

# do not fix latent factor variances for s and i to 1 (override default)
s ~~ NA*s
i ~~ NA*i

# error covariances
WJ21AEPF ~~ WJ21AEPS
PPVREPF ~~ PPVREPS
OWLSEPF ~~ OWLSEPS
LETTEREPF ~~ LETTEREPS
CSLANGPF ~~ CSLANGPS
CSLANGPF ~~ LETTEREPF
CSLANGPS ~~ LETTEREPS

# latent covariance
i ~~ s

# structural model
i =~ 1*fall_literacy + 1*spring_literacy
s =~ -1*fall_literacy + 1*spring_literacy
i ~ CHMATEDNP 
s ~ CHMATEDNP
'
fit <- sem(model, data=df, std.lv=TRUE)
## Warning: lavaan WARNING: could not compute standard errors!
## 
## Warning: lavaan WARNING: some estimated variances are negative
## Warning: lavaan WARNING: covariance matrix of latent variables is not positive definite; use inspect(fit,"cov.lv") to investigate.
# Heywood case. Negative variance for s. Set it to zero.
inspect(fit,"cov.lv")
##                 fll_lt sprng_ i     s    
## fall_literacy    4.70                    
## spring_literacy  4.54   4.62             
## i                4.12   4.08   4.10      
## s                0.42  -0.46  -0.02 -0.44

# Set variance of s to zero.
model <- '
# measurement model
fall_literacy =~ a*LETTEREPF + b*PPVREPF + c*OWLSEPF + d*WJ21AEPF + e*CSLANGPF
spring_literacy =~ a*LETTEREPS + b*PPVREPS + c*OWLSEPS + d*WJ21AEPS + e*CSLANGPS

# do not fix latent factor variances for i to 1 (override default)
# set var of s to zero (it is almost zero anyway)
s ~~ 0*s
i ~~ NA*i

# error covariances
WJ21AEPF ~~ WJ21AEPS
PPVREPF ~~ PPVREPS
OWLSEPF ~~ OWLSEPS
LETTEREPF ~~ LETTEREPS
CSLANGPF ~~ CSLANGPS
CSLANGPF ~~ LETTEREPF
CSLANGPS ~~ LETTEREPS

i ~~ s

# structural model
i =~ 1*fall_literacy + 1*spring_literacy
s =~ -1*fall_literacy + 1*spring_literacy
i ~ CHMATEDNP 
s ~ CHMATEDNP
'
fit <- sem(model, data=df, std.lv=TRUE)
## Warning: lavaan WARNING: covariance matrix of latent variables is not
## positive definite; use inspect(fit,"cov.lv") to investigate.
# Still problematic latent cov matrix. Not clear why...
inspect(fit,"cov.lv")
##                 fll_lt sprng_ i      s     
## fall_literacy   39.228                     
## spring_literacy 37.893 38.560              
## i               38.061 37.727 37.894       
## s               -0.168 -0.166 -0.167  0.001
# Try another approach...
# =====================================
# SEM with maternal education as a preidctor of spring literacy, controlling for fall literacy.
model <- '
# measurement model
fall_literacy =~ a*LETTEREPF + b*PPVREPF + c*OWLSEPF + d*WJ21AEPF + e*CSLANGPF
spring_literacy =~ a*LETTEREPS + b*PPVREPS + c*OWLSEPS + d*WJ21AEPS + e*CSLANGPS 

# correlated errors
WJ21AEPF ~~ WJ21AEPS
PPVREPF ~~ PPVREPS
OWLSEPF ~~ OWLSEPS
LETTEREPF ~~ LETTEREPS
CSLANGPF ~~ CSLANGPS
CSLANGPF ~~ LETTEREPF
CSLANGPS ~~ LETTEREPS

# structural model
spring_literacy ~ fall_literacy + CHMATEDNP
fall_literacy ~~ CHMATEDNP
'
fit <- sem(model, data=df, std.lv=TRUE, fixed.x=FALSE)
summary(fit, fit.measures=TRUE, standardized = TRUE)
## lavaan (0.5-16) converged normally after 111 iterations
## 
##                                                   Used       Total
##   Number of observations                           847        1059
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic              640.135
##   Degrees of freedom                                40
##   P-value (Chi-square)                           0.000
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic             5108.476
##   Degrees of freedom                                55
##   P-value                                        0.000
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    0.881
##   Tucker-Lewis Index (TLI)                       0.837
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)             -25741.042
##   Loglikelihood unrestricted model (H1)     -25420.975
## 
##   Number of free parameters                         26
##   Akaike (AIC)                               51534.084
##   Bayesian (BIC)                             51657.369
##   Sample-size adjusted Bayesian (BIC)        51574.800
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.133
##   90 Percent Confidence Interval          0.124  0.142
##   P-value RMSEA <= 0.05                          0.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.176
## 
## Parameter estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
##                    Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
## Latent variables:
##   fall_literacy =~
##     LETTEREPF (a)     2.902    0.193   15.061    0.000    2.902    0.347
##     PPVREPF   (b)     1.823    0.072   25.447    0.000    1.823    0.594
##     OWLSEPF   (c)     1.258    0.051   24.435    0.000    1.258    0.572
##     WJ21AEPF  (d)     5.702    0.245   23.311    0.000    5.702    0.629
##     CSLANGPF  (e)     1.707    0.104   16.489    0.000    1.707    0.425
##   spring_literacy =~
##     LETTEREPS (a)     2.902    0.193   15.061    0.000    4.103    0.449
##     PPVREPS   (b)     1.823    0.072   25.447    0.000    2.577    0.751
##     OWLSEPS   (c)     1.258    0.051   24.435    0.000    1.778    0.725
##     WJ21AEPS  (d)     5.702    0.245   23.311    0.000    8.061    0.671
##     CSLANGPS  (e)     1.707    0.104   16.489    0.000    2.413    0.519
## 
## Regressions:
##   spring_literacy ~
##     fall_literacy     0.962    0.059   16.212    0.000    0.680    0.680
##     CHMATEDNP         0.038    0.021    1.776    0.076    0.027    0.060
## 
## Covariances:
##   WJ21AEPF ~~
##     WJ21AEPS         26.364    2.989    8.820    0.000   26.364    0.420
##   PPVREPF ~~
##     PPVREPS           3.301    0.292   11.300    0.000    3.301    0.590
##   OWLSEPF ~~
##     OWLSEPS           1.746    0.152   11.498    0.000    1.746    0.573
##   LETTEREPF ~~
##     LETTEREPS        46.372    2.824   16.419    0.000   46.372    0.724
##   CSLANGPF ~~
##     CSLANGPS          6.565    0.589   11.153    0.000    6.565    0.453
##   LETTEREPF ~~
##     CSLANGPF          2.170    0.697    3.114    0.002    2.170    0.076
##   LETTEREPS ~~
##     CSLANGPS          1.845    0.805    2.294    0.022    1.845    0.057
##   fall_literacy ~~
##     CHMATEDNP         0.921    0.089   10.290    0.000    0.921    0.921
## 
## Variances:
##     LETTEREPF        61.530    3.119                     61.530    0.880
##     PPVREPF           6.109    0.369                      6.109    0.648
##     OWLSEPF           3.247    0.191                      3.247    0.672
##     WJ21AEPF         49.761    3.197                     49.761    0.605
##     CSLANGPF         13.253    0.698                     13.253    0.820
##     LETTEREPS        66.695    3.435                     66.695    0.798
##     PPVREPS           5.128    0.371                      5.128    0.436
##     OWLSEPS           2.861    0.193                      2.861    0.475
##     WJ21AEPS         79.353    4.839                     79.353    0.550
##     CSLANGPS         15.833    0.845                     15.833    0.731
##     CHMATEDNP         5.043    0.244                      5.043    1.000
##     fall_literacy     1.000                               1.000    1.000
##     spring_litrcy     1.000                               0.500    0.500
resid(fit)$cov
##           LETTEREPF PPVREPF OWLSEPF WJ21AEPF CSLANGPF LETTEREPS PPVREPS
## LETTEREPF 10.718                                                       
## PPVREPF    5.447     3.447                                             
## OWLSEPF    2.890     3.725   1.692                                     
## WJ21AEPF  18.893     7.044   5.689  14.259                             
## CSLANGPF  10.588     2.967   1.935   5.310    2.479                    
## LETTEREPS 11.230     5.758   3.009  15.943   10.626    3.455           
## PPVREPS    4.623     3.279   3.126   6.722    2.360   -0.304     0.309 
## OWLSEPS    1.965     3.093   1.512   4.095    1.746   -1.260     0.506 
## WJ21AEPS  23.997    11.315   8.065  20.238    8.287    8.524     1.820 
## CSLANGPS  14.478     2.888   2.065   5.249    2.739    8.316    -0.693 
## CHMATEDNP  2.938     1.404   0.790   1.613    0.993    2.256     0.877 
##           OWLSEPS WJ21AEPS CSLANGPS CHMATE
## LETTEREPF                                 
## PPVREPF                                   
## OWLSEPF                                   
## WJ21AEPF                                  
## CSLANGPF                                  
## LETTEREPS                                 
## PPVREPS                                   
## OWLSEPS    0.004                          
## WJ21AEPS   0.440   5.951                  
## CSLANGPS  -0.262   1.643   -0.079         
## CHMATEDNP  0.566   3.580    1.058    0.355

# It runs! It's ALIIIIIVVVVEE!!!
# =====================================
# Validity time!
child_info <- subset(df2, select=c("ASMTAGEPF", "ASMTAGEPS", "CHGENP", "CHMATEDNP", "CHINCOMNP", "POORP"))
child_info$POORP <- as.numeric(child_info$POORP) - 1
child_info$CHGENP <- as.numeric(child_info$CHGENP) - 1
fall_lit <- subset(df2, select=c("LETTEREPF", "PPVREPF", "OWLSEPF",  "WJ21AEPF", "CSLANGPF"))
spring_lit <- subset(df2, select=c("LETTEREPS", "PPVREPS", "OWLSEPS", "WJ21AEPS", "CSLANGPS"))

df <- cbind(fall_lit, spring_lit, child_info)

# =====================================
# CFA
model <- '
fall_literacy =~ LETTEREPF + PPVREPF + OWLSEPF + WJ21AEPF + CSLANGPF
spring_literacy =~ LETTEREPS + PPVREPS + OWLSEPS + WJ21AEPS + CSLANGPS 
'

fit <- cfa(model, data=df)
## Warning: lavaan WARNING: covariance matrix of latent variables is not
## positive definite; use inspect(fit,"cov.lv") to investigate.
summary(fit, fit.measures=TRUE, standardized = TRUE)
## lavaan (0.5-16) converged normally after  82 iterations
## 
##                                                   Used       Total
##   Number of observations                           852        1060
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic             1540.470
##   Degrees of freedom                                34
##   P-value (Chi-square)                           0.000
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic             4899.330
##   Degrees of freedom                                45
##   P-value                                        0.000
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    0.690
##   Tucker-Lewis Index (TLI)                       0.589
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)             -24514.694
##   Loglikelihood unrestricted model (H1)     -23744.459
## 
##   Number of free parameters                         21
##   Akaike (AIC)                               49071.388
##   Bayesian (BIC)                             49171.087
##   Sample-size adjusted Bayesian (BIC)        49104.398
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.228
##   90 Percent Confidence Interval          0.218  0.238
##   P-value RMSEA <= 0.05                          0.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.116
## 
## Parameter estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
##                    Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
## Latent variables:
##   fall_literacy =~
##     LETTEREPF         1.000                               4.915    0.542
##     PPVREPF           0.558    0.034   16.561    0.000    2.744    0.786
##     OWLSEPF           0.372    0.024   15.754    0.000    1.828    0.718
##     WJ21AEPF          1.165    0.081   14.401    0.000    5.726    0.621
##     CSLANGPF          0.449    0.037   12.267    0.000    2.206    0.493
##   spring_literacy =~
##     LETTEREPS         1.000                               5.022    0.535
##     PPVREPS           0.548    0.034   16.318    0.000    2.752    0.792
##     OWLSEPS           0.357    0.023   15.441    0.000    1.793    0.715
##     WJ21AEPS          1.558    0.106   14.662    0.000    7.824    0.655
##     CSLANGPS          0.492    0.039   12.561    0.000    2.469    0.518
## 
## Covariances:
##   fall_literacy ~~
##     spring_litrcy    27.032    2.422   11.162    0.000    1.095    1.095
## 
## Variances:
##     LETTEREPF        58.034    2.859                     58.034    0.706
##     PPVREPF           4.666    0.257                      4.666    0.383
##     OWLSEPF           3.132    0.162                      3.132    0.484
##     WJ21AEPF         52.303    2.610                     52.303    0.615
##     CSLANGPF         15.131    0.742                     15.131    0.757
##     LETTEREPS        62.908    3.103                     62.908    0.714
##     PPVREPS           4.503    0.250                      4.503    0.373
##     OWLSEPS           3.075    0.159                      3.075    0.489
##     WJ21AEPS         81.618    4.119                     81.618    0.571
##     CSLANGPS         16.605    0.818                     16.605    0.731
##     fall_literacy    24.158    2.867                      1.000    1.000
##     spring_litrcy    25.221    3.047                      1.000    1.000
resid(fit)$cov
##           LETTEREPF PPVREPF OWLSEPF WJ21AEPF CSLANGPF LETTEREPS PPVREPS
## LETTEREPF  0.000                                                       
## PPVREPF   -1.440     0.000                                             
## OWLSEPF   -2.972     0.395   0.000                                     
## WJ21AEPF   7.491    -1.188   0.124   0.000                             
## CSLANGPF   9.169    -0.514  -0.279   2.304    0.000                    
## LETTEREPS 39.832    -3.070  -3.751  -0.259    5.203    0.000           
## PPVREPS   -3.856     1.478  -0.086  -2.248   -1.680   -1.808     0.000 
## OWLSEPS   -3.513    -0.275   1.268  -1.616   -0.755   -2.155     0.529 
## WJ21AEPS   1.712    -2.558  -2.012  22.472   -0.500    5.464     0.358 
## CSLANGPS   7.534    -1.607  -1.262   0.907    6.863    8.201    -1.373 
##           OWLSEPS WJ21AEPS CSLANGPS
## LETTEREPF                          
## PPVREPF                            
## OWLSEPF                            
## WJ21AEPF                           
## CSLANGPF                           
## LETTEREPS                          
## PPVREPS                            
## OWLSEPS    0.000                   
## WJ21AEPS  -0.271   0.000           
## CSLANGPS  -0.595   2.201    0.000

# =====================================
# Correlated errors by method
model <- '
# measurement model
fall_literacy =~ LETTEREPF + PPVREPF + OWLSEPF + WJ21AEPF + CSLANGPF
spring_literacy =~ LETTEREPS + PPVREPS + OWLSEPS + WJ21AEPS + CSLANGPS 

# correlated errors
WJ21AEPF ~~ WJ21AEPS
PPVREPF ~~ PPVREPS
OWLSEPF ~~ OWLSEPS
LETTEREPF ~~ LETTEREPS
CSLANGPF ~~ CSLANGPS
'
fit <- sem(model, data=df, std.lv=TRUE)
summary(fit, fit.measures=TRUE, standardized = TRUE)
## lavaan (0.5-16) converged normally after 119 iterations
## 
##                                                   Used       Total
##   Number of observations                           852        1060
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic              238.186
##   Degrees of freedom                                29
##   P-value (Chi-square)                           0.000
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic             4899.330
##   Degrees of freedom                                45
##   P-value                                        0.000
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    0.957
##   Tucker-Lewis Index (TLI)                       0.933
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)             -23863.552
##   Loglikelihood unrestricted model (H1)     -23744.459
## 
##   Number of free parameters                         26
##   Akaike (AIC)                               47779.104
##   Bayesian (BIC)                             47902.541
##   Sample-size adjusted Bayesian (BIC)        47819.973
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.092
##   90 Percent Confidence Interval          0.081  0.103
##   P-value RMSEA <= 0.05                          0.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.069
## 
## Parameter estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
##                    Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
## Latent variables:
##   fall_literacy =~
##     LETTEREPF         4.891    0.313   15.633    0.000    4.891    0.538
##     PPVREPF           2.696    0.112   24.165    0.000    2.696    0.772
##     OWLSEPF           1.819    0.083   21.951    0.000    1.819    0.715
##     WJ21AEPF          5.894    0.310   18.994    0.000    5.894    0.639
##     CSLANGPF          2.296    0.156   14.722    0.000    2.296    0.514
##   spring_literacy =~
##     LETTEREPS         4.921    0.325   15.128    0.000    4.921    0.523
##     PPVREPS           2.726    0.110   24.830    0.000    2.726    0.786
##     OWLSEPS           1.788    0.081   21.951    0.000    1.788    0.714
##     WJ21AEPS          8.125    0.394   20.622    0.000    8.125    0.680
##     CSLANGPS          2.451    0.166   14.730    0.000    2.451    0.513
## 
## Covariances:
##   WJ21AEPF ~~
##     WJ21AEPS         25.366    2.841    8.927    0.000   25.366    0.408
##   PPVREPF ~~
##     PPVREPS           2.617    0.296    8.838    0.000    2.617    0.550
##   OWLSEPF ~~
##     OWLSEPS           1.717    0.163   10.540    0.000    1.717    0.549
##   LETTEREPF ~~
##     LETTEREPS        44.182    2.827   15.629    0.000   44.182    0.720
##   CSLANGPF ~~
##     CSLANGPS          7.429    0.649   11.455    0.000    7.429    0.473
##   fall_literacy ~~
##     spring_litrcy     0.964    0.008  122.592    0.000    0.964    0.964
## 
## Variances:
##     LETTEREPF        58.606    3.105                     58.606    0.710
##     PPVREPF           4.918    0.358                      4.918    0.404
##     OWLSEPF           3.171    0.200                      3.171    0.489
##     WJ21AEPF         50.325    2.886                     50.325    0.592
##     CSLANGPF         14.681    0.770                     14.681    0.736
##     LETTEREPS        64.336    3.379                     64.336    0.727
##     PPVREPS           4.606    0.345                      4.606    0.383
##     OWLSEPS           3.080    0.193                      3.080    0.491
##     WJ21AEPS         76.857    4.576                     76.857    0.538
##     CSLANGPS         16.775    0.878                     16.775    0.736
##     fall_literacy     1.000                               1.000    1.000
##     spring_litrcy     1.000                               1.000    1.000
resid(fit)$cov
##           LETTEREPF PPVREPF OWLSEPF WJ21AEPF CSLANGPF LETTEREPS PPVREPS
## LETTEREPF -0.334                                                       
## PPVREPF   -1.137     0.011                                             
## OWLSEPF   -2.887     0.506  -0.008                                     
## WJ21AEPF   6.807    -1.365  -0.134   0.024                             
## CSLANGPF   8.785    -0.648  -0.423   1.406    0.047                    
## LETTEREPS -0.523    -0.769  -2.331   3.267    6.445   -0.422           
## PPVREPS   -1.898     0.045   0.640  -0.484   -1.066   -1.402     0.038 
## OWLSEPS   -2.295     0.465   0.003  -0.536   -0.382   -1.952     0.588 
## WJ21AEPS   5.510    -0.165  -0.604  -0.009    0.418    4.771    -0.262 
## CSLANGPS   9.270    -0.556  -0.618   2.464   -0.024    8.543    -1.258 
##           OWLSEPS WJ21AEPS CSLANGPS
## LETTEREPF                          
## PPVREPF                            
## OWLSEPF                            
## WJ21AEPF                           
## CSLANGPF                           
## LETTEREPS                          
## PPVREPS                            
## OWLSEPS    0.011                   
## WJ21AEPS  -0.774  -0.046           
## CSLANGPS  -0.550   1.609   -0.078


# add covariances between LETTER and CSLANG variables
model <- '
# measurement model
fall_literacy =~ LETTEREPF + PPVREPF + OWLSEPF + WJ21AEPF + CSLANGPF
spring_literacy =~ LETTEREPS + PPVREPS + OWLSEPS + WJ21AEPS + CSLANGPS 

# correlated errors
WJ21AEPF ~~ WJ21AEPS
PPVREPF ~~ PPVREPS
OWLSEPF ~~ OWLSEPS
LETTEREPF ~~ LETTEREPS
CSLANGPF ~~ CSLANGPS
CSLANGPF ~~ LETTEREPF
CSLANGPS ~~ LETTEREPS
'
fit <- sem(model, data=df, std.lv=TRUE)
summary(fit, fit.measures=TRUE, standardized = TRUE)
## lavaan (0.5-16) converged normally after 136 iterations
## 
##                                                   Used       Total
##   Number of observations                           852        1060
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic              214.075
##   Degrees of freedom                                27
##   P-value (Chi-square)                           0.000
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic             4899.330
##   Degrees of freedom                                45
##   P-value                                        0.000
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    0.961
##   Tucker-Lewis Index (TLI)                       0.936
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)             -23851.497
##   Loglikelihood unrestricted model (H1)     -23744.459
## 
##   Number of free parameters                         28
##   Akaike (AIC)                               47758.993
##   Bayesian (BIC)                             47891.926
##   Sample-size adjusted Bayesian (BIC)        47803.006
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.090
##   90 Percent Confidence Interval          0.079  0.102
##   P-value RMSEA <= 0.05                          0.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.066
## 
## Parameter estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
##                    Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
## Latent variables:
##   fall_literacy =~
##     LETTEREPF         4.762    0.312   15.278    0.000    4.762    0.529
##     PPVREPF           2.712    0.112   24.326    0.000    2.712    0.777
##     OWLSEPF           1.831    0.083   22.120    0.000    1.831    0.719
##     WJ21AEPF          5.859    0.311   18.860    0.000    5.859    0.635
##     CSLANGPF          2.238    0.155   14.427    0.000    2.238    0.506
##   spring_literacy =~
##     LETTEREPS         4.840    0.325   14.899    0.000    4.840    0.517
##     PPVREPS           2.743    0.110   24.995    0.000    2.743    0.790
##     OWLSEPS           1.798    0.081   22.078    0.000    1.798    0.717
##     WJ21AEPS          8.095    0.394   20.543    0.000    8.095    0.677
##     CSLANGPS          2.412    0.166   14.491    0.000    2.412    0.507
## 
## Covariances:
##   WJ21AEPF ~~
##     WJ21AEPS         25.673    2.853    8.998    0.000   25.673    0.410
##   PPVREPF ~~
##     PPVREPS           2.520    0.296    8.505    0.000    2.520    0.538
##   OWLSEPF ~~
##     OWLSEPS           1.676    0.162   10.320    0.000    1.676    0.542
##   LETTEREPF ~~
##     LETTEREPS        43.361    2.765   15.684    0.000   43.361    0.708
##   CSLANGPF ~~
##     CSLANGPS          7.050    0.632   11.162    0.000    7.050    0.450
##   LETTEREPF ~~
##     CSLANGPF          2.978    0.719    4.142    0.000    2.978    0.102
##   LETTEREPS ~~
##     CSLANGPS          0.832    0.795    1.046    0.295    0.832    0.025
##   fall_literacy ~~
##     spring_litrcy     0.969    0.008  121.812    0.000    0.969    0.969
## 
## Variances:
##     LETTEREPF        58.418    3.049                     58.418    0.720
##     PPVREPF           4.838    0.358                      4.838    0.397
##     OWLSEPF           3.131    0.199                      3.131    0.483
##     WJ21AEPF         50.777    2.896                     50.777    0.597
##     CSLANGPF         14.575    0.759                     14.575    0.744
##     LETTEREPS        64.201    3.358                     64.201    0.733
##     PPVREPS           4.537    0.344                      4.537    0.376
##     OWLSEPS           3.057    0.193                      3.057    0.486
##     WJ21AEPS         77.393    4.580                     77.393    0.541
##     CSLANGPS         16.837    0.879                     16.837    0.743
##     fall_literacy     1.000                               1.000    1.000
##     spring_litrcy     1.000                               1.000    1.000
resid(fit)
## $cov
##           LETTEREPF PPVREPF OWLSEPF WJ21AEPF CSLANGPF LETTEREPS PPVREPS
## LETTEREPF  1.099                                                       
## PPVREPF   -0.869     0.000                                             
## OWLSEPF   -2.709     0.442  -0.013                                     
## WJ21AEPF   7.735    -1.369  -0.141  -0.015                             
## CSLANGPF   6.378    -0.530  -0.345   1.825    0.416                    
## LETTEREPS  1.161    -0.706  -2.292   3.743    6.837    0.505           
## PPVREPS   -1.707     0.014   0.552  -0.573   -0.983   -1.263     0.014 
## OWLSEPS   -2.163     0.384  -0.012  -0.587   -0.324   -1.853     0.531 
## WJ21AEPS   6.452    -0.336  -0.725  -0.125    0.840    5.575    -0.319 
## CSLANGPS   9.693    -0.528  -0.601   2.693    0.547    8.099    -1.192 
##           OWLSEPS WJ21AEPS CSLANGPS
## LETTEREPF                          
## PPVREPF                            
## OWLSEPF                            
## WJ21AEPF                           
## CSLANGPF                           
## LETTEREPS                          
## PPVREPS                            
## OWLSEPS    0.000                   
## WJ21AEPS  -0.800  -0.103           
## CSLANGPS  -0.504   1.998    0.051  
## 
## $mean
## LETTEREPF   PPVREPF   OWLSEPF  WJ21AEPF  CSLANGPF LETTEREPS   PPVREPS 
##         0         0         0         0         0         0         0 
##   OWLSEPS  WJ21AEPS  CSLANGPS 
##         0         0         0

# =====================================
# Measurement invariance in fall vs. spring
model <- '
# measurement model
fall_literacy =~ a*LETTEREPF + b*PPVREPF + c*OWLSEPF + d*WJ21AEPF + e*CSLANGPF
spring_literacy =~ a*LETTEREPS + b*PPVREPS + c*OWLSEPS + d*WJ21AEPS + e*CSLANGPS 

# correlated errors
WJ21AEPF ~~ WJ21AEPS
PPVREPF ~~ PPVREPS
OWLSEPF ~~ OWLSEPS
LETTEREPF ~~ LETTEREPS
CSLANGPF ~~ CSLANGPS
CSLANGPF ~~ LETTEREPF
CSLANGPS ~~ LETTEREPS
'
fit <- sem(model, data=df, std.lv=TRUE)
summary(fit, fit.measures=TRUE, standardized = TRUE)
## lavaan (0.5-16) converged normally after 118 iterations
## 
##                                                   Used       Total
##   Number of observations                           852        1060
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic              256.810
##   Degrees of freedom                                32
##   P-value (Chi-square)                           0.000
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic             4899.330
##   Degrees of freedom                                45
##   P-value                                        0.000
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    0.954
##   Tucker-Lewis Index (TLI)                       0.935
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)             -23872.864
##   Loglikelihood unrestricted model (H1)     -23744.459
## 
##   Number of free parameters                         23
##   Akaike (AIC)                               47791.728
##   Bayesian (BIC)                             47900.923
##   Sample-size adjusted Bayesian (BIC)        47827.881
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.091
##   90 Percent Confidence Interval          0.081  0.101
##   P-value RMSEA <= 0.05                          0.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.078
## 
## Parameter estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
##                    Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
## Latent variables:
##   fall_literacy =~
##     LETTEREPF (a)     4.810    0.296   16.248    0.000    4.810    0.533
##     PPVREPF   (b)     2.719    0.102   26.564    0.000    2.719    0.775
##     OWLSEPF   (c)     1.819    0.075   24.233    0.000    1.819    0.717
##     WJ21AEPF  (d)     6.530    0.298   21.898    0.000    6.530    0.681
##     CSLANGPF  (e)     2.321    0.139   16.690    0.000    2.321    0.521
##   spring_literacy =~
##     LETTEREPS (a)     4.810    0.296   16.248    0.000    4.810    0.514
##     PPVREPS   (b)     2.719    0.102   26.564    0.000    2.719    0.787
##     OWLSEPS   (c)     1.819    0.075   24.233    0.000    1.819    0.723
##     WJ21AEPS  (d)     6.530    0.298   21.898    0.000    6.530    0.581
##     CSLANGPS  (e)     2.321    0.139   16.690    0.000    2.321    0.492
## 
## Covariances:
##   WJ21AEPF ~~
##     WJ21AEPS         25.675    2.853    8.999    0.000   25.675    0.399
##   PPVREPF ~~
##     PPVREPS           2.577    0.300    8.577    0.000    2.577    0.544
##   OWLSEPF ~~
##     OWLSEPS           1.660    0.163   10.168    0.000    1.660    0.540
##   LETTEREPF ~~
##     LETTEREPS        43.395    2.770   15.665    0.000   43.395    0.709
##   CSLANGPF ~~
##     CSLANGPS          7.021    0.631   11.118    0.000    7.021    0.449
##   LETTEREPF ~~
##     CSLANGPF          2.896    0.713    4.061    0.000    2.896    0.100
##   LETTEREPS ~~
##     CSLANGPS          0.899    0.791    1.137    0.256    0.899    0.027
##   fall_literacy ~~
##     spring_litrcy     0.968    0.008  118.728    0.000    0.968    0.968
## 
## Variances:
##     LETTEREPF        58.331    3.045                     58.331    0.716
##     PPVREPF           4.918    0.357                      4.918    0.399
##     OWLSEPF           3.135    0.197                      3.135    0.487
##     WJ21AEPF         49.406    2.931                     49.406    0.537
##     CSLANGPF         14.487    0.757                     14.487    0.729
##     LETTEREPS        64.288    3.359                     64.288    0.735
##     PPVREPS           4.555    0.344                      4.555    0.381
##     OWLSEPS           3.014    0.193                      3.014    0.477
##     WJ21AEPS         83.863    4.552                     83.863    0.663
##     CSLANGPS         16.886    0.875                     16.886    0.758
##     fall_literacy     1.000                               1.000    1.000
##     spring_litrcy     1.000                               1.000    1.000
resid(fit)
## $cov
##           LETTEREPF PPVREPF OWLSEPF WJ21AEPF CSLANGPF LETTEREPS PPVREPS
## LETTEREPF  0.725                                                       
## PPVREPF   -1.033    -0.117                                             
## OWLSEPF   -2.737     0.464   0.029                                     
## WJ21AEPF   4.222    -3.234  -1.288  -6.965                             
## CSLANGPF   5.952    -0.772  -0.468  -0.223    0.123                    
## LETTEREPS  1.072    -0.642  -2.168   0.823    6.528    0.705           
## PPVREPS   -1.706     0.012   0.634  -2.185   -1.143   -1.068     0.125 
## OWLSEPS   -2.330     0.325  -0.005  -1.871   -0.510   -1.899     0.517 
## WJ21AEPS  13.416     3.761   2.150   4.571    3.728   13.343     4.128 
## CSLANGPS  10.017    -0.297  -0.406   1.716    0.592    8.538    -0.890 
##           OWLSEPS WJ21AEPS CSLANGPS
## LETTEREPF                          
## PPVREPF                            
## OWLSEPF                            
## WJ21AEPF                           
## CSLANGPF                           
## LETTEREPS                          
## PPVREPS                            
## OWLSEPS   -0.033                   
## WJ21AEPS   1.879  16.318           
## CSLANGPS  -0.389   6.362    0.429  
## 
## $mean
## LETTEREPF   PPVREPF   OWLSEPF  WJ21AEPF  CSLANGPF LETTEREPS   PPVREPS 
##         0         0         0         0         0         0         0 
##   OWLSEPS  WJ21AEPS  CSLANGPS 
##         0         0         0

# =====================================
# SEM with maternal education as a preidctor of spring literacy, controlling for fall literacy.
model <- '
# measurement model
fall_literacy =~ a*LETTEREPF + b*PPVREPF + c*OWLSEPF + d*WJ21AEPF + e*CSLANGPF
spring_literacy =~ a*LETTEREPS + b*PPVREPS + c*OWLSEPS + d*WJ21AEPS + e*CSLANGPS 

# correlated errors
WJ21AEPF ~~ WJ21AEPS
PPVREPF ~~ PPVREPS
OWLSEPF ~~ OWLSEPS
LETTEREPF ~~ LETTEREPS
CSLANGPF ~~ CSLANGPS
CSLANGPF ~~ LETTEREPF
CSLANGPS ~~ LETTEREPS

# structural model
spring_literacy ~ fall_literacy + CHMATEDNP
fall_literacy ~~ CHMATEDNP
'
fit <- sem(model, data=df, std.lv=TRUE, fixed.x=FALSE)
summary(fit, fit.measures=TRUE, standardized = TRUE)
## lavaan (0.5-16) converged normally after 110 iterations
## 
##                                                   Used       Total
##   Number of observations                           834        1060
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic              612.910
##   Degrees of freedom                                40
##   P-value (Chi-square)                           0.000
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic             4982.462
##   Degrees of freedom                                55
##   P-value                                        0.000
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    0.884
##   Tucker-Lewis Index (TLI)                       0.840
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)             -25326.377
##   Loglikelihood unrestricted model (H1)     -25019.922
## 
##   Number of free parameters                         26
##   Akaike (AIC)                               50704.754
##   Bayesian (BIC)                             50827.636
##   Sample-size adjusted Bayesian (BIC)        50745.069
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.131
##   90 Percent Confidence Interval          0.122  0.140
##   P-value RMSEA <= 0.05                          0.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.171
## 
## Parameter estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
##                    Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
## Latent variables:
##   fall_literacy =~
##     LETTEREPF (a)     3.300    0.196   16.855    0.000    3.300    0.396
##     PPVREPF   (b)     1.759    0.071   24.811    0.000    1.759    0.577
##     OWLSEPF   (c)     1.192    0.053   22.683    0.000    1.192    0.528
##     WJ21AEPF  (d)     5.217    0.242   21.569    0.000    5.217    0.603
##     CSLANGPF  (e)     1.719    0.107   16.020    0.000    1.719    0.415
##   spring_literacy =~
##     LETTEREPS (a)     3.300    0.196   16.855    0.000    4.766    0.515
##     PPVREPS   (b)     1.759    0.071   24.811    0.000    2.541    0.756
##     OWLSEPS   (c)     1.192    0.053   22.683    0.000    1.722    0.698
##     WJ21AEPS  (d)     5.217    0.242   21.569    0.000    7.535    0.642
##     CSLANGPS  (e)     1.719    0.107   16.020    0.000    2.483    0.523
## 
## Regressions:
##   spring_literacy ~
##     fall_literacy     1.039    0.062   16.885    0.000    0.719    0.719
##     CHMATEDNP         0.004    0.022    0.182    0.856    0.003    0.006
## 
## Covariances:
##   WJ21AEPF ~~
##     WJ21AEPS         26.345    2.917    9.032    0.000   26.345    0.424
##   PPVREPF ~~
##     PPVREPS           3.478    0.296   11.759    0.000    3.478    0.635
##   OWLSEPF ~~
##     OWLSEPS           2.010    0.166   12.136    0.000    2.010    0.595
##   LETTEREPF ~~
##     LETTEREPS        43.384    2.754   15.752    0.000   43.384    0.713
##   CSLANGPF ~~
##     CSLANGPS          6.923    0.628   11.018    0.000    6.923    0.453
##   LETTEREPF ~~
##     CSLANGPF          2.430    0.730    3.326    0.001    2.430    0.084
##   LETTEREPS ~~
##     CSLANGPS          0.797    0.827    0.963    0.336    0.797    0.025
##   fall_literacy ~~
##     CHMATEDNP         0.847    0.090    9.438    0.000    0.847    0.847
## 
## Variances:
##     LETTEREPF        58.707    3.064                     58.707    0.844
##     PPVREPF           6.187    0.374                      6.187    0.667
##     OWLSEPF           3.669    0.210                      3.669    0.721
##     WJ21AEPF         47.727    3.027                     47.727    0.637
##     CSLANGPF         14.231    0.756                     14.231    0.828
##     LETTEREPS        63.044    3.392                     63.044    0.735
##     PPVREPS           4.852    0.364                      4.852    0.429
##     OWLSEPS           3.114    0.202                      3.114    0.512
##     WJ21AEPS         80.876    4.820                     80.876    0.588
##     CSLANGPS         16.408    0.891                     16.408    0.727
##     CHMATEDNP         4.763    0.233                      4.763    1.000
##     fall_literacy     1.000                               1.000    1.000
##     spring_litrcy     1.000                               0.479    0.479
resid(fit)
## $cov
##           LETTEREPF PPVREPF OWLSEPF WJ21AEPF CSLANGPF LETTEREPS PPVREPS
## LETTEREPF 12.958                                                       
## PPVREPF    6.365     2.994                                             
## OWLSEPF    2.271     3.328   1.393                                     
## WJ21AEPF  19.031     5.459   4.456  11.339                             
## CSLANGPF  11.862     2.553   1.778   6.208    2.794                    
## LETTEREPS 12.449     6.038   2.309  13.864   11.519    2.707           
## PPVREPS    5.134     3.113   3.267   5.558    1.895    0.165     0.795 
## OWLSEPS    2.197     2.971   1.386   3.203    1.427   -1.232     1.122 
## WJ21AEPS  26.166    11.688   7.335  17.884    9.225    9.246     3.001 
## CSLANGPS  15.320     2.727   1.545   7.366    2.788    8.337    -0.717 
## CHMATEDNP  2.162     1.298   0.659   0.778    1.263    2.139     0.851 
##           OWLSEPS WJ21AEPS CSLANGPS CHMATE
## LETTEREPF                                 
## PPVREPF                                   
## OWLSEPF                                   
## WJ21AEPF                                  
## CSLANGPF                                  
## LETTEREPS                                 
## PPVREPS                                   
## OWLSEPS    0.223                          
## WJ21AEPS   0.926   7.092                  
## CSLANGPS  -0.485   3.348   -0.244         
## CHMATEDNP  0.567   2.286    0.676    0.275
## 
## $mean
## LETTEREPF   PPVREPF   OWLSEPF  WJ21AEPF  CSLANGPF LETTEREPS   PPVREPS 
##         0         0         0         0         0         0         0 
##   OWLSEPS  WJ21AEPS  CSLANGPS CHMATEDNP 
##         0         0         0         0