R/acc_plot.R

Defines functions acc_plot

Documented in acc_plot

#' Plot Model Accuracy
#'
#' only supports binary logistic regression at this time
#'
#' @param accuracy_data data generated by mcguiR::accuracy.log()
#' @param y desired value to be plotted - could be "correct", "incorrect", or "both"
#' @importFrom ggplot2 aes
#'
#' @return a plot of accuracy at each probability threshold
#' @export
#'
#' @examples
#' \dontrun{
#' iris2 <- iris[stringr::str_detect(Species, "setosa", negate = T), ]
#' irismodel <- glm(Species ~ ., data = iris2, family = binomial)
#' acc_data <- accuracy(irismodel, iris2, iris2$Species, "virginica", "versicolor")
#' acc_plot(acc_data, y = "both")}
acc_plot <- function(accuracy_data, y = "correct") {
        c <- ggplot2::ggplot(data = accuracy_data, ggplot2::aes(x = threshold)) +
            ggplot2::geom_line(ggplot2::aes(y = correct, color = "#66C2A5")) +
            ggplot2::theme(legend.position = "none") +
            ggplot2::xlab("Probability Threshold") +
            ggplot2::ylab("Percent Correct (%)") +
            ggplot2::labs(title = "Accuracy at Different Probability Thresholds")

        i <- ggplot2::ggplot(data = accuracy_data, ggplot2::aes(x = threshold)) +
            ggplot2::geom_line(aes(y = incorrect, color = "#D53E4F")) +
            ggplot2::theme(legend.position = "none") +
            ggplot2::xlab("Probability Threshold") +
            ggplot2::ylab("Percent Incorrect (%)") +
            ggplot2::labs(title = "Accuracy at Different Probability Thresholds")

        long <- tidyr::pivot_longer(accuracy_data, -threshold)

        b <- ggplot2::ggplot(data = long) +
            ggplot2::geom_line(ggplot2::aes(y = value, x = threshold,
                                            color = name)) +
            ggplot2::scale_color_manual(values = c("#66C2A5", "#D53E4F")) +
            ggplot2::theme(legend.title = ggplot2::element_blank()) +
            ggplot2::xlab("Probability Threshold") +
            ggplot2::ylab("Percent (%)") +
            ggplot2::labs(title = "Accuracy at Different Probability Thresholds")
    if(y == "correct") {
        return(c)
    }
    if(y == "incorrect") {
        return(i)
    }
    if(y == "both") {
        return(b)
    }
}
bmcguir8/mcguiR documentation built on Jan. 7, 2021, 8:40 p.m.