load libraries.

library(psych)
library(lavaan)
## This is lavaan 0.6-14
## lavaan is FREE software! Please report any bugs.
## 
## Attaching package: 'lavaan'
## The following object is masked from 'package:psych':
## 
##     cor2cov
library(semTools)
## 
## ###############################################################################
## This is semTools 0.5-6
## All users of R (or SEM) are invited to submit functions or ideas for functions.
## ###############################################################################
## 
## Attaching package: 'semTools'
## The following objects are masked from 'package:psych':
## 
##     reliability, skew
library(semPlot)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyverse)
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0     ✔ purrr   0.3.4
## ✔ tibble  3.1.8     ✔ stringr 1.4.1
## ✔ tidyr   1.2.1     ✔ forcats 0.5.2
## ✔ readr   2.1.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ ggplot2::%+%()     masks psych::%+%()
## ✖ ggplot2::alpha()   masks psych::alpha()
## ✖ readr::clipboard() masks semTools::clipboard()
## ✖ dplyr::filter()    masks stats::filter()
## ✖ dplyr::lag()       masks stats::lag()
library(haven)
library(writexl)

load data.

data <- read.csv("~/okara004@ucr.edu - Google Drive/My Drive/classes/3 -- spring '23/PSYC 214 -- stats/homework/HW #2/HW2_Data/WLS_HW2_Time.csv")

get descriptives.

# gender
gender <- factor(data$sexrsp, levels = 1:2, labels = c("Male", "Female"))
summary(gender)
##   Male Female 
##   3277   3801
3277 + 3801 ### = 7078 total participants
## [1] 7078
3801/7078 ### = 0.5370161, 53.70% female
## [1] 0.5370161
# age
psych::describe(data$age04) ### mean age in 2004: 64.35yrs; SD: 0.71yrs
##    vars    n  mean   sd median trimmed mad min max range skew kurtosis   se
## X1    1 7078 64.35 0.71     64   64.34   0  63  67     4 0.48     0.54 0.01
psych::describe(data$age11) ### mean age in 2011: 71.22yrs; SD: 0.92yrs
##    vars    n  mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 5534 71.22 0.92     71   71.14 1.48  70  74     4 0.59    -0.02 0.01
# education
data$educ92 <- as.numeric(data$educ92)
psych::describe(data$educ92) ### mean: 13.71 (about 1yr of college); SD: 2.30yrs
##    vars    n  mean  sd median trimmed mad min max range skew kurtosis   se
## X1    1 6858 13.71 2.3     12   13.32   0  12  21     9 1.05    -0.17 0.03

observed corr & cov/var matrices.

# isolate only the items of interest for the observed matrices.
time_data <- data[ , (7:14)]

# take a look at the observed correlation matrix.
corMat <- cor(time_data, use = "pairwise.complete.obs")
corMat
##             Simil_2004 Digits_2004 LFLU_2004 CFLU_2004 Simil_2011 Digits_2011
## Simil_2004   1.0000000   0.1856831 0.2387166 0.2349456  0.5568106   0.1634219
## Digits_2004  0.1856831   1.0000000 0.1902411 0.1690608  0.1501103   0.3478980
## LFLU_2004    0.2387166   0.1902411 1.0000000 0.3370050  0.2054583   0.2107888
## CFLU_2004    0.2349456   0.1690608 0.3370050 1.0000000  0.2384257   0.2326793
## Simil_2011   0.5568106   0.1501103 0.2054583 0.2384257  1.0000000   0.2095168
## Digits_2011  0.1634219   0.3478980 0.2107888 0.2326793  0.2095168   1.0000000
## LFLU_2011    0.2395870   0.1955691 0.5916502 0.3155063  0.2499542   0.2739231
## CFLU_2011    0.2180568   0.1784553 0.2969536 0.5905128  0.2429890   0.2703994
##             LFLU_2011 CFLU_2011
## Simil_2004  0.2395870 0.2180568
## Digits_2004 0.1955691 0.1784553
## LFLU_2004   0.5916502 0.2969536
## CFLU_2004   0.3155063 0.5905128
## Simil_2011  0.2499542 0.2429890
## Digits_2011 0.2739231 0.2703994
## LFLU_2011   1.0000000 0.3493626
## CFLU_2011   0.3493626 1.0000000
### the correlations within the T1 items appear to be about the same as the correlations within the T2 items, although the T2 correlations appear to be ever-so-slightly higher. correlations at T1 range from 0.17 to .34, and correlations at T2 range from 0.21 to 0.35, so a pretty similar range, which is good. each variable correlates with itself across time at about 0.50/0.60, with the exception of digits, which has a correlation of 0.35. it's good that these variables correlate highly with themselves across time, because it suggests that the items are reliable. each item at T1 correlates relatively lowly with the different items at T2 (range of 0.15 to 0.32), and this range is relatively comparable to the range we see within time, so that's another sign that the measure is pretty reliable. letter fluency & category fluency tend to correlate highly with each other compared to other pairs of different items (around 0.30 to 0.35). this makes sense because they appear to be similar measures.

