如果你在mlr3拿到機器學習的預測數據
ROC 過程原理探索
假設數據
df <- data.frame(A=iris$Sepal.Length,
group=sample(x = c(0,1),size = 150,replace = T))
分組為 0,1 # 變量A為連續性變量
library(pROC)
roc_obj <- roc(df g r o u p , d f group, df group,dfA, levels = c(0, 1),ci=T)
auc(roc_obj)
ci.auc(roc_obj)
如果你直接在機器學習拿到預測數據
fit <- lm(df g r o u p d f group~df group?dfA)
pre_df <- predict(fit,df)
roc_obj_2 <- roc(df$group, pre_df, levels = c(0, 1),ci=T)
auc(roc_obj_2)
實測demo
# 在train的性能
prediction_train_rf = learner_rf$predict(task, row_ids = train_id)
prediction_train_rf$confusion
prediction_train_rf$score(msr("classif.auc"))
p_rf_train=autoplot(prediction_train_rf,type = "roc")
p_rf_train# 在test的性能
prediction_test_rf = learner_rf$predict(task, row_ids = test_id)
prediction_test_rf$confusion
prediction_test_rf$score(msr("classif.auc"))
p_rf_test=autoplot(prediction_test_rf,type = "roc")
p_rf_test# train的AUC和CI,多ROC線準備
library(pROC)
# train的AUC和CI,多ROC線準備
roc_obj_rf_train <- roc(prediction_train_rf$truth, prediction_train_rf$prob[,1], ci=T)
auc(roc_obj_rf_train);ci.auc(roc_obj_rf_train)# 【與上面不一致】 不需要判斷 prob 選哪個
# roc_obj_rf_train <- roc(prediction_test_rf$truth,
# ifelse(
# prediction_test_rf$truth==1,
# prediction_test_rf$prob[,1],
# prediction_test_rf$prob[,2]
# ), ci=T)
# auc(roc_obj_rf_train);ci.auc(roc_obj_rf_train)
# test的AUC和CI,多ROC線準備
roc_obj_rf_test <- roc(prediction_test_rf$truth, prediction_test_rf$prob[,1], ci=T)
auc(roc_obj_rf_test);ci.auc(roc_obj_rf_test)
#
pROC::plot.roc(roc_obj_rf_test)