Estimating with SLCM

Overview

This vignette provides an introduction to the slcm package, which facilitates sparse latent class modeling. Sparse latent class modeling is useful for identifying subgroups within a population based on observed dichotomous data, while also enforcing sparsity to improve interpretability and prevent overfitting.

Installation

You can install the released version of slcm from CRAN with:

install.packages("slcm")

Or, you can be on the cutting-edge development version on GitHub using:

# install.packages("devtools")
devtools::install_github("tmsalab/slcm")

Usage

To use slcm, load the package using:

library(slcm)

From here, the SLCM model can be estimated using:

model_slcm = slcm::slcm(
  y = <data>,
  k = <k>
)

Estimating Sparse Latent Class Models

Preparing Data

The package requires an item matrix of dichtomous data. Each row represents an observation, and each column represents an item.

We can retrieve the matrix reasoning data set used in the SLCM paper from the edmdata package:

data("items_matrix_reasoning", package = "edmdata")

Model Estimation

To estimate a sparse latent class model, use the slcm() function. The basic usage involves specifying the data and the number of attributes (\(k\)).

# Use a demo data set from the paper

# Set a seed for reproducibility
set.seed(123)

# Set for demonstration purposes, increase to at least 1,000 in practice.
burnin = 50       

# Set for demonstration purposes, increase to at least 10,000 in practice. 
chain_length = 100 

model_reasoning = slcm(
  items_matrix_reasoning, k = 4, 
  burnin = burnin, chain_length = chain_length)

Model Output

The slcm() function returns an object of class slcm, which contains the estimated parameters (estimates), MCMC samples (chain), and model details (details).

You can access different components of the fitted model using accessor functions:

Class Probabilities

# Class probabilities
model_reasoning$estimates$pi$mean
       0000        0001        0010        0011        0100        0101 
0.130554445 0.127681591 0.010136258 0.019360519 0.009182326 0.173661949 
       0110        0111        1000        1001        1010        1011 
0.106776121 0.021352330 0.013473513 0.094931402 0.014677683 0.025805397 
       1100        1101        1110        1111 
0.047012334 0.085450895 0.094813583 0.025129655 

Item Probabilities

# Item response probabilities
model_reasoning$estimates$theta$mean
       Theta_0000 Theta_0001 Theta_0010 Theta_0011 Theta_0100 Theta_0101
Item1  0.43353663 0.92806041 0.87326792 0.99691604 0.75277699 0.98942625
Item2  0.54791147 0.98247067 0.88328982 0.99920989 0.76678117 0.99902234
Item3  0.42954184 0.68392622 0.59293850 0.80026762 0.52119739 0.76150710
Item4  0.15135931 0.19397317 0.42763267 0.72006347 0.16408751 0.24622420
Item5  0.53228830 0.56042404 0.81587368 0.83250427 0.60728936 0.63462300
Item6  0.22732794 0.48166208 0.77095969 0.92303422 0.22915380 0.48400785
Item7  0.09191903 0.12726223 0.13695356 0.18295630 0.12846063 0.17299544
Item8  0.18073979 0.25963844 0.48275660 0.56286441 0.32740665 0.41739421
Item9  0.44882929 0.82208132 0.48416205 0.90726274 0.48482363 0.95879556
Item10 0.14894326 0.15246935 0.27606754 0.28105356 0.43000001 0.43514213
Item11 0.34515541 0.38361048 0.43390019 0.59487946 0.44624695 0.57006820
Item12 0.14723320 0.16946374 0.70073121 0.73025218 0.17061386 0.19436202
Item13 0.13890218 0.14896180 0.44085542 0.45742097 0.15787379 0.16965577
Item14 0.12870803 0.13277828 0.43570009 0.44257196 0.44736497 0.45434486
Item15 0.20934256 0.21565627 0.44251471 0.44964846 0.82201608 0.82694170
Item16 0.20652884 0.50335198 0.47397378 0.76986470 0.59655828 0.85965019
Item17 0.10828044 0.12480934 0.16161787 0.29017317 0.11847130 0.18246231
Item18 0.04359916 0.04387375 0.20154305 0.20231811 0.21707466 0.21809445
Item19 0.13214936 0.19031802 0.17302415 0.30998836 0.39641030 0.63119210
Item20 0.07085707 0.09653346 0.09134135 0.19824890 0.26400453 0.31880466
Item21 0.05337965 0.08064665 0.06369332 0.09501866 0.06568464 0.09751470
Item22 0.04678944 0.05722293 0.07695318 0.09208618 0.06154046 0.07463584
Item23 0.14126435 0.15695150 0.27107158 0.29326223 0.47052831 0.49704834
Item24 0.12146827 0.12350459 0.40259891 0.40611350 0.44133992 0.44491894
Item25 0.36130064 0.41106069 0.50038495 0.55213621 0.70898589 0.75274330
       Theta_0110 Theta_0111 Theta_1000 Theta_1001 Theta_1010 Theta_1011
