This vignette shows examples of two-level (multilevel) models, with and without random slopes, with both continuous and ordinal data. Two-level models are specified with separate models for the within (level: 1) and between (level: 2) levels, and the cluster variable is given by the cluster argument.

Two-Level Model

two_level_model <- "
  level: 1
    fw =~ y1 + y2 + y3
    fw ~ x1 + x2 + x3
  level: 2
    fb =~ y1 + y2 + y3
    fb ~ w1 + w2
"

Continuous Indicators

fit_two_level_cont <- pls(
  two_level_model,
  data      = randomSlopes,
  cluster   = "cluster",
  bootstrap = TRUE,
  boot.R    = 50
)
summary(fit_two_level_cont)
#> plssem (0.2.0) ended normally after 68 iterations
#>   Estimator                                MC-PLSc-MLM
#>   Link                                          LINEAR
#>   Standard errors            Delta (cluster bootstrap)
#>                                                       
#>   Number of observations                          3000
#>   Number of clusters                               200
#>   Number of iterations                              68
#> 
#> Intraclass correlations:
#>   y1                                             0.334
#>   y2                                             0.196
#>   y3                                             0.514
#> 
#> Fit Measures:
#>                                                 Within  Between
#>   Chi-Square                                     7.397    8.466
#>   Degrees of Freedom                                 6        4
#>   SRMR                                           0.006    0.023
#>   RMSEA                                          0.009    0.075
#> 
#> 
#> Level 1 [within]:
#> 
#> R-squared [indicators]:
#>   y1                                             0.515
#>   y2                                             0.339
#>   y3                                             0.634
#> 
#> R-squared [latents]:
#>   fw                                             0.422
#> 
#> Latent Variables:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   fw =~         
#>     y1              0.717      0.014   51.760    0.000
#>     y2              0.582      0.014   40.960    0.000
#>     y3              0.797      0.013   63.569    0.000
#> 
#> Regressions:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   fw ~          
#>     x1              0.480      0.019   24.816    0.000
#>     x2              0.393      0.021   18.735    0.000
#>     x3              0.194      0.020    9.652    0.000
#> 
#> Covariances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   x1 ~~         
#>     x2             -0.022      0.014   -1.563    0.118
#>     x3              0.012      0.020    0.596    0.551
#>   x2 ~~         
#>     x3              0.011      0.017    0.657    0.511
#> 
#> Variances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>    .fw              0.578      0.020   28.941    0.000
#>     x1              1.000                             
#>     x2              1.000                             
#>     x3              1.000                             
#>    .y1              0.485      0.020   24.410    0.000
#>    .y2              0.661      0.017   39.950    0.000
#>    .y3              0.366      0.020   18.314    0.000
#> 
#> 
#> Level 2 [between]:
#> 
#> R-squared [indicators]:
#>   y1                                             0.825
#>   y2                                             0.656
#>   y3                                             0.830
#> 
#> R-squared [latents]:
#>   fb                                             0.046
#> 
#> Latent Variables:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   fb =~         
#>     y1              0.908      0.026   34.274    0.000
#>     y2              0.810      0.052   15.537    0.000
#>     y3              0.911      0.024   37.721    0.000
#> 
#> Regressions:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   fb ~          
#>     w1              0.169      0.073    2.312    0.021
#>     w2              0.131      0.083    1.578    0.114
#> 
#> Covariances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   w1 ~~         
#>     w2              0.004      0.082    0.050    0.960
#> 
#> Variances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>    .fb              0.954      0.030   31.374    0.000
#>     w1              1.000                             
#>     w2              1.000                             
#>    .y1              0.175      0.048    3.640    0.000
#>    .y2              0.344      0.084    4.076    0.000
#>    .y3              0.170      0.044    3.850    0.000

Ordered Indicators