# get the means & SDs.
psych::describe(corMat)
##             vars n mean   sd median trimmed  mad  min max range skew kurtosis
## Simil_2004     1 8 0.35 0.29   0.24    0.35 0.05 0.16   1  0.84 1.34     0.22
## Digits_2004    2 8 0.30 0.29   0.19    0.30 0.02 0.15   1  0.85 1.71     1.31
## LFLU_2004      3 8 0.38 0.28   0.27    0.38 0.10 0.19   1  0.81 1.24     0.00
## CFLU_2004      4 8 0.39 0.28   0.28    0.39 0.08 0.17   1  0.83 1.24     0.02
## Simil_2011     5 8 0.36 0.29   0.24    0.36 0.05 0.15   1  0.85 1.34     0.21
## Digits_2011    6 8 0.34 0.27   0.25    0.34 0.06 0.16   1  0.84 1.71     1.35
## LFLU_2011      7 8 0.40 0.27   0.29    0.40 0.08 0.20   1  0.80 1.27     0.09
## CFLU_2011      8 8 0.39 0.28   0.28    0.39 0.10 0.18   1  0.82 1.25     0.04
##              se
## Simil_2004  0.1
## Digits_2004 0.1
## LFLU_2004   0.1
## CFLU_2004   0.1
## Simil_2011  0.1
## Digits_2011 0.1
## LFLU_2011   0.1
## CFLU_2011   0.1
### will comment on the means & SDs of the var/cov matrix below, since it's more meaningful to interpret.

# turn matrix into data frame.
corMat_df <- as.data.frame(corMat)

# specify path, create file name for excel file, and save it to a data frame.
correlation_matrix <- "~/okara004@ucr.edu - Google Drive/My Drive/classes/3 -- spring '23/PSYC 214 -- stats/homework/HW #2/correlation_matrix.xlsx"

# this function will put the matrix into an excel spreadsheet.
write_xlsx(corMat_df, path = correlation_matrix)
 
# COVARIANCE MATRIX
covMat <- cov(time_data, use = "pairwise.complete.obs")
covMat
##             Simil_2004 Digits_2004 LFLU_2004 CFLU_2004 Simil_2011 Digits_2011
## Simil_2004    5.657111    1.331131  2.417782  3.346110   3.044747    1.006587
## Digits_2004   1.331131    9.417145  2.510843  3.157940   1.052011    2.762883
## LFLU_2004     2.417782    2.510843 18.940356  8.928069   2.057132    2.397620
## CFLU_2004     3.346110    3.157940  8.928069 37.754144   3.328515    3.679962
## Simil_2011    3.044747    1.052011  2.057132  3.328515   5.384844    1.271686
## Digits_2011   1.006587    2.762883  2.397620  3.679962   1.271686    6.902690
## LFLU_2011     2.327008    2.464364 10.640388  7.860272   2.399484    2.973873
## CFLU_2011     2.993917    3.200140  7.657134 21.265482   3.323114    4.173102
##             LFLU_2011 CFLU_2011
## Simil_2004   2.327008  2.993917
## Digits_2004  2.464364  3.200140
## LFLU_2004   10.640388  7.657134
## CFLU_2004    7.860272 21.265482
## Simil_2011   2.399484  3.323114
## Digits_2011  2.973873  4.173102
## LFLU_2011   17.488017  8.685133
## CFLU_2011    8.685133 35.660451
# get the means & SDs.
psych::describe(covMat)
##             vars n  mean    sd median trimmed  mad  min   max range skew
## Simil_2004     1 8  2.77  1.43   2.71    2.77 0.76 1.01  5.66  4.65 0.68
## Digits_2004    2 8  3.24  2.62   2.64    3.24 0.80 1.05  9.42  8.37 1.52
## LFLU_2004      3 8  6.94  5.94   5.08    6.94 4.24 2.06 18.94 16.88 0.83
## CFLU_2004      4 8 11.17 12.35   5.77   11.17 3.75 3.16 37.75 34.60 1.19
## Simil_2011     5 8  2.73  1.38   2.72    2.73 0.94 1.05  5.38  4.33 0.52
## Digits_2011    6 8  3.15  1.86   2.87    3.15 1.57 1.01  6.90  5.90 0.72
## LFLU_2011      7 8  6.85  5.43   5.42    6.85 4.53 2.33 17.49 15.16 0.73
## CFLU_2011      8 8 10.87 11.71   5.92   10.87 4.07 2.99 35.66 32.67 1.14
##             kurtosis   se
## Simil_2004     -0.48 0.51
## Digits_2004     0.98 0.93
## LFLU_2004      -0.71 2.10
## CFLU_2004      -0.18 4.37
## Simil_2011     -0.86 0.49
## Digits_2011    -0.57 0.66
## LFLU_2011      -0.93 1.92
## CFLU_2011      -0.33 4.14
### means at T1 appear to be a bit larger than the means at T2, which makes sense with our hypothesis that cognitive ability is declining. variance at T1 is only slightly larger than variance at T2, meaning that there is slightly less variability at T2. but the variances really aren't all that different from each other.

