@@ -350,9 +350,9 @@ evaluate_multi_ <- function(df, pred_label_col, actual_val_col, pretty.name = FA
350350
351351# Generates Analytics View Summary Table for logistic/binomial regression. Handles Test Mode.
352352# ' @export
353- evaluate_binary_training_and_test <- function (df , actual_val , threshold = " f_score" , pretty.name = FALSE ){
353+ evaluate_binary_training_and_test <- function (df , actual_val , threshold = " f_score" , pretty.name = FALSE , report_metrics = FALSE ){
354354 actual_val_col <- col_name(substitute(actual_val )) # Get name of column as string.
355- training_ret <- df %> % glance_rowwise(model , binary_classification_threshold = threshold )
355+ training_ret <- df %> % glance_rowwise(model , binary_classification_threshold = threshold , report_metrics = report_metrics )
356356 ret <- training_ret
357357
358358 grouped_col <- colnames(df )[! colnames(df ) %in% c(" model" , " .test_index" , " source.data" )]
@@ -378,6 +378,31 @@ evaluate_binary_training_and_test <- function(df, actual_val, threshold = "f_sco
378378 dplyr :: select(auc = AUC , f_score , accuracy_rate ,
379379 misclassification_rate , precision , recall ,
380380 n , positives , negatives )
381+ # Match training-side report_metrics extras so bind_rows keeps real values
382+ # on the test row (#37256; same invariant as rf_evaluation_training_and_test).
383+ if (isTRUE(report_metrics )) {
384+ actual_raw <- test_pred_ret [[actual_val_col ]]
385+ actual_for_roc <- binary_label(actual_raw )
386+ pred_prob <- test_pred_ret [[" predicted_probability" ]]
387+ threshold_value <- if (is.numeric(threshold )) {
388+ threshold
389+ } else if (! is.null(eret $ threshold )) {
390+ eret $ threshold [[1 ]]
391+ } else {
392+ 0.5
393+ }
394+ predicted_positive <- pred_prob > = threshold_value
395+ valid <- ! (is.na(actual_for_roc ) | is.na(predicted_positive ) | is.na(pred_prob ))
396+ tn <- sum(! actual_for_roc [valid ] & ! predicted_positive [valid ], na.rm = TRUE )
397+ fp <- sum(! actual_for_roc [valid ] & predicted_positive [valid ], na.rm = TRUE )
398+ tp <- sum(actual_for_roc [valid ] & predicted_positive [valid ], na.rm = TRUE )
399+ fn <- sum(actual_for_roc [valid ] & ! predicted_positive [valid ], na.rm = TRUE )
400+ specificity <- if ((tn + fp ) > 0 ) tn / (tn + fp ) else NA_real_
401+ sensitivity <- if ((tp + fn ) > 0 ) tp / (tp + fn ) else NA_real_
402+ test_ret $ pr_auc <- aupr(pred_prob , actual_for_roc )
403+ test_ret $ balanced_accuracy <- if (is.na(specificity ) || is.na(sensitivity )) NA_real_ else (specificity + sensitivity ) / 2
404+ test_ret $ specificity <- specificity
405+ }
381406 test_ret $ is_test_data <- TRUE
382407 test_ret
383408 }, error = function (e ){
@@ -396,9 +421,18 @@ evaluate_binary_training_and_test <- function(df, actual_val, threshold = "f_sco
396421 }
397422
398423 # sort column order
399- ret <- ret %> % dplyr :: select(auc , f_score , accuracy_rate , misclassification_rate , precision ,
400- recall , p.value , positives , negatives , n , logLik , AIC , BIC , # The order of positives, negatives, n is made the same as random forest and decision tree.
401- deviance , null.deviance , df.null , df.residual , everything())
424+ if (isTRUE(report_metrics )) {
425+ ret <- ret %> % dplyr :: select(dplyr :: any_of(c(
426+ " auc" , " roc_auc" , " pr_auc" , " f_score" , " balanced_accuracy" , " accuracy_rate" ,
427+ " misclassification_rate" , " precision" , " recall" , " specificity" ,
428+ " p.value" , " positives" , " negatives" , " n" , " logLik" , " AIC" , " BIC" ,
429+ " deviance" , " null.deviance" , " df.null" , " df.residual" )),
430+ everything())
431+ } else {
432+ ret <- ret %> % dplyr :: select(auc , f_score , accuracy_rate , misclassification_rate , precision ,
433+ recall , p.value , positives , negatives , n , logLik , AIC , BIC , # The order of positives, negatives, n is made the same as random forest and decision tree.
434+ deviance , null.deviance , df.null , df.residual , everything())
435+ }
402436
403437 # Reorder columns. Bring group_by column first, and then is_test_data column, if it exists.
404438 if (! is.null(ret $ is_test_data )) {
@@ -421,7 +455,14 @@ evaluate_binary_training_and_test <- function(df, actual_val, threshold = "f_sco
421455 colnames(ret )[colnames(ret ) == " misclassification_rate" ] <- " Misclass. Rate"
422456 colnames(ret )[colnames(ret ) == " precision" ] <- " Precision"
423457 colnames(ret )[colnames(ret ) == " recall" ] <- " Recall"
424- colnames(ret )[colnames(ret ) == " auc" ] <- " AUC"
458+ if (isTRUE(report_metrics )) {
459+ colnames(ret )[colnames(ret ) == " auc" ] <- " ROC AUC"
460+ colnames(ret )[colnames(ret ) == " pr_auc" ] <- " PR AUC"
461+ colnames(ret )[colnames(ret ) == " balanced_accuracy" ] <- " Balanced Accuracy"
462+ colnames(ret )[colnames(ret ) == " specificity" ] <- " Specificity"
463+ } else {
464+ colnames(ret )[colnames(ret ) == " auc" ] <- " AUC"
465+ }
425466 colnames(ret )[colnames(ret ) == " n" ] <- " Rows"
426467 colnames(ret )[colnames(ret ) == " positives" ] <- " Rows (TRUE)"
427468 colnames(ret )[colnames(ret ) == " negatives" ] <- " Rows (FALSE)"
0 commit comments