fit_two_level_ord <- pls(
  two_level_model,
  data      = randomSlopesOrdered,
  cluster   = "cluster",
  bootstrap = TRUE,
  boot.R    = 50,
  ordered   = colnames(randomSlopesOrdered) # explicitly specify variables as ordered
)
summary(fit_two_level_ord)
#> plssem (0.2.0) ended normally after 83 iterations
#>   Estimator                             MC-OrdPLSc-MLM
#>   Link                                          PROBIT
#>   Standard errors            Delta (cluster bootstrap)
#>                                                       
#>   Number of observations                          3000
#>   Number of clusters                               200
#>   Number of iterations                              83
#> 
#> Intraclass correlations:
#>   y1                                             0.341
#>   y2                                             0.194
#>   y3                                             0.514
#> 
#> Fit Measures:
#>                                                 Within  Between
#>   Chi-Square                                     3.345    5.073
#>   Degrees of Freedom                                 6        4
#>   SRMR                                           0.005    0.020
#>   RMSEA                                          0.000    0.037
#> 
#> 
#> Level 1 [within]:
#> 
#> R-squared [indicators]:
#>   y1                                             0.504
#>   y2                                             0.340
#>   y3                                             0.621
#> 
#> R-squared [latents]:
#>   fw                                             0.400
#> 
#> Latent Variables:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   fw =~         
#>     y1              0.710      0.015   48.832    0.000
#>     y2              0.584      0.024   24.594    0.000
#>     y3              0.788      0.027   28.830    0.000
#> 
#> Regressions:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   fw ~          
#>     x1              0.468      0.019   24.198    0.000
#>     x2              0.379      0.025   15.057    0.000
#>     x3              0.200      0.017   11.490    0.000
#> 
#> Covariances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   x1 ~~         
#>     x2             -0.015      0.016   -0.992    0.321
#>     x3              0.009      0.022    0.412    0.680
#>   x2 ~~         
#>     x3              0.009      0.021    0.440    0.660
#> 
#> Variances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>    .fw              0.600      0.022   27.586    0.000
#>     x1              1.000                             
#>     x2              1.000                             
#>     x3              1.000                             
#>    .y1              0.496      0.021   24.067    0.000
#>    .y2              0.660      0.028   23.818    0.000
#>    .y3              0.379      0.043    8.783    0.000
#> 
#> 
#> Level 2 [between]:
#> 
#> R-squared [indicators]:
#>   y1                                             0.811
#>   y2                                             0.657
#>   y3                                             0.813
#> 
#> R-squared [latents]:
#>   fb                                             0.044
#> 
#> Latent Variables:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   fb =~         
#>     y1              0.900      0.026   34.715    0.000
#>     y2              0.811      0.070   11.559    0.000
#>     y3              0.902      0.028   32.515    0.000
#> 
#> Regressions:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   fb ~          
#>     w1              0.171      0.070    2.430    0.015
#>     w2              0.119      0.074    1.597    0.110
#> 
#> Covariances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   w1 ~~         
#>     w2              0.025      0.059    0.416    0.677
#> 
#> Variances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>    .fb              0.956      0.025   38.490    0.000
#>     w1              1.000                             
#>     w2              1.000                             
#>    .y1              0.189      0.047    4.058    0.000
#>    .y2              0.343      0.114    3.016    0.003
#>    .y3              0.187      0.050    3.738    0.000
#> 
#> Thresholds:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>     y1|t1          -2.703      0.108  -25.097    0.000
#>     y1|t2          -1.737      0.057  -30.535    0.000
#>     y1|t3          -0.435      0.046   -9.522    0.000
#>     y1|t4           0.337      0.051    6.554    0.000
#>     y1|t5           1.329      0.078   16.996    0.000
#>     y1|t6           2.332      0.110   21.152    0.000
#>     y2|t1          -2.960      0.159  -18.561    0.000
#>     y2|t2          -1.899      0.066  -28.702    0.000
#>     y2|t3          -0.987      0.041  -23.935    0.000
#>     y2|t4           0.210      0.040    5.259    0.000
#>     y2|t5           1.014      0.039   25.751    0.000
#>     y2|t6           2.190      0.066   33.137    0.000
#>     y3|t1          -2.231      0.126  -17.757    0.000
#>     y3|t2          -1.273      0.055  -22.972    0.000
#>     y3|t3           0.014      0.042    0.331    0.741
#>     y3|t4           0.701      0.054   13.038    0.000
#>     y3|t5           1.840      0.097   19.043    0.000
#>     y3|t6           2.687      0.202   13.274    0.000
#>     x1|t1          -2.364      0.087  -27.126    0.000
#>     x1|t2          -1.332      0.034  -38.839    0.000
#>     x1|t3          -0.656      0.026  -25.152    0.000
#>     x1|t4           0.472      0.024   19.363    0.000
#>     x1|t5           1.499      0.036   42.214    0.000
#>     x1|t6           2.806      0.114   24.521    0.000
#>     x2|t1          -2.823      0.105  -26.769    0.000
#>     x2|t2          -1.957      0.049  -39.729    0.000
#>     x2|t3          -1.021      0.026  -39.733    0.000
#>     x2|t4          -0.113      0.023   -5.031    0.000
#>     x2|t5           0.797      0.026   30.628    0.000
#>     x2|t6           1.993      0.046   43.546    0.000
#>     x3|t1          -2.996      0.144  -20.775    0.000
#>     x3|t2          -2.006      0.051  -39.272    0.000
#>     x3|t3          -0.851      0.028  -30.132    0.000
#>     x3|t4          -0.156      0.031   -5.086    0.000
#>     x3|t5           1.026      0.022   46.425    0.000
#>     x3|t6           2.143      0.048   44.414    0.000
#>     w1|t1          -2.071      0.214   -9.658    0.000
#>     w1|t2          -0.887      0.112   -7.904    0.000
#>     w1|t3           0.090      0.081    1.123    0.261
#>     w1|t4           1.276      0.119   10.761    0.000
#>     w2|t1          -2.028      0.231   -8.782    0.000
#>     w2|t2          -0.959      0.092  -10.452    0.000
#>     w2|t3          -0.138      0.083   -1.662    0.097
#>     w2|t4           0.713      0.091    7.842    0.000
#>     w2|t5           1.683      0.152   11.111    0.000