# export the matrix to an excel file.
covMat_df <- as.data.frame(covMat)
covariance_matrix <- "~/okara004@ucr.edu - Google Drive/My Drive/classes/3 -- spring '23/PSYC 214 -- stats/homework/HW #2/covariance_matrix.xlsx"
write_xlsx(covMat_df, path = covariance_matrix)

configural invariance.

equate one set of lambdas per latent variable and one set of intercepts per latent.

model.CI <- '
#Latent variables
CogT1 =~ NA*Simil_2004 + a*Simil_2004 + Digits_2004 + LFLU_2004 + CFLU_2004
CogT2 =~ NA*Simil_2011 + a*Simil_2011 + Digits_2011 + LFLU_2011 + CFLU_2011

# Residuals
Simil_2004 ~~ Simil_2004
Simil_2011 ~~ Simil_2011
Digits_2004 ~~ Digits_2004
Digits_2011 ~~ Digits_2011
LFLU_2004 ~~ LFLU_2004
LFLU_2011 ~~ LFLU_2011
CFLU_2004 ~~ CFLU_2004
CFLU_2011 ~~ CFLU_2011

# Residual covariances
Simil_2004 ~~ Simil_2011
Digits_2004 ~~ Digits_2011
LFLU_2004 ~~ LFLU_2011
CFLU_2004 ~~ CFLU_2011

# Intercepts
Simil_2004 ~ nu1*1
Simil_2011 ~ nu1*1
Digits_2004 ~ nu11*1
Digits_2011 ~ nu12*1
LFLU_2004 ~ nu21*1
LFLU_2011 ~ nu22*1
CFLU_2004 ~ nu31*1
CFLU_2011 ~ nu32*1

#Latent variances/covariances
#Standardized CogT1
CogT1 ~~ 1*CogT1
#covariance
CogT2 ~~ cov*CogT1
#variance Time 2
CogT2 ~~ NA*CogT2

#Latent means
CogT1 ~ a1*1
CogT2 ~ a2*1
a1 == 0
'

# run the model and see how it fits.
model.CI.fit <- sem(model.CI, data = data, missing = "FIML")
summary(model.CI.fit, fit.measures = TRUE, standardized = T)
## lavaan 0.6.14 ended normally after 107 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        32
##   Number of equality constraints                     3
## 
##   Number of observations                          7078
##   Number of missing patterns                        75
## 
## Model Test User Model:
##                                                       
##   Test statistic                                28.999
##   Degrees of freedom                                15
##   P-value (Chi-square)                           0.016
## 
## Model Test Baseline Model:
## 
##   Test statistic                              7086.446
##   Degrees of freedom                                28
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.998
##   Tucker-Lewis Index (TLI)                       0.996
##                                                       
##   Robust Comparative Fit Index (CFI)             0.998
##   Robust Tucker-Lewis Index (TLI)                0.996
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)             -95272.349
##   Loglikelihood unrestricted model (H1)     -95257.849
##                                                       
##   Akaike (AIC)                              190602.698
##   Bayesian (BIC)                            190801.775
##   Sample-size adjusted Bayesian (SABIC)     190709.620
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.011
##   90 Percent confidence interval - lower         0.005
##   90 Percent confidence interval - upper         0.018
##   P-value H_0: RMSEA <= 0.050                    1.000
##   P-value H_0: RMSEA >= 0.080                    0.000
##                                                       
##   Robust RMSEA                                   0.016
##   90 Percent confidence interval - lower         0.004
##   90 Percent confidence interval - upper         0.026
##   P-value H_0: Robust RMSEA <= 0.050             1.000
##   P-value H_0: Robust RMSEA >= 0.080             0.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.010
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Observed
##   Observed information based on                Hessian
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   CogT1 =~                                                              
##     Simil_2004 (a)    1.059    0.041   25.767    0.000    1.059    0.445
##     Digts_2004        1.105    0.057   19.421    0.000    1.105    0.360
##     LFLU_2004         2.491    0.086   28.845    0.000    2.491    0.570
##     CFLU_2004         3.496    0.138   25.380    0.000    3.496    0.568
##   CogT2 =~                                                              
##     Simil_2011 (a)    1.059    0.041   25.767    0.000    1.021    0.439
##     Digts_2011        1.252    0.073   17.163    0.000    1.207    0.458
##     LFLU_2011         2.700    0.142   19.020    0.000    2.604    0.620
##     CFLU_2011         3.600    0.205   17.582    0.000    3.472    0.578
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .Simil_2004 ~~                                                         
##    .Sml_2011          2.117    0.082   25.680    0.000    2.117    0.475
##  .Digits_2004 ~~                                                        
##    .Dgt_2011          1.658    0.125   13.276    0.000    1.658    0.247
##  .LFLU_2004 ~~                                                          
##    .LFLU_201          5.098    0.328   15.523    0.000    5.098    0.431
##  .CFLU_2004 ~~                                                          
##    .CFLU_201         11.024    0.750   14.698    0.000   11.024    0.444
##   CogT1 ~~                                                              
##     CogT2    (cov)    0.883    0.039   22.402    0.000    0.915    0.915
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Sm_2004  (nu1)    6.577    0.029  229.089    0.000    6.577    2.763
##    .Sm_2011  (nu1)    6.577    0.029  229.089    0.000    6.577    2.827
##    .Dg_2004 (nu11)    7.197    0.042  171.902    0.000    7.197    2.344
##    .Dg_2011 (nu12)    7.086    0.055  129.049    0.000    7.086    2.690
##    .LFLU_20 (nu21)   11.327    0.059  190.461    0.000   11.327    2.592
##    .LFLU_20 (nu22)   11.782    0.100  117.506    0.000   11.782    2.804
##    .CFLU_20 (nu31)   20.782    0.103  201.946    0.000   20.782    3.376
##    .CFLU_20 (nu32)   20.306    0.155  130.890    0.000   20.306    3.381
##     CogT1     (a1)    0.000                               0.000    0.000
##     CogT2     (a2)   -0.281    0.030   -9.391    0.000   -0.292   -0.292
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Simil_2004        4.543    0.102   44.745    0.000    4.543    0.802
##    .Simil_2011        4.371    0.103   42.293    0.000    4.371    0.807
##    .Digits_2004       8.207    0.181   45.225    0.000    8.207    0.871
##    .Digits_2011       5.483    0.145   37.705    0.000    5.483    0.790
##    .LFLU_2004        12.890    0.414   31.121    0.000   12.890    0.675
##    .LFLU_2011        10.878    0.397   27.434    0.000   10.878    0.616
##    .CFLU_2004        25.668    0.922   27.830    0.000   25.668    0.677
##    .CFLU_2011        24.018    0.937   25.621    0.000   24.018    0.666
##     CogT1             1.000                               1.000    1.000
##     CogT2             0.930    0.075   12.335    0.000    1.000    1.000
## 
## Constraints:
##                                                |Slack|
##     a1 - 0                                       0.000
### the p-value for the chi-squared is significant, but we have a pretty big sample size so we don't need to worry about that too much. the CFI, TLI, and RMSEA all meet the appropriate thresholds, indicating the model fits the data pretty well.  

