如何将命中率与误报率数据导入pROC包的roc对象?
Absolutely! The pROC package is fully capable of creating a roc object from your cumulative empirical ROC point data—perfect for fitting curves and calculating partial AUC values in your recognition memory task. Here's a step-by-step guide tailored to your data:
Step 1: Prepare Your Data
First, store your cumulative false alarm rates (FPR = 1 - Specificity) and hit rates (TPR = Sensitivity) as vectors. Make sure they're ordered by increasing confidence threshold (i.e., FPR and TPR both rise as you lower the confidence cutoff, which matches your provided data):
library(pROC) # Your cumulative empirical ROC points fpr <- c(0.05, 0.11, 0.20, 0.28, 0.45) tpr <- c(0.45, 0.52, 0.57, 0.59, 0.62)
Step 2: Create a ROC Object
Since you don't have raw participant-level data, you can construct a valid roc object by initializing a dummy object and replacing its core sensitivity/specificity values with your empirical points. This is a clean workaround that works seamlessly with pROC's downstream functions:
# Initialize a dummy ROC object (we'll overwrite its core data next) roc_obj <- roc(response = c(1, 0), predictor = c(1, 0)) # Replace with your empirical data roc_obj$sensitivities <- tpr roc_obj$specificities <- 1 - fpr # Convert FPR to Specificity roc_obj$thresholds <- 1:length(tpr) # Use confidence interval indices as thresholds # Verify the object (optional) roc_obj
Step 3: Fit Curves & Calculate Metrics
Now you can use standard pROC functions to visualize the curve, fit a smoothed version, and compute AUC/partial AUC:
Plot the Empirical ROC Curve & Smoothed Fit
# Plot empirical points plot(roc_obj, main = "Empirical ROC Curve (Recognition Memory Task)", col = "#2c3e50", lwd = 2, print.auc = TRUE) # Add a smoothed fitted curve (optional, for better visualization) smooth_roc <- smooth(roc_obj) lines(smooth_roc, col = "#e74c3c", lwd = 2, lty = 2) # Add legend legend("bottomright", legend = c("Empirical Points", "Smoothed Fit"), col = c("#2c3e50", "#e74c3c"), lwd = 2, lty = c(1, 2))
Calculate Full & Partial AUC
# Full AUC full_auc <- auc(roc_obj) cat("Full AUC:", round(full_auc, 3), "\n") # Partial AUC (e.g., FPR range from 0 to 0.3) partial_auc <- auc(roc_obj, partial.auc = c(0, 0.3)) cat("Partial AUC (FPR 0-0.3):", round(partial_auc, 3), "\n")
Important Notes
- Order Matters: Ensure your FPR/TPR points are ordered by decreasing confidence threshold (i.e., FPR and TPR increase as you relax the confidence cutoff). Your provided data already follows this pattern, which is correct.
- Raw Data Option: If you have the total number of target words (positive samples) and lure words (negative samples), you can construct a more precise
rocobject by simulating raw predictor values based on your cumulative rates. For example, if you had 100 targets and 100 lures, you'd create a predictor vector where 45 targets have the highest confidence, 5 lures have the highest confidence, etc. This allows pROC to calculate confidence intervals via bootstrapping.
内容的提问来源于stack exchange,提问作者Jason Finley

