# 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, res_scale = 0.1
    )
    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, res_scale = 0.1
    )

    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 31261.60              4485             NA
    #> Reduced Model              11 31347.39              4489       85.78891
    #>               Diff in DoF p.values      AIC      BIC
    #> Full Model             NA     <NA> 31291.60 31354.82
    #> 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.

    paraBLS.TIC_LGCM.f <- c(
      "alpha0", "alpha1", "alpha2", "alphag",
      paste0("psi", c("00", "01", "02", "0g", "11", "12", "1g", "22", "2g", "gg")),
      "residuals",
      paste0("beta1", c(0:2, "g")), paste0("beta2", c(0:2, "g")),
      paste0("mux", 1:2), paste0("phi", c("11", "12", "22")),
      "mueta0", "mueta1", "mueta2", "mu_knot"
      )
    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"), res_scale = 0.1,
      paramOut = TRUE, names = paraBLS.TIC_LGCM.f
      )

    Math_LGCM_TIC_BLS_f@Estimates
    #>         Name Estimate     SE
    #> 1     alpha0  39.4616 0.4859
    #> 2     alpha1   1.7616 0.0166
    #> 3     alpha2   0.6787 0.0157
    #> 4     alphag  35.1612 0.4296
    #> 5      psi00 101.7760 7.4808
    #> 6      psi01   0.5426 0.1818
    #> 7      psi02  -0.4301 0.1713
    #> 8      psi0g -16.5414 4.7124
    #> 9      psi11   0.0874 0.0089
    #> 10     psi12  -0.0046 0.0058
    #> 11     psi1g  -0.5309 0.1805
    #> 12     psi22   0.0138 0.0084
    #> 13     psi2g  -0.1142 0.1838
    #> 14     psigg  29.5487 6.3320
    #> 15 residuals  31.4936 0.8937
    #> 16    beta10   2.3116 0.8618
    #> 17    beta11  -0.0439 0.0336
    #> 18    beta12  -0.0190 0.0268
    #> 19    beta1g   0.2187 0.9361
    #> 20    beta20   2.0918 0.8289
    #> 21    beta21   0.1149 0.0311
    #> 22    beta22  -0.0149 0.0265
    #> 23    beta2g  -0.9865 0.8716
    #> 24      mux1   0.0000 0.0447
    #> 25      mux2   0.0000 0.0447
    #> 26     phi11   0.9980 0.0631
    #> 27     phi12   0.7759 0.0565
    #> 28     phi22   0.9980 0.0631
    #> 29    mueta0  39.4616 0.5201
    #> 30    mueta1   1.7616 0.0170
    #> 31    mueta2   0.6787 0.0157
    #> 32   mu_knot  35.1612 0.4312
    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.