# visualization of the model.
semPaths(model.CI.fit, what = "est")

weak invariance.

equate all sets of lambdas and one set of intercepts per latent.

model.WI <- '
#Latent variables
CogT1 =~ NA*Simil_2004 + a*Simil_2004 + b*Digits_2004 + c*LFLU_2004 + d*CFLU_2004
CogT2 =~ NA*Simil_2011 + a*Simil_2011 + b*Digits_2011 + c*LFLU_2011 + d*CFLU_2011

# Residuals
Simil_2004 ~~ Simil_2004
Simil_2011 ~~ Simil_2011
Digits_2004 ~~ Digits_2004
Digits_2011 ~~ Digits_2011
LFLU_2004 ~~ LFLU_2004
LFLU_2011 ~~ LFLU_2011
CFLU_2004 ~~ CFLU_2004
CFLU_2011 ~~ CFLU_2011

# Residual covariances
Simil_2004 ~~ Simil_2011
Digits_2004 ~~ Digits_2011
LFLU_2004 ~~ LFLU_2011
CFLU_2004 ~~ CFLU_2011

# Intercepts
Simil_2004 ~ nu1*1
Simil_2011 ~ nu1*1
Digits_2004 ~ nu11*1
Digits_2011 ~ nu12*1
LFLU_2004 ~ nu21*1
LFLU_2011 ~ nu22*1
CFLU_2004 ~ nu31*1
CFLU_2011 ~ nu32*1

#Latent variances/covariances
#Standardized CogT1
CogT1 ~~ 1*CogT1
#covariance
CogT2 ~~ cov*CogT1
#variance Time 2
CogT2 ~~ NA*CogT2

#Latent means
CogT1 ~ a1*1
CogT2 ~ a2*1
a1 == 0
'

