Examples of Longitudinal Mediation 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", "getMediation_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
RMS_dat0$ex1 <- scale(RMS_dat0$Approach_to_Learning)
xstarts <- mean(baseT)

Example 1: Fit longitudinal mediation model with a bilinear spline functional form to assess how the baseline teacher-reported approach to learning influences the development of mathematics ability, mediated through the development of reading ability.

paraMed2_BLS <- c(
  "muX", "phi11", "alphaM1", "alphaMr", "alphaM2", "mugM",
  paste0("psi", c("M1M1", "M1Mr", "M1M2", "MrMr", "MrM2", "M2M2"), "_r"),
  "alphaY1", "alphaYr", "alphaY2", "mugY",
  paste0("psi", c("Y1Y1", "Y1Yr", "Y1Y2", "YrYr", "YrY2", "Y2Y2"), "_r"),
  paste0("beta", rep(c("M", "Y"), each = 3), rep(c(1, "r", 2), 2)),
  paste0("beta", c("M1Y1", "M1Yr", "M1Y2", "MrYr", "MrY2", "M2Y2")),
  "muetaM1", "muetaMr", "muetaM2", "muetaY1", "muetaYr", "muetaY2", 
  paste0("Mediator", c("11", "1r", "12", "rr", "r2", "22")),
  paste0("total", c("1", "r", "2")),
  "residualsM", "residualsY", "residualsYM"
  )
Med2_LGCM_BLS <- getMediation(
  dat = RMS_dat0, t_var = rep("T", 2), y_var = "M", m_var = "R",
  x_type = "baseline", x_var = "ex1", curveFun = "bilinear spline", 
  records = list(1:9, 1:9), res_scale = c(0.1, 0.1), res_cor = 0.3, 
  paramOut = TRUE, names = paraMed2_BLS
  )
Med2_LGCM_BLS@Estimates
#>           Name Estimate      SE
#> 1          muX   0.0000  0.0447
#> 2        phi11   0.9980  0.0630
#> 3      alphaM1   2.1134  0.0246
#> 4      alphaMr 111.8319  0.7841
#> 5      alphaM2   0.6878  0.0134
#> 6         mugM  26.3108  0.2453
#> 7    psiM1M1_r   0.1935  0.0173
#> 8    psiM1Mr_r   4.6661  0.4371
#> 9    psiM1M2_r  -0.0278  0.0066
#> 10   psiMrMr_r 226.5296 15.3549
#> 11   psiMrM2_r  -1.9011  0.2145
#> 12   psiM2M2_r   0.0341  0.0045
#> 13     alphaY1   0.9622  0.0678
#> 14     alphaYr  19.0900  3.1871
#> 15     alphaY2   0.3800  0.2432
#> 16        mugY  34.7042  0.3575
#> 17   psiY1Y1_r   0.0553  0.0062
#> 18   psiY1Yr_r   1.7819  0.1920
#> 19   psiY1Y2_r  -0.0078  0.0043
#> 20   psiYrYr_r 104.2707  7.8922
#> 21   psiYrY2_r  -0.7595  0.1566
#> 22   psiY2Y2_r   0.0235  0.0052
#> 23      betaM1   0.0623  0.0231
#> 24      betaMr   5.5471  0.6945
#> 25      betaM2  -0.0468  0.0118
#> 26      betaY1   0.0149  0.0139
#> 27      betaYr   1.2907  0.5133
#> 28      betaY2  -0.0212  0.0135
#> 29    betaM1Y1   0.3807  0.0317
#> 30    betaM1Yr   0.1206  0.9362
#> 31    betaM1Y2   0.0548  0.0505
#> 32    betaMrYr   0.7277  0.0309
#> 33    betaMrY2  -0.0012  0.0020
#> 34    betaM2Y2   0.4813  0.1434
#> 35     muetaM1   2.1134  0.0247
#> 36     muetaMr 111.8319  0.8223
#> 37     muetaM2   0.6878  0.0136
#> 38     muetaY1   1.7667  0.0166
#> 39     muetaYr 100.7289  0.8923
#> 40     muetaY2   0.6942  0.0172
#> 41  Mediator11   0.0237  0.0090
#> 42  Mediator1r   0.0075  0.0585
#> 43  Mediator12   0.0034  0.0034
#> 44  Mediatorrr   4.0368  0.5298
#> 45  Mediatorr2  -0.0066  0.0111
#> 46  Mediator22  -0.0225  0.0088
#> 47      total1   0.0386  0.0156
#> 48      totalr   5.3350  0.6953
#> 49      total2  -0.0469  0.0133
#> 50  residualsM  33.8855  1.0615
#> 51  residualsY  40.5671  0.8725
#> 52 residualsYM   6.9264  0.6861

Example 2: Fit longitudinal mediation model with a bilinear spline functional form to assess how the development of reading ability influences the development of science ability, mediated through the development of mathematics ability.

