Wednesday, October 18, 2017

latent change score model in R


For those interested... I made R implement LCS, it's actually pretty flexible... enjoy!

#as with Mplus code, I suggest you rename your T1 and T2 variables obsY1 and obsY2 first
install.packages("lavaan") #you need to install lavaan first
library(lavaan) #you need to call in lavaan first
#in lavaan the model and data are 2 entities, I like this, they become connected after defining the model

#first the model
LCS2waves <- ' LatY1 =~ 1*obsY1 #define latents behind the observed
obsY1~~ measerr*obsY1 # same measurement error measerr across time
obsY1~0*1 #intercept of 1-indicator@0 to identify the mean of the latent; if free obsY1~1
LatY2 =~ 1*obsY2; #define latents behind the observed
obsY2~~ measerr*obsY2 #same measurement error measerr across time
obsY2~0*1  #intercept of 1-indicator@0 to identify the mean of the latent; if free obsY1~1
#LCS part
LCS21 =~ 1*obsY2 #define LCS on the second variable in subtraction
LatY2 =~ 1*LatY1; # autoregression AR1 @1
LatY2~~0*LatY2 #all LatY2 variance is explained, no error left
LatY1~1 # LatY1 mean of first estimated
LatY2~0*1 # LatY2 intercept =0 so LCS21 mean can be identified
#LCS21~~0*LCS21 #no need to set it to 0 per se, but this commonly becomes <0
LCS21~1 # LCS21 mean or intercept estimated
#this is the mean of change, IF nothing points to it, if anything points to it, need to center them to interpret this intercept
LCS21~LatY1 #proportional growth, this is not needed, if you take it out you will have a covariance between them estimated
'


#now one can fit the model above to some (any) data
fit <- sem(model = LCS2waves, data = YOURDATA)

# show results
summary(fit, fit.measures=TRUE)

#mine showed something like:

lavaan (0.5-23.1097) converged normally after  46 iterations

  Number of observations                            61

  Estimator                                         ML
  Minimum Function Test Statistic                   NA
  Degrees of freedom                                -1
  Minimum Function Value               0.0000000000000

User model versus baseline model:

  Comparative Fit Index (CFI)                       NA
  Tucker-Lewis Index (TLI)                          NA

Loglikelihood and Information Criteria:

  Loglikelihood user model (H0)               -409.528
  Loglikelihood unrestricted model (H1)       -409.528

  Number of free parameters                          6
  Akaike (AIC)                                 831.056
  Bayesian (BIC)                               843.721
  Sample-size adjusted Bayesian (BIC)          824.846

Root Mean Square Error of Approximation:

  RMSEA                                             NA
  90 Percent Confidence Interval             NA     NA
  P-value RMSEA <= 0.05                             NA

Standardized Root Mean Square Residual:

  SRMR                                           0.000

Parameter Estimates:

  Information                                 Expected
  Standard Errors                             Standard

Latent Variables:
                   Estimate  Std.Err  z-value  P(>|z|)
  LatY1 =~                                            
    obsY1             1.000                           
  LatY2 =~                                            
    obsY2             1.000                           
  LCS21 =~                                            
    obsY2             1.000                           
  LatY2 =~                                            
    LatY1             1.000                           

Regressions:
                   Estimate  Std.Err  z-value  P(>|z|)
  LCS21 ~                                             
    LatY1             0.568    0.423    1.341    0.180

Intercepts:
                   Estimate  Std.Err  z-value  P(>|z|)
   .obsY1             0.000                           
   .obsY2             0.000                           
    LatY1             9.131    0.912   10.009    0.000
    LatY2             0.000                           
   .LCS21             3.569    3.742    0.954    0.340

Variances:
                   Estimate  Std.Err  z-value  P(>|z|)
   .obsY1   (msrr)   51.146   14.017    3.649    0.000
   .obsY2   (msrr)   51.146   14.017    3.649    0.000
    LatY2             0.000                           
    LatY1            -0.376   10.659   -0.035    0.972
   .LCS21            -5.233   14.091   -0.371    0.710
 
~~~~~~~~~~~~~~~~~~~~~~~~
This paper introduces LCS amazingly simply and intuitively, a graphical depiction below; when it's going to be published maybe the R code will accompany it. 
 
Berggren, R., Nilsson, J., Brehmer, Y., Schmiedek, F., & Lövdén, M. (2018). No evidence that foreign language learning in older age improve cognitive ability A RCT.