# run the model and see how it fits.
model.WI.fit <- sem(model.WI, data = data, missing = "FIML")
summary(model.WI.fit, fit.measures = TRUE, standardized = T)
## lavaan 0.6.14 ended normally after 92 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        32
##   Number of equality constraints                     6
## 
##   Number of observations                          7078
##   Number of missing patterns                        75
## 
## Model Test User Model:
##                                                       
##   Test statistic                                33.392
##   Degrees of freedom                                18
##   P-value (Chi-square)                           0.015
## 
## Model Test Baseline Model:
## 
##   Test statistic                              7086.446
##   Degrees of freedom                                28
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.998
##   Tucker-Lewis Index (TLI)                       0.997
##                                                       
##   Robust Comparative Fit Index (CFI)             0.998
##   Robust Tucker-Lewis Index (TLI)                0.997
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)             -95274.545
##   Loglikelihood unrestricted model (H1)     -95257.849
##                                                       
##   Akaike (AIC)                              190601.091
##   Bayesian (BIC)                            190779.574
##   Sample-size adjusted Bayesian (SABIC)     190696.952
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.011
##   90 Percent confidence interval - lower         0.005
##   90 Percent confidence interval - upper         0.017
##   P-value H_0: RMSEA <= 0.050                    1.000
##   P-value H_0: RMSEA >= 0.080                    0.000
##                                                       
##   Robust RMSEA                                   0.015
##   90 Percent confidence interval - lower         0.003
##   90 Percent confidence interval - upper         0.024
##   P-value H_0: Robust RMSEA <= 0.050             1.000
##   P-value H_0: Robust RMSEA >= 0.080             0.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.012
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Observed
##   Observed information based on                Hessian
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   CogT1 =~                                                              
##     Simil_2004 (a)    1.034    0.038   27.566    0.000    1.034    0.435
##     Digts_2004 (b)    1.153    0.044   26.035    0.000    1.153    0.374
##     LFLU_2004  (c)    2.526    0.077   32.628    0.000    2.526    0.577
##     CFLU_2004  (d)    3.457    0.123   28.184    0.000    3.457    0.563
##   CogT2 =~                                                              
##     Simil_2011 (a)    1.034    0.038   27.566    0.000    1.051    0.450
##     Digts_2011 (b)    1.153    0.044   26.035    0.000    1.172    0.446
##     LFLU_2011  (c)    2.526    0.077   32.628    0.000    2.567    0.612
##     CFLU_2011  (d)    3.457    0.123   28.184    0.000    3.513    0.584
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .Simil_2004 ~~                                                         
##    .Sml_2011          2.113    0.083   25.584    0.000    2.113    0.474
##  .Digits_2004 ~~                                                        
##    .Dgt_2011          1.649    0.125   13.223    0.000    1.649    0.246
##  .LFLU_2004 ~~                                                          
##    .LFLU_201          5.101    0.329   15.525    0.000    5.101    0.430
##  .CFLU_2004 ~~                                                          
##    .CFLU_201         11.011    0.750   14.682    0.000   11.011    0.443
##   CogT1 ~~                                                              
##     CogT2    (cov)    0.932    0.025   37.366    0.000    0.917    0.917
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Sm_2004  (nu1)    6.577    0.029  229.588    0.000    6.577    2.770
##    .Sm_2011  (nu1)    6.577    0.029  229.588    0.000    6.577    2.819
##    .Dg_2004 (nu11)    7.196    0.042  171.458    0.000    7.196    2.336
##    .Dg_2011 (nu12)    7.070    0.053  134.534    0.000    7.070    2.693
##    .LFLU_20 (nu21)   11.327    0.060  190.161    0.000   11.327    2.586
##    .LFLU_20 (nu22)   11.756    0.095  123.203    0.000   11.756    2.803
##    .CFLU_20 (nu31)   20.786    0.103  202.173    0.000   20.786    3.383
##    .CFLU_20 (nu32)   20.292    0.152  133.925    0.000   20.292    3.371
##     CogT1     (a1)    0.000                               0.000    0.000
##     CogT2     (a2)   -0.289    0.030   -9.544    0.000   -0.285   -0.285
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Simil_2004        4.571    0.099   46.015    0.000    4.571    0.810
##    .Simil_2011        4.340    0.102   42.628    0.000    4.340    0.797
##    .Digits_2004       8.158    0.177   45.969    0.000    8.158    0.860
##    .Digits_2011       5.520    0.143   38.626    0.000    5.520    0.801
##    .LFLU_2004        12.796    0.402   31.810    0.000   12.796    0.667
##    .LFLU_2011        11.001    0.380   28.968    0.000   11.001    0.625
##    .CFLU_2004        25.811    0.891   28.954    0.000   25.811    0.684
##    .CFLU_2011        23.884    0.916   26.060    0.000   23.884    0.659
##     CogT1             1.000                               1.000    1.000
##     CogT2             1.033    0.043   24.023    0.000    1.000    1.000
## 
## Constraints:
##                                                |Slack|
##     a1 - 0                                       0.000
### p-value for chi-square is still significant, but again, we don't have to worry too much about this because of the large sample size. we gain 3 degrees of freedom going from CI to WI, but our model does not fit significantly worse. AIC and BIC are just slightly smaller for WI than CI. CFI & RMSEA are the same between models, and TLI is ever-so-slightly larger for WI than CI. overall, WI is not all that different from CI in terms of model fit. 

# visualization of the model.
semPaths(model.WI.fit, what = "est")

strong invariance.

equate all sets of lambdas and all sets of intercepts.

