# Examples of Latent Growth Curve Models

## Load nlpsem package, dependent packages and set CSOLNP as the optimizer

    library(nlpsem)
    mxOption(model = NULL, key = "Default optimizer", "CSOLNP", reset = FALSE)

## Load pre-computed models

    load(system.file("extdata", "getLGCM_examples.RData", package = "nlpsem"))

## Load example data and preprocess data

    # Load ECLS-K (2011) data
    data("RMS_dat")
    RMS_dat0 <- RMS_dat
    # Re-baseline the data so that the estimated initial status is for the
    # starting point of the study
    baseT <- RMS_dat0$T1
    RMS_dat0$T1 <- RMS_dat0$T1 - baseT
    RMS_dat0$T2 <- RMS_dat0$T2 - baseT
    RMS_dat0$T3 <- RMS_dat0$T3 - baseT
    RMS_dat0$T4 <- RMS_dat0$T4 - baseT
    RMS_dat0$T5 <- RMS_dat0$T5 - baseT
    RMS_dat0$T6 <- RMS_dat0$T6 - baseT
    RMS_dat0$T7 <- RMS_dat0$T7 - baseT
    RMS_dat0$T8 <- RMS_dat0$T8 - baseT
    RMS_dat0$T9 <- RMS_dat0$T9 - baseT
    # Standardize time-invariant covariates (TICs)
    ## ex1 and ex2 are standardized growth TICs in models
    RMS_dat0$ex1 <- scale(RMS_dat0$Approach_to_Learning)
    RMS_dat0$ex2 <- scale(RMS_dat0$Attention_focus)
    xstarts <- mean(baseT)

## Example 1: Fit bilinear spline LGCMs with both a random and a fixed knot to evaluate the development of mathematics ability from Kindergarten to Grade 5. These models are then compared using a likelihood ratio test (LRT) utilizing the getLRT() function.

    Math_LGCM_BLS_f <- getLGCM(
      dat = RMS_dat0, t_var = "T", y_var = "M", curveFun = "bilinear spline",
      intrinsic = TRUE, records = 1:9, growth_TIC = NULL, tries = 10
    )
    Math_LGCM_BLS_r <- getLGCM(
      dat = RMS_dat0, t_var = "T", y_var = "M", curveFun = "bilinear spline",
      intrinsic = FALSE, records = 1:9, growth_TIC = NULL, tries = 10
    )

    getLRT(
      full = Math_LGCM_BLS_f@mxOutput, reduced = Math_LGCM_BLS_r@mxOutput, boot = FALSE, rep = NA
      )
    #>               # of Free Param -2loglik Degree of Freedom Diff in loglik
    #> Full Model                 15 31317.97              4485             NA
    #> Reduced Model              11 31347.39              4489       29.42711
    #>               Diff in DoF p.values      AIC      BIC
    #> Full Model             NA     <NA> 31347.97 31411.18
    #> Reduced Model           4  <0.0001 31369.39 31415.75

The output illustrates a difference of \(4\) degrees of freedom between the full model (bilinear spline LGCM with a random knot) and the reduced model (bilinear spline LGCM with a fixed knot). This difference arises from the knot variance and its covariance with the intercept and the two slopes. A Likelihood Ratio Test (LRT) returns a p-value of less than \(0.0001\), which is indicative of the test favoring the full model. Additionally, both the Akaike Information Criterion (AIC) and the Bayesian Information Criterion (BIC) corroborate this preference, suggesting the superiority of the full model over the reduced one.

