You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用R语言基于CMMS评分临床数据集绘制ROC曲线并计算AUC

ROC Curve & AUC Calculation for CMMS Dementia Data

Hey there! Let's walk through exactly how to analyze your CMMS score and dementia dataset in R—we'll build the ROC curve and calculate the AUC using the pROC package, which is the go-to tool for this kind of work.

Step 1: Get Your Data into R First

First, we need to convert your grouped frequency data into a format R can work with. You gave counts per CMMS interval, so we'll start by creating a data frame with those details, then expand it into individual sample records (since ROC functions typically expect one row per patient).

# Create a data frame with your interval upper bounds and counts
cmms_counts <- data.frame(
  cmms_upper = c(5, 10, 15, 20, 25, 30),  # Using upper bounds since your cutoffs are "< X"
  non_dementia = c(0, 0, 3, 9, 16, 18),
  dementia = c(2, 1, 4, 5, 3, 1)
)

# Expand counts into individual patient records
# Non-dementia cases (label = 0)
non_dem <- data.frame(
  cmms_score = rep(cmms_counts$cmms_upper, cmms_counts$non_dementia),
  dementia_status = 0
)

# Dementia cases (label = 1)
dem <- data.frame(
  cmms_score = rep(cmms_counts$cmms_upper, cmms_counts$dementia),
  dementia_status = 1
)

# Combine into one full dataset
full_dataset <- rbind(non_dem, dem)

I used the interval upper bounds as the CMMS score for each group because your cutoff logic is "score < X = dementia"—this aligns with how you're planning to use the cutoffs clinically.

Step 2: Install & Load the pROC Package

If you haven't installed pROC yet, run this first:

install.packages("pROC")
library(pROC)

Step 3: Calculate ROC Curve & AUC

Now we'll use the roc() function to generate the ROC object, which holds all the metrics we need. The direction = "<" is critical here—it tells the function that lower CMMS scores are associated with positive (dementia) cases, which matches your clinical rule.

# Generate the ROC object
cmms_roc <- roc(
  response = full_dataset$dementia_status,
  predictor = full_dataset$cmms_score,
  direction = "<"  # Lower score = more likely to be dementia
)

# Print the AUC value
cat("AUC for CMMS predicting dementia:", round(auc(cmms_roc), 3), "\n")

Step 4: Plot the ROC Curve

Let's make a clean, informative plot with a reference line (for random chance) and the AUC value labeled:

# Plot the ROC curve
plot(cmms_roc,
     main = "ROC Curve: CMMS Score vs Dementia",
     xlab = "False Positive Rate (1 - Specificity)",
     ylab = "True Positive Rate (Sensitivity)",
     col = "#2c3e50",
     lwd = 2)

# Add the 50/50 random chance reference line
abline(a = 0, b = 1, lty = 2, col = "#95a5a6")

# Add the AUC value to the plot
text(x = 0.6, y = 0.2, paste0("AUC = ", round(auc(cmms_roc), 3)))

Bonus: Check Cutoff-Specific Metrics

If you want to see exactly how well each of your proposed cutoffs (<5, <10, <20, etc.) performs in terms of sensitivity and specificity, use the coords() function:

# Get sensitivity, specificity, and more for all possible cutoffs
cutoff_metrics <- coords(cmms_roc, x = "all", transpose = FALSE)
print(cutoff_metrics)

This will show you every possible cutoff point, along with its corresponding true positive rate, false positive rate, sensitivity, and specificity—super useful for picking the best cutoff for your clinical needs.


内容的提问来源于stack exchange,提问作者Jingxin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:36:29