# Examples of Longitudinal Mixture 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", "getMIX_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)
    ## gx1 and gx2 are standardized cluster TICs in models
    RMS_dat0$gx1 <- scale(RMS_dat0$INCOME)
    RMS_dat0$gx2 <- scale(RMS_dat0$EDU)
    xstarts <- mean(baseT)

## Example 1: Fit bilinear spline LGCMs with 1-, 2-, and 3- latent classes to examine the heterogeneity in the development of mathematics skills. The enumeration process is conducted using the `getSummary()` function, with `HetModels = TRUE` specified.

    Math_BLS_LGCM1 <- getLGCM(
      dat = RMS_dat0, t_var = "T", y_var = "M", curveFun = "BLS", intrinsic = FALSE,
      records = 1:9, tries = 10
      )
    Math_BLS_LGCM2 <- getMIX(
      dat = RMS_dat0, prop_starts = c(0.45, 0.55), sub_Model = "LGCM", y_var = "M",
      t_var = "T", records = 1:9, curveFun = "BLS", intrinsic = FALSE, tries = 10
    )
    set.seed(20191029)
    Math_BLS_LGCM3 <- getMIX(
      dat = RMS_dat0, prop_starts = c(0.30, 0.40, 0.30), sub_Model = "LGCM", y_var = "M",
      t_var = "T", records = 1:9, curveFun = "BLS", intrinsic = FALSE, tries = 10
    )

    Figure1 <- getFigure(
      model = Math_BLS_LGCM1@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")'

    Figure2 <- getFigure(
      model = Math_BLS_LGCM2@mxOutput, nClass = 2, 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
    #> Treating first argument as an object that stores a character
    show(Figure2)
    #> figOutput Object
    #> --------------------
    #> Trajectories: 1
    #> Figure 1:
    #> `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'

    Figure3 <- getFigure(
      model = Math_BLS_LGCM3@mxOutput, nClass = 3, 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
    #> Treating first argument as an object that stores a character
    #> Treating first argument as an object that stores a character
    show(Figure3)
    #> figOutput Object
    #> --------------------
    #> Trajectories: 1
    #> Figure 1:
    #> `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'

    getSummary(model_list = list(Math_BLS_LGCM1@mxOutput, Math_BLS_LGCM2@mxOutput, Math_BLS_LGCM3@mxOutput),
               HetModels = TRUE)
    #> # A tibble: 3 × 11
    #>   Model  No_Params `-2ll`    AIC    BIC Y_res_c1 Y_res_c2 Y_res_c3 `%Class1`
    #>   <chr>      <int>  <dbl>  <dbl>  <dbl>    <dbl>    <dbl>    <dbl> <chr>
    #> 1 Model1        11 31347. 31369. 31416.     34.0     NA       NA   100%
    #> 2 Model2        23 31171. 31217. 31314.     30.5     33.0     NA   24.6%
    #> 3 Model3        35 31008. 31078. 31226.     29.6     31.0     31.5 22.6%
    #> # ℹ 2 more variables: `%Class2` <chr>, `%Class3` <chr>

## Example 2: Fit reduced bilinear spline bivariate LGCMs with three latent classes to analyze the heterogeneity in the co-development of reading and mathematics skills.

    set.seed(20191029)
    RM_BLS_PLGCM3 <- getMIX(
      dat = RMS_dat0, prop_starts = c(0.30, 0.40, 0.30), sub_Model = "MGM",
      cluster_TIC = c("gx1", "gx2"), t_var = c("T", "T"), y_var = c("R", "M"),
      curveFun = "BLS", intrinsic = FALSE, records = list(1:9, 1:9),
      y_model = "LGCM", tries = 10, paramOut = TRUE
      )

    Figure4 <- getFigure(
      model = RM_BLS_PLGCM3@mxOutput, nClass = 3, cluster_TIC = c("gx1", "gx2"),
      sub_Model = "MGM", y_var = c("R", "M"), curveFun = "BLS", y_model = "LGCM",
      t_var = c("T", "T"), records = list(1:9, 1:9), m_var = NULL, x_var = NULL,
      x_type = NULL, xstarts = xstarts, xlab = "Month",
      outcome = c("Reading", "Mathematics")
    )
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Treating first argument as an object that stores a character
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Treating first argument as an object that stores a character
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Treating first argument as an object that stores a character
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Treating first argument as an object that stores a character
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Treating first argument as an object that stores a character
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Treating first argument as an object that stores a character
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    #> Warning in warnModelCreatedByOldVersion(model): You are using OpenMx version
    #> 2.22.11 with a model created by OpenMx version 2.21.8. This may work fine
    #> (fingers crossed), but if you run into trouble then please recreate your model
    #> with the current version of OpenMx.
    show(Figure4)
    #> figOutput Object
    #> --------------------
    #> Trajectories: 2
    #>
    #> Trajectory 1 :
    #>   Figure 1:
    #> `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'

    #>
    #> Trajectory 2 :
    #>   Figure 1:
    #> `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'