model.StrongI <- '
#Latent variables
CogT1 =~ NA*Simil_2004 + a*Simil_2004 + b*Digits_2004 + c*LFLU_2004 + d*CFLU_2004
CogT2 =~ NA*Simil_2011 + a*Simil_2011 + b*Digits_2011 + c*LFLU_2011 + d*CFLU_2011

# Residuals
Simil_2004 ~~ Simil_2004
Simil_2011 ~~ Simil_2011
Digits_2004 ~~ Digits_2004
Digits_2011 ~~ Digits_2011
LFLU_2004 ~~ LFLU_2004
LFLU_2011 ~~ LFLU_2011
CFLU_2004 ~~ CFLU_2004
CFLU_2011 ~~ CFLU_2011

# Residual covariances
Simil_2004 ~~ Simil_2011
Digits_2004 ~~ Digits_2011
LFLU_2004 ~~ LFLU_2011
CFLU_2004 ~~ CFLU_2011

# Intercepts
Simil_2004 ~ nu1*1
Simil_2011 ~ nu1*1
Digits_2004 ~ nu11*1
Digits_2011 ~ nu11*1
LFLU_2004 ~ nu21*1
LFLU_2011 ~ nu21*1
CFLU_2004 ~ nu31*1
CFLU_2011 ~ nu31*1

#Latent variances/covariances
#Standardized CogT1
CogT1 ~~ 1*CogT1
#covariance
CogT2 ~~ cov*CogT1
#variance Time 2
CogT2 ~~ NA*CogT2

#Latent means
CogT1 ~ a1*1
CogT2 ~ a2*1
a1 == 0
'

# run the model and see how it fits.
model.StrongI.fit <- sem(model.StrongI, data = data, missing = "FIML")
summary(model.StrongI.fit, fit.measures = TRUE, standardized = T)
## lavaan 0.6.14 ended normally after 87 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        32
##   Number of equality constraints                     9
## 
##   Number of observations                          7078
##   Number of missing patterns                        75
## 
## Model Test User Model:
##                                                       
##   Test statistic                               109.972
##   Degrees of freedom                                21
##   P-value (Chi-square)                           0.000
## 
## Model Test Baseline Model:
## 
##   Test statistic                              7086.446
##   Degrees of freedom                                28
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.987
##   Tucker-Lewis Index (TLI)                       0.983
##                                                       
##   Robust Comparative Fit Index (CFI)             0.987
##   Robust Tucker-Lewis Index (TLI)                0.983
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)             -95312.835
##   Loglikelihood unrestricted model (H1)     -95257.849
##                                                       
##   Akaike (AIC)                              190671.670
##   Bayesian (BIC)                            190829.560
##   Sample-size adjusted Bayesian (SABIC)     190756.471
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.024
##   90 Percent confidence interval - lower         0.020
##   90 Percent confidence interval - upper         0.029
##   P-value H_0: RMSEA <= 0.050                    1.000
##   P-value H_0: RMSEA >= 0.080                    0.000
##                                                       
##   Robust RMSEA                                   0.034
##   90 Percent confidence interval - lower         0.028
##   90 Percent confidence interval - upper         0.041
##   P-value H_0: Robust RMSEA <= 0.050             1.000
##   P-value H_0: Robust RMSEA >= 0.080             0.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.021
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Observed
##   Observed information based on                Hessian
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   CogT1 =~                                                              
##     Simil_2004 (a)    1.046    0.036   28.741    0.000    1.046    0.440
##     Digts_2004 (b)    1.182    0.044   27.108    0.000    1.182    0.383
##     LFLU_2004  (c)    2.361    0.074   31.954    0.000    2.361    0.542
##     CFLU_2004  (d)    3.624    0.120   30.146    0.000    3.624    0.586
##   CogT2 =~                                                              
##     Simil_2011 (a)    1.046    0.036   28.741    0.000    1.069    0.458
##     Digts_2011 (b)    1.182    0.044   27.108    0.000    1.208    0.459
##     LFLU_2011  (c)    2.361    0.074   31.954    0.000    2.412    0.578
##     CFLU_2011  (d)    3.624    0.120   30.146    0.000    3.704    0.610
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .Simil_2004 ~~                                                         
##    .Sml_2011          2.084    0.082   25.493    0.000    2.084    0.471
##  .Digits_2004 ~~                                                        
##    .Dgt_2011          1.599    0.124   12.852    0.000    1.599    0.240
##  .LFLU_2004 ~~                                                          
##    .LFLU_201          5.563    0.314   17.693    0.000    5.563    0.446
##  .CFLU_2004 ~~                                                          
##    .CFLU_201         10.176    0.746   13.633    0.000   10.176    0.422
##   CogT1 ~~                                                              
##     CogT2    (cov)    0.941    0.025   37.130    0.000    0.920    0.920
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Sm_2004  (nu1)    6.572    0.027  245.557    0.000    6.572    2.766
##    .Sm_2011  (nu1)    6.572    0.027  245.557    0.000    6.572    2.816
##    .Dg_2004 (nu11)    7.124    0.035  205.432    0.000    7.124    2.309
##    .Dg_2011 (nu11)    7.124    0.035  205.432    0.000    7.124    2.706
##    .LFLU_20 (nu21)   11.528    0.054  214.558    0.000   11.528    2.648
##    .LFLU_20 (nu21)   11.528    0.054  214.558    0.000   11.528    2.761
##    .CFLU_20 (nu31)   20.567    0.096  213.699    0.000   20.567    3.323
##    .CFLU_20 (nu31)   20.567    0.096  213.699    0.000   20.567    3.389
##     CogT1     (a1)    0.000                               0.000    0.000
##     CogT2     (a2)   -0.275    0.017  -16.088    0.000   -0.269   -0.269
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Simil_2004        4.550    0.099   45.980    0.000    4.550    0.806
##    .Simil_2011        4.304    0.101   42.642    0.000    4.304    0.790
##    .Digits_2004       8.126    0.178   45.630    0.000    8.126    0.853
##    .Digits_2011       5.472    0.143   38.216    0.000    5.472    0.790
##    .LFLU_2004        13.387    0.383   34.915    0.000   13.387    0.706
##    .LFLU_2011        11.608    0.361   32.174    0.000   11.608    0.666
##    .CFLU_2004        25.170    0.903   27.881    0.000   25.170    0.657
##    .CFLU_2011        23.105    0.923   25.044    0.000   23.105    0.627
##     CogT1             1.000                               1.000    1.000
##     CogT2             1.045    0.044   23.829    0.000    1.000    1.000
## 
## Constraints:
##                                                |Slack|
##     a1 - 0                                       0.000
### p-value for chi-square is once again significant, and once again we don't need to worry about it too much because of the sample size. we've gained 3 degrees of freedom going from WI to strong invariance, but our model fits significantly worse. AIC and BIC are a bit worse for strong invariance compared to WI, but it's not too big of a difference. CFI and TLI are a bit smaller for strong invariance compared to WI, but they still easily meet the 0.95 threshold, so still a good fit. RMSEA is a bit larger for strong invariance compared to WI, but it still easily meets the 0.05 threshold as well. overall, a little bit worse than WI, but still a good fit!

