How to directly plot ROC of h2o model object in R

后端 未结 4 1561
灰色年华
灰色年华 2021-01-06 00:33

My apologies if I\'m missing something obvious. I\'ve been thoroughly enjoying working with h2o in the last few days using R interface. I would like to evaluate my model, sa

4条回答
  •  伪装坚强ぢ
    2021-01-06 01:19

    Building off @Lauren's example, after you run model.performance you can extract all necessary information for ggplot from perf@metrics$thresholds_and_metric_scores. This code produces the ROC curve, but you can also add precision, recall to the selected variables for plotting the PR curve.

    Here is some example code using the same model as above.

    library(h2o)
    library(dplyr)
    library(ggplot2)
    
    h2o.init()
    
    # Run GLM of CAPSULE ~ AGE + RACE + PSA + DCAPS
    prostatePath <- system.file("extdata", "prostate.csv", package = "h2o")
    prostate.hex <- h2o.importFile(
        path = prostatePath, 
        destination_frame = "prostate.hex"
        )
    glm <- h2o.glm(
        y = "CAPSULE",
        x = c("AGE", "RACE", "PSA", "DCAPS"), 
        training_frame = prostate.hex, 
        family = "binomial", 
        nfolds = 0, 
        alpha = 0.5, 
        lambda_search = FALSE
    )
    
    # Model performance
    perf <- h2o.performance(glm, newdata = prostate.hex)
    
    # Extract info for ROC curve
    curve_dat <- data.frame(perf@metrics$thresholds_and_metric_scores) %>%
        select(c(tpr, fpr))
    
    # Plot ROC curve
    ggplot(curve_dat, aes(x = fpr, y = tpr)) +
        geom_point() +
        geom_line() +
        geom_segment(
            aes(x = 0, y = 0, xend = 1, yend = 1),
            linetype = "dotted",
            color = "grey50"
            ) +
        xlab("False Positive Rate") +
        ylab("True Positive Rate") +
        ggtitle("ROC Curve") +
        theme_bw()
    

    Which produces this plot:

    roc_plot

提交回复
热议问题