library(dcmdata)
dtmr_data
#> # A tibble: 990 × 28
#> id `1` `2` `3` `4` `5` `6` `7` `8a` `8b` `8c` `8d` `9`
#> <fct> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
#> 1 0008… 1 1 0 1 0 0 1 1 0 1 1 0
#> 2 0009… 0 1 0 0 0 0 0 1 1 1 0 1
#> 3 0024… 0 1 0 0 0 0 1 1 1 1 0 0
#> 4 0031… 0 1 0 0 1 0 1 1 1 0 0 0
#> 5 0061… 0 1 1 0 0 0 0 0 0 1 0 0
#> 6 0087… 0 1 1 1 0 0 0 1 1 1 1 0
#> 7 0092… 0 1 1 1 1 0 0 1 1 1 0 0
#> 8 0097… 0 0 0 1 0 0 0 1 0 1 0 0
#> 9 0111… 0 1 1 0 0 0 0 1 0 1 1 0
#> 10 0121… 0 1 0 0 0 0 0 1 1 1 1 0
#> # ℹ 980 more rows
#> # ℹ 15 more variables: `10a` <int>, `10b` <int>, `10c` <int>, `11` <int>,
#> # `12` <int>, `13` <int>, `14` <int>, `15a` <int>, `15b` <int>, `15c` <int>,
#> # `16` <int>, `17` <int>, `18` <int>, `21` <int>, `22` <int>
dtmr_qmatrix
#> # A tibble: 27 × 5
#> item referent_units partitioning_iterating appropriateness
#> <chr> <dbl> <dbl> <dbl>
#> 1 1 1 0 0
#> 2 2 0 0 1
#> 3 3 0 1 0
#> 4 4 1 0 0
#> 5 5 1 0 0
#> 6 6 0 1 0
#> 7 7 1 0 0
#> 8 8a 0 0 1
#> 9 8b 0 0 1
#> 10 8c 0 0 1
#> # ℹ 17 more rows
#> # ℹ 1 more variable: multiplicative_comparison <dbl>Diagnostic classification models (DCMs) are confirmatory latent class models that estimate, for each respondent, the probability that they are proficient on each of a set of assessed skills, or attributes (Bradshaw, 2016). These probabilities are rich, but they are rarely what gets reported. Instead, results are usually summarized as classifications (e.g., proficient or not proficient), and the most common way to create those classifications is to label any respondent with a probability of proficiency above 0.5 as proficient (Bradshaw & Levy, 2019).
A threshold of 0.5 is intuitive, but nothing guarantees that it is the best choice. Depending on the attribute, a different threshold may do a better job of balancing the two kinds of errors we can make: classifying a non-proficient respondent as proficient (a false positive) and classifying a proficient respondent as not proficient (a false negative). The goal of thresholdr is to estimate an optimal probability classification threshold for each attribute.
An example model
To see how this works in practice, we’ll use the Diagnosing Teachers’ Multiplicative Reasoning (DTMR) data from the dcmdata package. These data were simulated to mirror the DTMR assessment (Bradshaw et al., 2014), which measures four attributes with 27 items. Because the data are simulated, dcmdata also includes the true attribute profiles for each respondent, which means we can check how close our estimated thresholds come to the true optimal thresholds.
Estimating the model
We’ll estimate a loglinear cognitive diagnostic model (LCDM; Henson et al., 2009) using measr (Thompson, 2023). For speed, we use method = "optim", which uses Stan’s optimizer to find point estimates of the model parameters.
library(measr)
dtmr_spec <- dcm_specify(
dtmr_qmatrix,
identifier = "item",
measurement_model = lcdm(),
structural_model = unconstrained()
)
dtmr_lcdm <- dcm_estimate(
dtmr_spec,
data = dtmr_data,
identifier = "id",
method = "optim",
seed = 1213
)We can then extract the estimated proficiency probabilities for each respondent with measr_extract().
dtmr_probs <- measr_extract(dtmr_lcdm, "attribute_prob")
dtmr_probs
#> # A tibble: 990 × 5
#> id referent_units partitioning_iterating appropriateness
#> <chr> <dbl> <dbl> <dbl>
#> 1 000809 0.996 0.999 0.962
#> 2 000994 0.00302 0.977 0.957
#> 3 002427 0.00125 0.908 0.951
#> 4 003128 0.995 0.994 0.958
#> 5 006198 0.0000359 0.950 0.0125
#> 6 008702 0.0683 0.998 0.951
#> 7 009276 0.975 0.995 0.979
#> 8 009767 0.000179 0.152 0.103
#> 9 011173 0.0301 0.993 0.492
#> 10 012199 0.00588 0.995 0.992
#> # ℹ 980 more rows
#> # ℹ 1 more variable: multiplicative_comparison <dbl>Finding an optimal threshold when the truth is known
If we knew every respondent’s true proficiency status, choosing a threshold would be a familiar problem. For every possible threshold, we can calculate the sensitivity (the proportion of truly proficient respondents classified as proficient) and specificity (the proportion of truly non-proficient respondents classified as not proficient). Plotting these values for all possible thresholds gives a receiver operating characteristic (ROC) curve, and there are many established criteria for selecting the “best” point on that curve.
Because the DTMR data is simulated, we know the true proficiency status for each respondent on each attribute (dtmr_true_profiles). Using these data, we can calculate the optimal threshold for the referent_units attribute using Youden’s J statistic (Youden, 1950), which selects the threshold that maximizes sensitivity + specificity − 1.
library(thresholdr)
library(dplyr)
comparison_data <-
left_join(
select(dtmr_probs, id, estimate = referent_units),
select(dcmdata::dtmr_true_profiles, id, truth = referent_units),
by = "id"
)
comparison_data
#> # A tibble: 990 × 3
#> id estimate truth
#> <chr> <dbl> <dbl>
#> 1 000809 0.996 1
#> 2 000994 0.00302 0
#> 3 002427 0.00125 0
#> 4 003128 0.995 1
#> 5 006198 0.0000359 0
#> 6 008702 0.0683 0
#> 7 009276 0.975 1
#> 8 009767 0.000179 0
#> 9 011173 0.0301 0
#> 10 012199 0.00588 0
#> # ℹ 980 more rows
calc_youden(
estimates = comparison_data$estimate,
truth = comparison_data$truth
)
#> [1] 0.2939304In this example, the optimal classification threshold is 0.294. thresholdr currently supports six criteria, each available as a calc_*() function. Different criteria can disagree (Perkins & Schisterman, 2006), so it is important to consider what you want to optimize for when choosing a criterion.
comparison_data |>
summarize(
cz = calc_cz(estimate, truth),
gmean = calc_gmean(estimate, truth),
iu = calc_iu(estimate, truth),
minp = calc_minp(estimate, truth),
topleft = calc_topleft(estimate, truth),
youden = calc_youden(estimate, truth)
)
#> # A tibble: 1 × 6
#> cz gmean iu minp topleft youden
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 0.294 0.294 0.294 0.591 0.294 0.294| Function | Method | optimal_method |
Criterion |
|---|---|---|---|
calc_cz() |
Concordance probability (Liu, 2012) | "cz" |
Maximize sensitivity × specificity |
calc_gmean() |
G-mean (Kubat & Matwin, 1997) | "gmean" |
Maximize √(sensitivity × specificity) |
calc_iu() |
Index of union (Unal, 2017) | "iu" |
Minimize distance of sensitivity and specificity from the AUC |
calc_minp() |
Minimum p-value (Miller & Siegmund, 1982) | "minp" |
Maximize the χ² association between true and predicted status |
calc_topleft() |
Closest to top-left (Perkins & Schisterman, 2006) | "topleft" |
Minimize distance to perfect classification |
calc_youden() |
Youden’s J statistic (Youden, 1950) | "youden" |
Maximize sensitivity + specificity − 1 |
Estimating an optimal threshold when the truth is unknown
In practice, of course, we never know respondents’ true proficiency status. All we have are the estimated probabilities of proficiency.
thresholdr addresses this by treating the estimated probabilities as a description of what the truth could plausibly be. A respondent with a probability of proficiency of 0.8 should be truly proficient in about 80% of plausible realities. By repeatedly generating sets of “true” classifications that are consistent with the estimated probabilities, we can calculate an optimal threshold for each generated set and summarize the results. thresholdr provides two approaches for doing this:
- Resampling: Generate many independent sets of “true” values directly from the estimated probabilities, find the optimal threshold for each set, and average them.
- Iteration: Search for a self-consistent threshold. Each iteration generates “true” values that are weighted toward the current threshold estimate, and then calculates a new optimal threshold from those values. Multiple chains are run from different starting points, and convergence is checked with the statistic (Vehtari et al., 2021).
Both approaches return the estimated optimal threshold along with the distribution of performance metrics (e.g., sensitivity and specificity) across all of the generated sets of “true” values.
find_threshold()
The main function in thresholdr is find_threshold(). When given an estimated model, find_threshold() extracts the probabilities of proficiency and estimates an optimal threshold for each attribute. By default, thresholds are estimated using resampling (approx_method = "resample") and Youden’s J statistic (optimal_method = "youden"). We can also manually specify additional threshold to compare to the estimated optimal threshold. In this example, we’ll supply comp_thresholds = 0.5 so that we can compare the estimated optimal threshold to the traditional value of 0.5.
set.seed(1213)
dtmr_thresholds <- find_threshold(dtmr_lcdm, comp_thresholds = 0.5)
dtmr_thresholds
#> # A tibble: 8 × 6
#> attribute threshold sensitivity specificity j_index
#> <chr> <dbl> <rvar[1d]> <rvar[1d]> <rvar[1d]>
#> 1 referent_units 0.354 0.92 ± 0.0132 0.93 ± 0.0078 0.85 ± 0.017
#> 2 referent_units 0.5 0.88 ± 0.0142 0.96 ± 0.0067 0.84 ± 0.017
#> 3 partitioning_iterating 0.513 0.93 ± 0.0095 0.93 ± 0.0100 0.86 ± 0.015
#> 4 partitioning_iterating 0.5 0.93 ± 0.0094 0.92 ± 0.0100 0.86 ± 0.015
#> 5 appropriateness 0.612 0.90 ± 0.0091 0.88 ± 0.0136 0.78 ± 0.018
#> 6 appropriateness 0.5 0.94 ± 0.0080 0.83 ± 0.0147 0.77 ± 0.018
#> 7 multiplicative_comparis… 0.614 0.91 ± 0.0084 0.94 ± 0.0101 0.85 ± 0.014
#> 8 multiplicative_comparis… 0.5 0.94 ± 0.0075 0.90 ± 0.0118 0.85 ± 0.015
#> # ℹ 1 more variable: distance <rvar[1d]>The result contains one row per threshold per attribute. For each attribute, the first row is the estimated optimal threshold and the second row is our comparison threshold of 0.5. The remaining columns are rvar objects from the posterior package. Each rvar contains the full distribution of a performance metric across all of the generated sets of “true” values, printed as a mean ± standard deviation. By default, the metrics are sensitivity, specificity, the J-index, and the distance from perfect classification (see probably::threshold_perf()); a different set of metrics can be requested with the metrics argument.
Because each rvar is a full distribution, we can summarize it however we like. For example, we can calculate the expected value and a 90% interval for the J-index of each threshold.
dtmr_thresholds |>
mutate(
j_mean = E(j_index),
j_lower = as.vector(quantile(j_index, probs = 0.05)),
j_upper = as.vector(quantile(j_index, probs = 0.95))
) |>
select(attribute, threshold, j_mean, j_lower, j_upper)
#> # A tibble: 8 × 5
#> attribute threshold j_mean j_lower j_upper
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 referent_units 0.354 0.851 0.822 0.878
#> 2 referent_units 0.5 0.839 0.812 0.866
#> 3 partitioning_iterating 0.513 0.856 0.833 0.881
#> 4 partitioning_iterating 0.5 0.856 0.832 0.881
#> 5 appropriateness 0.612 0.783 0.751 0.813
#> 6 appropriateness 0.5 0.770 0.739 0.799
#> 7 multiplicative_comparison 0.614 0.854 0.831 0.877
#> 8 multiplicative_comparison 0.5 0.846 0.821 0.870How close did we get?
Because the DTMR data are simulated, we can calculate the true optimal Youden threshold for each attribute and compare it to our estimates.
library(purrr)
all_comparison <- map(colnames(dtmr_probs)[-1], \(x) {
left_join(
select(dtmr_probs, id, estimate = !!x),
select(dcmdata::dtmr_true_profiles, id, truth = !!x),
by = "id"
) |>
mutate(attribute = x, .after = id)
}) |>
list_rbind()
threshold_comparison <- all_comparison |>
summarize(true_optimal = calc_youden(estimate, truth), .by = attribute) |>
left_join(
dtmr_thresholds |>
filter(threshold != .5) |>
select(attribute, estimated_threshold = threshold),
by = "attribute"
)
threshold_comparison
#> # A tibble: 4 × 3
#> attribute true_optimal estimated_threshold
#> <chr> <dbl> <dbl>
#> 1 referent_units 0.294 0.354
#> 2 partitioning_iterating 0.664 0.513
#> 3 appropriateness 0.701 0.612
#> 4 multiplicative_comparison 0.624 0.614For three of the four attributes (referent_units, appropriateness, and multiplicative_comparison), the estimated threshold moves away from 0.5 in the same direction as the true optimal threshold. For partitioning and iterating, however, the estimated threshold stays close to 0.5, even though the true optimal threshold is noticeably higher. The estimates are approximations, and they are only as good as the estimated probabilities they are based on.
We can visualize the results on the ROC curve for an attribute. The figure below shows the true ROC curve for referent units, along with the points corresponding to the true optimal threshold, the estimated optimal threshold, and the traditional threshold of 0.5.
Customizing the estimation
find_threshold() and the lower-level optimal_resample() and optimal_iterate() functions offer options for customizing the estimation. Some of the most useful are:
-
optimal_method: Choose a different criterion for the optimal threshold (see Table 1). -
comp_thresholds: Evaluate one or more competing thresholds alongside the optimal threshold. -
metrics: Supply ayardstick::metric_set()to change the performance metrics that are reported.
For example, we could estimate thresholds using the concordance probability method and report accuracy and Cohen’s kappa instead of the default metrics.
set.seed(1213)
find_threshold(
dtmr_lcdm,
optimal_method = "cz",
samples = 500,
metrics = metric_set(yardstick::accuracy, yardstick::kap)
)
#> # A tibble: 4 × 4
#> attribute threshold accuracy kap
#> <chr> <dbl> <rvar[1d]> <rvar[1d]>
#> 1 referent_units 0.350 0.93 ± 0.0076 0.84 ± 0.017
#> 2 partitioning_iterating 0.521 0.93 ± 0.0074 0.86 ± 0.015
#> 3 appropriateness 0.615 0.89 ± 0.0089 0.78 ± 0.019
#> 4 multiplicative_comparison 0.603 0.92 ± 0.0070 0.84 ± 0.015If you are working with probabilities that did not come from a model object, you can pass a vector of probabilities, rather than a fitted model, directly to optimal_resample() or optimal_iterate().
set.seed(1213)
optimal_resample(
estimates = dtmr_probs$appropriateness,
optimal_method = "youden",
comp_thresholds = 0.5
)
#> # A tibble: 2 × 5
#> threshold sensitivity specificity j_index distance
#> <dbl> <rvar[1d]> <rvar[1d]> <rvar[1d]> <rvar[1d]>
#> 1 0.607 0.90 ± 0.0094 0.88 ± 0.014 0.78 ± 0.019 0.024 ± 0.0043
#> 2 0.5 0.93 ± 0.0081 0.83 ± 0.015 0.77 ± 0.019 0.032 ± 0.0054Supported models
In addition to models estimated with measr, find_threshold() works with models estimated by several other R packages for DCMs:
-
measr:
measr::dcm_estimate() -
GDINA:
GDINA::GDINA() -
CDM:
CDM::din(),CDM::gdina(),CDM::gdm()(dichotomous attributes only), andCDM::mcdina()
