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.1) 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.1) 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   47.355    0.000
#>     y2              0.584      0.024   24.011    0.000
#>     y3              0.788      0.028   27.912    0.000
#> 
#> Regressions:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   fw ~          
#>     x1              0.468      0.019   24.371    0.000
#>     x2              0.379      0.026   14.664    0.000
#>     x3              0.200      0.018   11.141    0.000
#> 
#> Covariances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   x1 ~~         
#>     x2             -0.015      0.015   -0.998    0.318
#>     x3              0.009      0.022    0.419    0.675
#>   x2 ~~         
#>     x3              0.009      0.020    0.447    0.655
#> 
#> Variances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>    .fw              0.600      0.022   27.389    0.000
#>     x1              1.000                             
#>     x2              1.000                             
#>     x3              1.000                             
#>    .y1              0.496      0.021   23.340    0.000
#>    .y2              0.660      0.028   23.253    0.000
#>    .y3              0.379      0.045    8.503    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.722    0.000
#>     y2              0.811      0.070   11.531    0.000
#>     y3              0.902      0.027   32.790    0.000
#> 
#> Regressions:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   fb ~          
#>     w1              0.171      0.068    2.526    0.012
#>     w2              0.119      0.074    1.610    0.107
#> 
#> Covariances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   w1 ~~         
#>     w2              0.025      0.058    0.428    0.669
#> 
#> Variances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>    .fb              0.956      0.025   38.366    0.000
#>     w1              1.000                             
#>     w2              1.000                             
#>    .y1              0.189      0.047    4.059    0.000
#>    .y2              0.343      0.114    3.009    0.003
#>    .y3              0.187      0.050    3.770    0.000
#> 
#> Thresholds:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>     y1|t1          -2.703      0.109  -24.852    0.000
#>     y1|t2          -1.737      0.059  -29.476    0.000
#>     y1|t3          -0.435      0.048   -9.069    0.000
#>     y1|t4           0.337      0.050    6.694    0.000
#>     y1|t5           1.329      0.080   16.521    0.000
#>     y1|t6           2.332      0.111   21.047    0.000
#>     y2|t1          -2.960      0.160  -18.509    0.000
#>     y2|t2          -1.899      0.067  -28.356    0.000
#>     y2|t3          -0.987      0.040  -24.606    0.000
#>     y2|t4           0.210      0.040    5.318    0.000
#>     y2|t5           1.014      0.042   24.000    0.000
#>     y2|t6           2.190      0.067   32.707    0.000
#>     y3|t1          -2.231      0.126  -17.684    0.000
#>     y3|t2          -1.273      0.057  -22.448    0.000
#>     y3|t3           0.014      0.041    0.333    0.739
#>     y3|t4           0.701      0.053   13.213    0.000
#>     y3|t5           1.840      0.103   17.779    0.000
#>     y3|t6           2.687      0.201   13.350    0.000
#>     x1|t1          -2.364      0.087  -27.126    0.000
#>     x1|t2          -1.332      0.033  -40.315    0.000
#>     x1|t3          -0.656      0.028  -23.473    0.000
#>     x1|t4           0.472      0.023   20.472    0.000
#>     x1|t5           1.499      0.035   42.396    0.000
#>     x1|t6           2.806      0.114   24.521    0.000
#>     x2|t1          -2.823      0.106  -26.751    0.000
#>     x2|t2          -1.957      0.049  -39.726    0.000
#>     x2|t3          -1.021      0.025  -41.164    0.000
#>     x2|t4          -0.113      0.024   -4.806    0.000
#>     x2|t5           0.797      0.027   30.051    0.000
#>     x2|t6           1.993      0.046   43.723    0.000
#>     x3|t1          -2.996      0.144  -20.798    0.000
#>     x3|t2          -2.006      0.051  -39.126    0.000
#>     x3|t3          -0.851      0.027  -30.931    0.000
#>     x3|t4          -0.156      0.030   -5.120    0.000
#>     x3|t5           1.026      0.024   43.063    0.000
#>     x3|t6           2.143      0.048   44.371    0.000
#>     w1|t1          -2.071      0.214   -9.658    0.000
#>     w1|t2          -0.887      0.105   -8.419    0.000
#>     w1|t3           0.090      0.075    1.202    0.229
#>     w1|t4           1.276      0.119   10.693    0.000
#>     w2|t1          -2.028      0.230   -8.800    0.000
#>     w2|t2          -0.959      0.096   -9.968    0.000
#>     w2|t3          -0.138      0.085   -1.617    0.106
#>     w2|t4           0.713      0.098    7.311    0.000
#>     w2|t5           1.683      0.161   10.437    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.1) 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.1) 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.391    0.000
#>     y2              0.585      0.021   27.359    0.000
#>     y3              0.789      0.025   31.458    0.000
#> 
#> Regressions:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   fw ~          
#>     x1              0.470      0.027   17.705    0.000
#>     x2              0.379      0.036   10.543    0.000
#>     x3              0.196      0.034    5.824    0.000
#> 
#> Covariances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   x1 ~~         
#>     x2             -0.016      0.023   -0.696    0.487
#>     x3              0.005      0.023    0.224    0.822
#>   x2 ~~         
#>     x3              0.009      0.023    0.377    0.706
#> 
#> Variances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>    .fw              0.600      0.025   24.076    0.000
#>     x1              1.000                             
#>     x2              1.000                             
#>     x3              1.000                             
#>    .y1              0.496      0.037   13.465    0.000
#>    .y2              0.658      0.025   26.352    0.000
#>    .y3              0.377      0.040    9.517    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.044   20.407    0.000
#>     y2              0.810      0.060   13.490    0.000
#>     y3              0.904      0.037   24.460    0.000
#> 
#> Regressions:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   fb ~          
#>     w1              0.178      0.080    2.224    0.026
#>     w2              0.113      0.117    0.961    0.336
#>   s1 ~          
#>     w1              0.061      0.142    0.433    0.665
#>     w2              0.443      0.136    3.248    0.001
#>   s2 ~          
#>     w1             -0.255      0.219   -1.163    0.245
#>     w2              0.066      0.224    0.296    0.767
#> 
#> Covariances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>   w1 ~~         
#>     w2              0.016      0.042    0.378    0.705
#> 
#> Variances:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>    .fb              0.955      0.032   29.560    0.000
#>    .s1              0.799      0.115    6.937    0.000
#>    .s2              0.931      0.097    9.565    0.000
#>     w1              1.000                             
#>     w2              1.000                             
#>    .y1              0.188      0.080    2.356    0.018
#>    .y2              0.344      0.097    3.531    0.000
#>    .y3              0.182      0.067    2.725    0.006
#> 
#> Thresholds:
#>                  Estimate  Std.Error  z.value  P(>|z|)
#>     y1|t1          -2.745      0.137  -20.030    0.000
#>     y1|t2          -1.776      0.060  -29.471    0.000
#>     y1|t3          -0.439      0.045   -9.687    0.000
#>     y1|t4           0.335      0.048    6.944    0.000
#>     y1|t5           1.334      0.054   24.582    0.000
#>     y1|t6           2.419      0.140   17.223    0.000
#>     y2|t1          -2.952      0.173  -17.035    0.000
#>     y2|t2          -1.931      0.079  -24.360    0.000
#>     y2|t3          -0.992      0.038  -26.334    0.000
#>     y2|t4           0.215      0.038    5.614    0.000
#>     y2|t5           1.028      0.044   23.179    0.000
#>     y2|t6           2.189      0.079   27.644    0.000
#>     y3|t1          -2.229      0.124  -18.038    0.000
#>     y3|t2          -1.277      0.074  -17.259    0.000
#>     y3|t3           0.015      0.054    0.268    0.788
#>     y3|t4           0.710      0.059   12.110    0.000
#>     y3|t5           1.828      0.118   15.482    0.000
#>     y3|t6           2.657      0.223   11.935    0.000
#>     x1|t1          -2.388      0.083  -28.912    0.000
#>     x1|t2          -1.337      0.026  -51.553    0.000
#>     x1|t3          -0.661      0.024  -27.385    0.000
#>     x1|t4           0.489      0.016   30.960    0.000
#>     x1|t5           1.490      0.038   39.712    0.000
#>     x1|t6           2.716      0.161   16.919    0.000
#>     x2|t1          -2.700      0.096  -27.981    0.000
#>     x2|t2          -1.954      0.058  -33.498    0.000
#>     x2|t3          -1.025      0.026  -39.061    0.000
#>     x2|t4          -0.121      0.024   -4.997    0.000
#>     x2|t5           0.788      0.024   32.410    0.000
#>     x2|t6           2.009      0.050   40.422    0.000
#>     x3|t1          -3.243      0.159  -20.392    0.000
#>     x3|t2          -1.981      0.050  -39.289    0.000
#>     x3|t3          -0.859      0.028  -30.256    0.000
#>     x3|t4          -0.151      0.023   -6.451    0.000
#>     x3|t5           1.016      0.033   31.205    0.000
#>     x3|t6           2.143      0.066   32.552    0.000
#>     w1|t1          -2.032      0.258   -7.873    0.000
#>     w1|t2          -0.907      0.115   -7.921    0.000
#>     w1|t3           0.092      0.090    1.031    0.303
#>     w1|t4           1.277      0.136    9.402    0.000
#>     w2|t1          -2.055      0.201  -10.234    0.000
#>     w2|t2          -0.950      0.118   -8.042    0.000
#>     w2|t3          -0.137      0.088   -1.561    0.119
#>     w2|t4           0.703      0.092    7.674    0.000
#>     w2|t5           1.679      0.192    8.730    0.000