Random Slopes Model

Random slopes are specified with rv() modifiers at level 1, where the random slopes (here s1 and s2) are variables at level 2.

slopes_model <- '
  level: 1
    fw =~ y1 + y2 + y3
    fw ~ rv("s1")*x1 + rv("s2")*x2 + x3
  level: 2
    fb =~ y1 + y2 + y3
    fb ~ w1 + w2
    # the random slopes are latent variables at the between level
    s1 + s2 ~ w1 + w2
'

Continuous Indicators

fit_slopes_cont <- pls(
  slopes_model,
  data      = randomSlopes,
  cluster   = "cluster",
  bootstrap = TRUE,
  boot.R    = 50
)
summary(fit_slopes_cont)
#> plssem (0.2.0) ended normally after 107 iterations
#>   Estimator                                MC-PLSc-MLM
#>   Link                                          LINEAR
#>   Standard errors            Delta (cluster bootstrap)
#>                                                       
#>   Number of observations                          3000
#>   Number of clusters                               200
#>   Number of iterations                             107
#> 
#> Intraclass correlations:
#>   y1                                             0.331
#>   y2                                             0.193
#>   y3                                             0.510
#> 
#> Random slopes (standard deviations):
#>   s1 (fw ~ x1)                                   0.191
#>   s2 (fw ~ x2)                                   0.138
#> 
#> Fit Measures:
#>                                                 Within  Between
#>   Chi-Square                                     7.469   17.316
#>   Degrees of Freedom                                 6       11
#>   SRMR                                           0.007    0.031
#>   RMSEA                                          0.009    0.054
#> 
#> 
#> Level 1 [within]:
#> 
#> R-squared [indicators]:
#>   y1                                             0.515
#>   y2                                             0.338
#>   y3                                             0.634
#> 
#> R-squared [latents]:
#>   fw                                             0.424
#> 
#> Latent Variables:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   fw =~         
#>     y1              0.717      0.013   56.386    0.000
#>     y2              0.581      0.016   37.294    0.000
#>     y3              0.796      0.012   63.964    0.000
#> 
#> Regressions:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   fw ~          
#>     x1              0.482      0.016   30.311    0.000
#>     x2              0.393      0.019   21.157    0.000
#>     x3              0.191      0.020    9.756    0.000
#> 
#> Covariances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   x1 ~~         
#>     x2             -0.021      0.020   -1.031    0.303
#>     x3              0.012      0.017    0.712    0.476
#>   x2 ~~         
#>     x3              0.011      0.019    0.568    0.570
#> 
#> Variances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>    .fw              0.576      0.018   32.901    0.000
#>     x1              1.000                             
#>     x2              1.000                             
#>     x3              1.000                             
#>    .y1              0.485      0.018   26.583    0.000
#>    .y2              0.662      0.018   36.509    0.000
#>    .y3              0.366      0.020   18.457    0.000
#> 
#> 
#> Level 2 [between]:
#> 
#> R-squared [indicators]:
#>   y1                                             0.822
#>   y2                                             0.652
#>   y3                                             0.834
#> 
#> R-squared [latents]:
#>   fb                                             0.044
#>   s1                                             0.265
#>   s2                                             0.043
#> 
#> Latent Variables:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   fb =~         
#>     y1              0.906      0.024   37.558    0.000
#>     y2              0.807      0.030   27.230    0.000
#>     y3              0.913      0.020   45.199    0.000
#> 
#> Regressions:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   fb ~          
#>     w1              0.170      0.072    2.376    0.018
#>     w2              0.120      0.070    1.716    0.086
#>   s1 ~          
#>     w1              0.161      0.129    1.252    0.210
#>     w2              0.487      0.095    5.122    0.000
#>   s2 ~          
#>     w1             -0.167      0.205   -0.815    0.415
#>     w2              0.126      0.217    0.579    0.563
#> 
#> Covariances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   w1 ~~         
#>     w2              0.005      0.059    0.084    0.933
#> 
#> Variances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>    .fb              0.956      0.030   31.645    0.000
#>    .s1              0.735      0.105    6.996    0.000
#>    .s2              0.957      0.081   11.740    0.000
#>     w1              1.000                             
#>     w2              1.000                             
#>    .y1              0.178      0.044    4.079    0.000
#>    .y2              0.348      0.048    7.270    0.000
#>    .y3              0.166      0.037    4.509    0.000