## Example 2: Fit the full bilinear spline LGCM to assess the development of mathematics skill from Kindergarten to Grade 5. This model includes two growth time-invariant covariates (TICs), baseline values of teacher-reported approach to learning and attentional focus. Point estimates and corresponding standard errors (SEs) of all parameters are presented within the original parameter space. The plot of the growth status of mathematics ability is also provided.

    Math_LGCM_TIC_BLS_f <- getLGCM(
      dat = RMS_dat0, t_var = "T", y_var = "M", curveFun = "bilinear spline",
      intrinsic = TRUE, records = 1:9, growth_TIC = c("ex1", "ex2"),
      tries = 10, paramOut = TRUE
      )

    Math_LGCM_TIC_BLS_f@Estimates
    #>         Name Estimate     SE
    #> 1     alpha0  39.5902 0.5151
    #> 2     alpha1   1.8019 0.0170
    #> 3     alpha2   0.7792 0.0119
    #> 4     alphag  31.2000 0.0018
    #> 5      psi00 101.9917 7.4991
    #> 6      psi01   0.5358 0.1855
    #> 7      psi02  -0.4928 0.1785
    #> 8      psi0g -12.0062 5.7511
    #> 9      psi11   0.0897 0.0091
    #> 10     psi12   0.0016 0.0061
    #> 11     psi1g  -0.4548 0.2033
    #> 12     psi22   0.0261 0.0083
    #> 13     psi2g  -0.6429 0.2222
    #> 14     psigg  49.6612 8.2099
    #> 15 residuals  31.3298 0.8815
    #> 16    beta10   2.3188 0.7411
    #> 17    beta11  -0.0447 0.0261
    #> 18    beta12  -0.0206 0.0232
    #> 19    beta1g   0.1985 0.7101
    #> 20    beta20   2.1114 0.7513
    #> 21    beta21   0.1124 0.0263
    #> 22    beta22  -0.0148 0.0241
    #> 23    beta2g  -0.4938 0.7457
    #> 24      mux1   0.0000 0.0447
    #> 25      mux2   0.0000 0.0447
    #> 26     phi11   0.9980 0.0632
    #> 27     phi12   0.7759 0.0566
    #> 28     phi22   0.9980 0.0631
    #> 29    mueta0  39.5902 0.5480
    #> 30    mueta1   1.8019 0.0174
    #> 31    mueta2   0.7792 0.0120
    #> 32   mu_knot  31.2000 0.0163
    Figure1 <- getFigure(
      model = Math_LGCM_TIC_BLS_f@mxOutput, nClass = NULL, cluster_TIC = NULL, sub_Model = "LGCM",
      y_var = "M", curveFun = "BLS", y_model = "LGCM", t_var = "T", records = 1:9,
      m_var = NULL, x_var = NULL, x_type = NULL, xstarts = xstarts, xlab = "Month",
      outcome = "Mathematics"
    )
    #> Treating first argument as an object that stores a character
    show(Figure1)
    #> figOutput Object
    #> --------------------
    #> Trajectories: 1
    #> Figure 1:
    #> `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'

In the provided output, the terms alpha0, alpha1, alpha2, and alphag denote the conditional mean values of the intercept, first slope, second slope, and knot on growth TICs. The terms mueta0, mueta1, mueta2, and mu_knot represent the mean values of the intercept, first slope, second slope, and knot. These corresponding values match as we’ve centered the growth TICs.

The mean values of growth factors suggest that the rate of progress in mathematical ability slowed down after approximately 35.16 months since the beginning of the study. Moreover, the psi terms indicate the unexplained variance or covariance of the growth factors, while residuals corresponds to residual variance.

Moreover, beta10, beta11, beta12, and beta1g are path coefficients from the first growth TIC (i.e., baseline teacher-reported approach to learning) to the intercept, first slope, second slope, and knot, respectively. Similarly, beta2’s correspond to the path coefficients of the baseline teacher-reported attentional focus.

The interpretations of these coefficients are rather intuitive. For instance, a beta1g value of \(0.2187\) implies that a one standardized unit increase in baseline teacher-reported approach to learning delays the transition time to slower development by \(0.2187\) months. Similarly, a beta2g value of \(-0.9865\) indicates that a one standardized unit increase in baseline teacher-reported attentional focus advances the transition time to slower development by \(0.9865\) months.

However, caution is required when considering potential colinearity between growth TICs. The graphic representation of mathematical development also supports the observation that developmental pace slows down in later stages.