ROC surface plot in R or Python

34 Views Asked by At

Let X,Y,Z be random variables from F1,F2,F3 cdf,respectively. I want to write the manuel code for the ROC surface plot. How can I do it? By using R studio or Python. Thank you for your helps.

x<-rW(100,7,2)
y<-rW(100,9,4)
z<-rW(100,18,8)
n <- 50
x <- seq(0, 1, length.out = n)
y <- seq(0, 1, length.out = n)
z <- outer(x, y, function(x, y) ifelse(x <= y, x, y))

# Multiplying z values to increase the surface volume
z <- z * 2  # For example, we multiply z values by 2

# Create a 3D surface graph
plot_ly(x = x, y = y, z = z, type = "surface") %>%
  layout(scene = list(
xaxis = list(title = "False Positive Rate (FPR)", range = c(0, 1)),
yaxis = list(title = "True Positive Rate (TPR)", range = c(0, 1)),
zaxis = list(title = "Threshold")
)) 

I tried this code. But, I don,t like it. I need to VUS value on plot and each axes must be between 0 and 1. Let see "Statistics in Medicine - 2004 - Nakas - Ordered multiple‐class ROC analysis with continuous measurements" article. I need to plot like in Figure 2 or something like that.

1

There are 1 best solutions below

1
refik On

To create a ROC surface similar to the one you described, you can use libraries such as Matplotlib and NumPy

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Declare X, Y, Z
np.random.seed(0)
x = np.random.normal(7, 2, 100)
y = np.random.normal(9, 4, 100)
z = np.random.normal(18, 8, 100)

# Define the number of thresholds
n = 50

# Create thresholds
thresholds = np.linspace(0, 1, n)

# Initialize an empty array to store the ROCs
roc_values = np.zeros((n, n))

# Calculate ROCs for each threshold combination
for i, threshold_x in enumerate(thresholds):
    for j, threshold_y in enumerate(thresholds):
        roc_values[i, j] = np.mean((x <= threshold_x) & (y <= threshold_y) & (z <= threshold_x))

# Create a grid for surface plot
X, Y = np.meshgrid(thresholds, thresholds)

# Plot the ROC surface
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, roc_values, cmap='viridis')

# Set labels and title
ax.set_xlabel('False Positive Rate (FPR)')
ax.set_ylabel('True Positive Rate (TPR)')
ax.set_zlabel('VUS Value')
ax.set_title('ROC Surface Plot')

# Set limits for axes
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_zlim(0, 1)

plt.show()

You can adjust the sample data generation and threshold parameters however you want.