# visualization of the model.
semPaths(model.StrongI.fit, what = "est")

strict invariance.

equate all sets of lambdas, all sets of intercepts, & all sets of residuals.

model.StrictI <- '
#Latent variables
CogT1 =~ NA*Simil_2004 + a*Simil_2004 + b*Digits_2004 + c*LFLU_2004 + d*CFLU_2004
CogT2 =~ NA*Simil_2011 + a*Simil_2011 + b*Digits_2011 + c*LFLU_2011 + d*CFLU_2011

# Residuals
Simil_2004 ~~ e*Simil_2004
Simil_2011 ~~ e*Simil_2011
Digits_2004 ~~ f*Digits_2004
Digits_2011 ~~ f*Digits_2011
LFLU_2004 ~~ g*LFLU_2004
LFLU_2011 ~~ g*LFLU_2011
CFLU_2004 ~~ h*CFLU_2004
CFLU_2011 ~~ h*CFLU_2011

# Residual covariances
Simil_2004 ~~ Simil_2011
Digits_2004 ~~ Digits_2011
LFLU_2004 ~~ LFLU_2011
CFLU_2004 ~~ CFLU_2011

# Intercepts
Simil_2004 ~ nu1*1
Simil_2011 ~ nu1*1
Digits_2004 ~ nu11*1
Digits_2011 ~ nu11*1
LFLU_2004 ~ nu21*1
LFLU_2011 ~ nu21*1
CFLU_2004 ~ nu31*1
CFLU_2011 ~ nu31*1

#Latent variances/covariances
#Standardized CogT1
CogT1 ~~ 1*CogT1
#covariance
CogT2 ~~ cov*CogT1
#variance Time 2
CogT2 ~~ NA*CogT2

#Latent means
CogT1 ~ a1*1
CogT2 ~ a2*1
a1 == 0
'

