import numpy as np
import rpy2.robjects as robj
r = robj.r
from rpy2.robjects.packages import importr
def roc_test_r(targets_1, scores_1, targets_2, scores_2, method='delong'):
# method: “delong”, “bootstrap” or “venkatraman”
importr('pROC')
robj.globalenv['targets_1'] = targets_1 = robj.FloatVector(targets_1)
robj.globalenv['scores_1'] = scores_1 = robj.FloatVector(scores_1)
robj.globalenv['targets_2'] = targets_2 = robj.FloatVector(targets_2)
robj.globalenv['scores_2'] = scores_2 = robj.FloatVector(scores_2)
r('roc_1 <- roc(targets_1, scores_1)')
r('roc_2 <- roc(targets_2, scores_2)')
r('result = roc.test(roc_1, roc_2, method="%s")' % method)
p_value = r('p_value = result$p.value')
return np.array(p_value)[0]
网友评论