paraMed3_BLS <- c(
  "muetaX1", "muetaXr", "muetaX2", "mugX", 
  paste0("psi", c("X1X1", "X1Xr", "X1X2", "XrXr", "XrX2", "X2X2")),
  "alphaM1", "alphaMr", "alphaM2", "mugM", 
  paste0("psi", c("M1M1", "M1Mr", "M1M2", "MrMr", "MrM2", "M2M2"), "_r"),
  "alphaY1", "alphaYr", "alphaY2", "mugY", 
  paste0("psi", c("Y1Y1", "Y1Yr", "Y1Y2", "YrYr", "YrY2", "Y2Y2"), "_r"),
  paste0("beta", c("X1Y1", "X1Yr", "X1Y2", "XrYr", "XrY2", "X2Y2",
                   "X1M1", "X1Mr", "X1M2", "XrMr", "XrM2", "X2M2",
                   "M1Y1", "M1Yr", "M1Y2", "MrYr", "MrY2", "M2Y2")),
  "muetaM1", "muetaMr", "muetaM2", "muetaY1", "muetaYr", "muetaY2", 
  paste0("mediator", c("111", "11r", "112", "1rr", "1r2", "122", "rr2", "r22", "rrr", "222")),
  paste0("total", c("11", "1r", "12", "rr", "r2", "22")),
  "residualsX", "residualsM", "residualsY", "residualsMX", "residualsYX", "residualsYM"
  )
set.seed(20191029)
Med3_LGCM_BLS <- getMediation(
  dat = RMS_dat0, t_var = rep("T", 3), y_var = "S", m_var = "M", x_type = "longitudinal", 
  x_var = "R", curveFun = "bilinear spline", records = list(2:9, 1:9, 1:9), 
  res_scale = c(0.1, 0.1, 0.1),  res_cor = c(0.3, 0.3), tries = 10, paramOut = TRUE, 
  names = paraMed3_BLS
  )
Med3_LGCM_BLS@Estimates
#>           Name Estimate      SE
#> 1      muetaX1   2.1134  0.0245
#> 2      muetaXr 111.8439  0.8270
#> 3      muetaX2   0.6874  0.0135
#> 4         mugX  26.3157  0.2478
#> 5      psiX1X1   0.1898  0.0174
#> 6      psiX1Xr   5.0488  0.4757
#> 7      psiX1X2  -0.0275  0.0066
#> 8      psiXrXr 259.0603 17.8840
#> 9      psiXrX2  -2.1602  0.2313
#> 10     psiX2X2   0.0341  0.0044
#> 11     alphaM1   0.9251  0.0670
#> 12     alphaMr  15.9474  3.0276
#> 13     alphaM2   0.4582  0.2806
#> 14        mugM  34.6342  0.3577
#> 15   psiM1M1_r   0.0544  0.0063
#> 16   psiM1Mr_r   1.7670  0.1998
#> 17   psiM1M2_r  -0.0073  0.0042
#> 18   psiMrMr_r 104.5097  8.3212
#> 19   psiMrM2_r  -0.7660  0.1576
#> 20   psiM2M2_r   0.0233  0.0051
#> 21     alphaY1   0.0432  0.0678
#> 22     alphaYr   0.6016  1.3882
#> 23     alphaY2  -1.1500  0.2806
#> 24        mugY  33.6805  1.0216
#> 25   psiY1Y1_r   0.0195  0.0041
#> 26   psiY1Yr_r   0.5092  0.0964
#> 27   psiY1Y2_r  -0.0010  0.0028
#> 28   psiYrYr_r  36.6572  3.3169
#> 29   psiYrY2_r  -0.3656  0.0836
#> 30   psiY2Y2_r   0.0079  0.0041
#> 31    betaX1Y1   0.3987  0.0313
#> 32    betaX1Yr   0.6677  1.2151
#> 33    betaX1Y2   0.0653  0.0571
#> 34    betaXrYr   0.7445  0.0347
#> 35    betaXrY2  -0.0020  0.0023
#> 36    betaX2Y2   0.4755  0.1700
#> 37    betaX1M1   0.1540  0.0371
#> 38    betaX1Mr   4.4495  1.2668
#> 39    betaX1M2  -0.1999  0.0700
#> 40    betaXrMr   0.1960  0.0348
#> 41    betaXrM2   0.0090  0.0028
#> 42    betaX2M2   0.8529  0.1818
#> 43    betaM1Y1   0.2718  0.0541
#> 44    betaM1Yr  -2.6244  1.5247
#> 45    betaM1Y2   0.0092  0.0928
#> 46    betaMrYr   0.2927  0.0367
#> 47    betaMrY2   0.0028  0.0024
#> 48    betaM2Y2   0.3738  0.1587
#> 49     muetaM1   1.7676  0.0166
#> 50     muetaMr 100.6304  0.8937
#> 51     muetaM2   0.6963  0.0171
#> 52     muetaY1   0.8493  0.0131
#> 53     muetaYr  56.7453  0.9210
#> 54     muetaY2   0.5806  0.0131
#> 55 mediator111   0.1084  0.0229
#> 56 mediator11r  -1.0462  0.6148
#> 57 mediator112   0.0037  0.0370
#> 58 mediator1rr   0.1954  0.3497
#> 59 mediator1r2   0.0019  0.0039
#> 60 mediator122   0.0244  0.0269
#> 61 mediatorrr2   0.2179  0.0308
#> 62 mediatorr22   0.0021  0.0018
#> 63 mediatorrrr  -0.0008  0.0010
#> 64 mediator222   0.1777  0.0713
#> 65     total11   0.2624  0.0273
#> 66     total1r   3.5987  1.2804
#> 67     total12  -0.1700  0.0633
#> 68     totalrr   0.4140  0.0339
#> 69     totalr2   0.0103  0.0024
#> 70     total22   1.0306  0.1773
#> 71  residualsX  41.1750  1.0840
#> 72  residualsM  19.4421  0.8803
#> 73  residualsY  33.9661  0.5548
#> 74 residualsMX   7.0083  0.7114
#> 75 residualsYX   1.8316  0.5713
#> 76 residualsYM   2.5687  0.5551