# run the model and see how it fits.
model.StrictI.fit <- sem(model.StrictI, data = data, missing = "FIML")
summary(model.StrictI.fit, fit.measures = TRUE, standardized = T)
## lavaan 0.6.14 ended normally after 75 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        32
##   Number of equality constraints                    13
## 
##   Number of observations                          7078
##   Number of missing patterns                        75
## 
## Model Test User Model:
##                                                       
##   Test statistic                               291.616
##   Degrees of freedom                                25
##   P-value (Chi-square)                           0.000
## 
## Model Test Baseline Model:
## 
##   Test statistic                              7086.446
##   Degrees of freedom                                28
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.962
##   Tucker-Lewis Index (TLI)                       0.958
##                                                       
##   Robust Comparative Fit Index (CFI)             0.965
##   Robust Tucker-Lewis Index (TLI)                0.961
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)             -95403.657
##   Loglikelihood unrestricted model (H1)     -95257.849
##                                                       
##   Akaike (AIC)                              190845.314
##   Bayesian (BIC)                            190975.744
##   Sample-size adjusted Bayesian (SABIC)     190915.367
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.039
##   90 Percent confidence interval - lower         0.035
##   90 Percent confidence interval - upper         0.043
##   P-value H_0: RMSEA <= 0.050                    1.000
##   P-value H_0: RMSEA >= 0.080                    0.000
##                                                       
##   Robust RMSEA                                   0.052
##   90 Percent confidence interval - lower         0.046
##   90 Percent confidence interval - upper         0.058
##   P-value H_0: Robust RMSEA <= 0.050             0.280
##   P-value H_0: Robust RMSEA >= 0.080             0.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.041
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Observed
##   Observed information based on                Hessian
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   CogT1 =~                                                              
##     Simil_2004 (a)    1.091    0.037   29.821    0.000    1.091    0.461
##     Digts_2004 (b)    1.208    0.046   26.532    0.000    1.208    0.416
##     LFLU_2004  (c)    2.425    0.075   32.527    0.000    2.425    0.565
##     CFLU_2004  (d)    3.703    0.121   30.602    0.000    3.703    0.599
##   CogT2 =~                                                              
##     Simil_2011 (a)    1.091    0.037   29.821    0.000    1.046    0.446
##     Digts_2011 (b)    1.208    0.046   26.532    0.000    1.158    0.402
##     LFLU_2011  (c)    2.425    0.075   32.527    0.000    2.324    0.549
##     CFLU_2011  (d)    3.703    0.121   30.602    0.000    3.549    0.583
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .Simil_2004 ~~                                                         
##    .Sml_2011          2.069    0.083   25.044    0.000    2.069    0.469
##  .Digits_2004 ~~                                                        
##    .Dgt_2011          1.702    0.131   12.992    0.000    1.702    0.244
##  .LFLU_2004 ~~                                                          
##    .LFLU_201          5.569    0.318   17.540    0.000    5.569    0.444
##  .CFLU_2004 ~~                                                          
##    .CFLU_201         10.412    0.754   13.816    0.000   10.412    0.426
##   CogT1 ~~                                                              
##     CogT2    (cov)    0.885    0.022   40.260    0.000    0.923    0.923
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Sm_2004  (nu1)    6.572    0.027  244.621    0.000    6.572    2.776
##    .Sm_2011  (nu1)    6.572    0.027  244.621    0.000    6.572    2.801
##    .Dg_2004 (nu11)    7.136    0.035  206.075    0.000    7.136    2.459
##    .Dg_2011 (nu11)    7.136    0.035  206.075    0.000    7.136    2.476
##    .LFLU_20 (nu21)   11.504    0.054  214.995    0.000   11.504    2.681
##    .LFLU_20 (nu21)   11.504    0.054  214.995    0.000   11.504    2.716
##    .CFLU_20 (nu31)   20.573    0.096  213.681    0.000   20.573    3.331
##    .CFLU_20 (nu31)   20.573    0.096  213.681    0.000   20.573    3.380
##     CogT1     (a1)    0.000       NA                      0.000    0.000
##     CogT2     (a2)   -0.264    0.016  -16.084    0.000   -0.276   -0.276
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Simil_2004 (e)    4.414    0.083   53.253    0.000    4.414    0.788
##    .Simil_2011 (e)    4.414    0.083   53.253    0.000    4.414    0.801
##    .Digts_2004 (f)    6.966    0.126   55.373    0.000    6.966    0.827
##    .Digts_2011 (f)    6.966    0.126   55.373    0.000    6.966    0.839
##    .LFLU_2004  (g)   12.534    0.314   39.936    0.000   12.534    0.681
##    .LFLU_2011  (g)   12.534    0.314   39.936    0.000   12.534    0.699
##    .CFLU_2004  (h)   24.448    0.762   32.101    0.000   24.448    0.641
##    .CFLU_2011  (h)   24.448    0.762   32.101    0.000   24.448    0.660
##     CogT1             1.000                               1.000    1.000
##     CogT2             0.919    0.036   25.423    0.000    1.000    1.000
## 
## Constraints:
##                                                |Slack|
##     a1 - 0                                       0.000
### p-value for chi-square is once again significant, and once again we don't need to worry about it too much because of the sample size. we've gained 4 degrees of freedom this time going from strong invariance to strict invariance, but our model fits significantly worse. AIC and BIC are a bit worse for strict compared to strong, but again not too big of a difference. CFI and TLI are smaller for strict compared to strong, but they still meet the 0.95 threshold, so still a good fit. RMSEA is larger for strict compared to strong, but it still meets the 0.05 threshold as well. overall, a little bit worse than strong, but still a good fit! this means that we can confidently say we've established measurement invariance. thus, the T2 mean of -0.264 indicates that on average, cognitive ability declined by -0.264 units from T1 to T2, with a variance of 0.919 units. that's a pretty small change, though.

# visualization of the model.
semPaths(model.StrictI.fit, what = "est")