Ordered Indicators

fit_slopes_ord <- pls(
  slopes_model,
  data      = randomSlopesOrdered,
  cluster   = "cluster",
  bootstrap = TRUE,
  boot.R    = 50,
  ordered   = colnames(randomSlopesOrdered) # explicitly specify variables as ordered
)
summary(fit_slopes_ord)
#> plssem (0.2.0) ended normally after 121 iterations
#>   Estimator                             MC-OrdPLSc-MLM
#>   Link                                          PROBIT
#>   Standard errors            Delta (cluster bootstrap)
#>                                                       
#>   Number of observations                          3000
#>   Number of clusters                               200
#>   Number of iterations                             121
#> 
#> Intraclass correlations:
#>   y1                                             0.342
#>   y2                                             0.193
#>   y3                                             0.515
#> 
#> Random slopes (standard deviations):
#>   s1 (fw ~ x1)                                   0.215
#>   s2 (fw ~ x2)                                   0.145
#> 
#> Fit Measures:
#>                                                 Within  Between
#>   Chi-Square                                     3.230   21.503
#>   Degrees of Freedom                                 6       11
#>   SRMR                                           0.005    0.056
#>   RMSEA                                          0.000    0.069
#> 
#> 
#> Level 1 [within]:
#> 
#> R-squared [indicators]:
#>   y1                                             0.504
#>   y2                                             0.342
#>   y3                                             0.623
#> 
#> R-squared [latents]:
#>   fw                                             0.400
#> 
#> Latent Variables:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   fw =~         
#>     y1              0.710      0.026   27.162    0.000
#>     y2              0.585      0.021   27.676    0.000
#>     y3              0.789      0.025   31.949    0.000
#> 
#> Regressions:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   fw ~          
#>     x1              0.470      0.027   17.680    0.000
#>     x2              0.379      0.035   10.821    0.000
#>     x3              0.196      0.034    5.813    0.000
#> 
#> Covariances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   x1 ~~         
#>     x2             -0.016      0.024   -0.693    0.488
#>     x3              0.005      0.023    0.222    0.824
#>   x2 ~~         
#>     x3              0.009      0.023    0.377    0.706
#> 
#> Variances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>    .fw              0.600      0.025   23.561    0.000
#>     x1              1.000                             
#>     x2              1.000                             
#>     x3              1.000                             
#>    .y1              0.496      0.037   13.352    0.000
#>    .y2              0.658      0.025   26.657    0.000
#>    .y3              0.377      0.039    9.666    0.000
#> 
#> 
#> Level 2 [between]:
#> 
#> R-squared [indicators]:
#>   y1                                             0.812
#>   y2                                             0.656
#>   y3                                             0.818
#> 
#> R-squared [latents]:
#>   fb                                             0.045
#>   s1                                             0.201
#>   s2                                             0.069
#> 
#> Latent Variables:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   fb =~         
#>     y1              0.901      0.045   20.148    0.000
#>     y2              0.810      0.060   13.447    0.000
#>     y3              0.904      0.037   24.423    0.000
#> 
#> Regressions:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   fb ~          
#>     w1              0.178      0.091    1.966    0.049
#>     w2              0.113      0.115    0.979    0.327
#>   s1 ~          
#>     w1              0.061      0.152    0.404    0.687
#>     w2              0.443      0.134    3.309    0.001
#>   s2 ~          
#>     w1             -0.255      0.222   -1.147    0.251
#>     w2              0.066      0.224    0.296    0.767
#> 
#> Covariances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   w1 ~~         
#>     w2              0.016      0.042    0.379    0.705
#> 
#> Variances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>    .fb              0.955      0.035   27.547    0.000
#>    .s1              0.799      0.114    7.028    0.000
#>    .s2              0.931      0.099    9.361    0.000
#>     w1              1.000                             
#>     w2              1.000                             
#>    .y1              0.188      0.081    2.327    0.020
#>    .y2              0.344      0.098    3.519    0.000
#>    .y3              0.182      0.067    2.721    0.007
#> 
#> Thresholds:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>     y1|t1          -2.745      0.137  -20.052    0.000
#>     y1|t2          -1.776      0.060  -29.609    0.000
#>     y1|t3          -0.439      0.046   -9.497    0.000
#>     y1|t4           0.335      0.049    6.881    0.000
#>     y1|t5           1.334      0.054   24.709    0.000
#>     y1|t6           2.419      0.142   17.040    0.000
#>     y2|t1          -2.952      0.175  -16.859    0.000
#>     y2|t2          -1.931      0.081  -23.739    0.000
#>     y2|t3          -0.992      0.037  -26.802    0.000
#>     y2|t4           0.215      0.039    5.562    0.000
#>     y2|t5           1.028      0.040   25.739    0.000
#>     y2|t6           2.189      0.079   27.746    0.000
#>     y3|t1          -2.229      0.125  -17.774    0.000
#>     y3|t2          -1.277      0.077  -16.674    0.000
#>     y3|t3           0.015      0.055    0.262    0.793
#>     y3|t4           0.710      0.060   11.795    0.000
#>     y3|t5           1.828      0.121   15.145    0.000
#>     y3|t6           2.657      0.229   11.625    0.000
#>     x1|t1          -2.388      0.083  -28.912    0.000
#>     x1|t2          -1.337      0.023  -56.939    0.000
#>     x1|t3          -0.661      0.026  -25.869    0.000
#>     x1|t4           0.489      0.016   31.238    0.000
#>     x1|t5           1.490      0.036   40.877    0.000
#>     x1|t6           2.716      0.161   16.919    0.000
#>     x2|t1          -2.700      0.097  -27.954    0.000
#>     x2|t2          -1.954      0.058  -33.599    0.000
#>     x2|t3          -1.025      0.025  -40.341    0.000
#>     x2|t4          -0.121      0.025   -4.763    0.000
#>     x2|t5           0.788      0.024   33.446    0.000
#>     x2|t6           2.009      0.050   40.495    0.000
#>     x3|t1          -3.243      0.159  -20.380    0.000
#>     x3|t2          -1.981      0.050  -39.289    0.000
#>     x3|t3          -0.859      0.028  -30.562    0.000
#>     x3|t4          -0.151      0.023   -6.541    0.000
#>     x3|t5           1.016      0.031   32.977    0.000
#>     x3|t6           2.143      0.066   32.543    0.000
#>     w1|t1          -2.032      0.258   -7.873    0.000
#>     w1|t2          -0.907      0.118   -7.673    0.000
#>     w1|t3           0.092      0.087    1.061    0.289
#>     w1|t4           1.277      0.132    9.692    0.000
#>     w2|t1          -2.055      0.200  -10.256    0.000
#>     w2|t2          -0.950      0.108   -8.776    0.000
#>     w2|t3          -0.137      0.086   -1.593    0.111
#>     w2|t4           0.703      0.093    7.538    0.000
#>     w2|t5           1.679      0.184    9.124    0.000