Item1  0.97720937  0.9998390 0.45046115 0.93381460 0.88205865  0.9972463
Item2  0.96644603  0.9999668 0.55143243 0.98276260 0.88501638  0.9992342
Item3  0.80187913  0.9313768 0.56653141 0.79590558 0.80115190  0.9374935
Item4  0.59159032  0.9153245 0.16709416 0.30256744 0.65453818  0.9729735
Item5  0.86508636  0.8796843 0.54771433 0.69318288 0.82489857  0.9035463
Item6  0.77270366  0.9238288 0.53467691 0.78393303 0.94126327  0.9877349
Item7  0.27288500  0.3361228 0.11710002 0.15833811 0.30927772  0.3775667
Item8  0.65794385  0.7263660 0.49226057 0.58938110 0.79430713  0.8309086
Item9  0.67230836  0.9897545 0.74117129 0.95320321 0.76728347  0.9785569
Item10 0.59710878  0.6014146 0.62782426 0.63168558 0.77081236  0.7731880
Item11 0.59355529  0.8400355 0.48077796 0.68748639 0.70909057  0.9322167
Item12 0.73288628  0.7611288 0.15657541 0.17974098 0.71401076  0.7426721
Item13 0.47305964  0.4899046 0.20117708 0.21242055 0.53581552  0.5524740
Item14 0.79021887  0.7951778 0.27505420 0.28072876 0.63748888  0.6442850
Item15 0.92142447  0.9247912 0.30360557 0.41888970 0.54708351  0.6551850
Item16 0.91650783  0.9872063 0.22759459 0.52697651 0.62823612  0.8661809
Item17 0.40855202  0.7751046 0.11871851 0.13700635 0.17481334  0.3082800
Item18 0.52434399  0.5256686 0.05203545 0.05234248 0.22273845  0.2235710
Item19 0.70051720  0.8999926 0.22444678 0.29852675 0.27614038  0.4349429
Item20 0.30564679  0.4864285 0.08778495 0.19445378 0.15696437  0.4536872
Item21 0.07773461  0.1139547 0.08570496 0.12288762 0.09980525  0.1416084
Item22 0.09806329  0.1163520 0.07485655 0.08772623 0.11515418  0.1331145
Item23 0.64438980  0.6694995 0.14622346 0.16245997 0.27746756  0.3001130
Item24 0.77541689  0.7786533 0.12198970 0.12402603 0.40359677  0.4071114
Item25 0.81373422  0.8482572 0.37812314 0.42916711 0.51865165  0.5700028
       Theta_1100 Theta_1101 Theta_1110 Theta_1111
Item1  0.76761152  0.9907330  0.9798881  0.9998708
Item2  0.76939329  0.9990348  0.9671473  0.9999677
Item3  0.72530061  0.8983539  0.9456151  0.9886205
Item4  0.22358785  0.4292888  0.8069765  0.9970584
Item5  0.62223503  0.7573103  0.8728428  0.9362519
Item6  0.53687564  0.7855759  0.9419676  0.9879215
Item7  0.19483788  0.2511107  0.5696088  0.6388837
Item8  0.66042600  0.7518251  0.9034853  0.9278667
Item9  0.76465193  0.9942371  0.8869680  0.9991365
Item10 0.87938378  0.8813997  0.9423916  0.9431554
Item11 0.89849839  0.9704135  0.9809660  0.9998488
Item12 0.18101725  0.2056845  0.7453187  0.7726707
Item13 0.22350834  0.2364183  0.5680925  0.5846986
Item14 0.65345990  0.6602539  0.9095328  0.9129182
Item15 0.88063786  0.9275865  0.9608816  0.9790731
Item16 0.94004634  0.9916510  0.9990029  0.9999305
Item17 0.12967953  0.1982191  0.4300917  0.7887947
Item18 0.24289802  0.2439670  0.5559749  0.5572889
Item19 0.52468908  0.7491641  0.8067705  0.9477343
Item20 0.29748575  0.4806122  0.4218423  0.7274963
Item21 0.10455826  0.1470504  0.1207377  0.1680240
Item22 0.09434918  0.1101639  0.1416019  0.1629035
Item23 0.47861230  0.5051577  0.6520811  0.6769139
Item24 0.44238827  0.4459673  0.7762872  0.7795236
Item25 0.77742126  0.8155063  0.8681653  0.8963811

Q Matrix

# Estimated 0/1 Q matrix
model_reasoning$estimates$q
       Q_1 Q_2 Q_3 Q_4
Item1    0   1   1   1
Item2    0   0   0   1
Item3    1   1   1   1
Item4    1   0   1   1
Item5    0   0   1   0
Item6    1   0   1   0
Item7    1   1   1   0
Item8    1   1   1   0
Item9    0   1   0   1
Item10   1   1   0   0
Item11   1   1   1   1
Item12   0   0   1   0
Item13   0   0   1   0
Item14   1   1   1   0
Item15   0   1   0   0
Item16   1   1   1   0
Item17   0   1   1   1
Item18   0   1   1   0
Item19   0   1   1   0
Item20   1   1   1   1
Item21   1   0   0   1
Item22   1   1   1   0
Item23   0   1   0   0
Item24   0   1   1   0
Item25   0   1   0   0