diff --git a/R/helpers.R b/R/helpers.R index 759daf6..01b8b82 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -1078,11 +1078,22 @@ create_reference_lines_data <- function(curve, prevalence, ) hover_text_perfect <- "Perfect Prediction ({reference_group})
Lift: {round(y, digits = 3)}
Predicted Positives: {100*x}%" } else { + + if((unique(unlist(prevalence))) == 0) { + + random_guess <- rep(NaN, 100) + + } else { + + random_guess <- rep(1, 100) + + } + reference_group <- rep(c("reference_line", "reference_line_perfect_model"), each = 100) reference_line_x_values <- rep(seq(0.01, 1, by = 0.01), times = 2) reference_line_y_values <- c( - rep(1, 100), + random_guess, return_perfect_prediction_lift_y_values(unique(unlist(prevalence))) ) @@ -1280,10 +1291,17 @@ return_perfect_prediction_gains_y_values <- function(prevalence) { return_perfect_prediction_lift_y_values <- function(prevalence) { - c( - rep( round(1 / prevalence, digits = 3), 100 * round(prevalence, digits = 3)), - seq( round(1 / prevalence, digits = 3), 1, length.out = (100 - 100 * (round(prevalence, digits = 3)))) - ) + if (prevalence == 0) { + + rep(NaN, 100) + + } else { + + c( + rep( round(1 / prevalence, digits = 3), 100 * round(prevalence, digits = 3)), + seq( round(1 / prevalence, digits = 3), 1, length.out = (100 - 100 * (round(prevalence, digits = 3)))) + ) + } } @@ -1292,9 +1310,14 @@ return_perfect_prediction_lift_y_values <- function(prevalence) { extract_axes_ranges <- function(performance_data_ready, curve, min_p_threshold, max_p_threshold) { - if (curve %in% c("lift", "decision", "interventions avoided")) { + if (curve %in% c("lift")) { max_y_range <- max(c(1, performance_data_ready$y), na.rm = TRUE) } + + if (curve %in% c("decision", "interventions avoided")) { + max_y_range <- max(performance_data_ready$y, na.rm = TRUE) + } + if (curve %in% c("decision", "interventions avoided")) { min_x_range <- min_p_threshold @@ -1343,10 +1366,10 @@ extract_axes_ranges <- function(performance_data_ready, curve, ) } - curve_axis_range |> purrr::map(~ extand_axis_range(.x)) + curve_axis_range |> purrr::map(~ extend_axis_ranges(.x)) } -extand_axis_range <- function(axis_range, extand_range_by = 1.1) { +extend_axis_ranges <- function(axis_range, extand_range_by = 1.1) { margin <- (extand_range_by - 1) * diff(axis_range) c(axis_range[1] - margin, axis_range[2] + margin) @@ -1367,9 +1390,10 @@ return_treat_none_y_values <- function(prevalence) { prepare_performance_data_for_interactive_marker <- function( performance_data_ready_for_curve, perf_dat_type) { + performance_data_for_interactive_marker <- performance_data_ready_for_curve - performance_data_for_interactive_marker$y[is.nan(performance_data_for_interactive_marker$y)] <- -1 + performance_data_for_interactive_marker$y[is.nan(performance_data_for_interactive_marker$y)] <- -100 performance_data_for_interactive_marker$x[is.nan(performance_data_for_interactive_marker$x)] <- -1 if ( perf_dat_type %in% c("several models", "several populations") ) { diff --git a/inst/rest_api/main.R b/inst/rest_api/main.R deleted file mode 100644 index 967e904..0000000 --- a/inst/rest_api/main.R +++ /dev/null @@ -1,5 +0,0 @@ -library(plumber) - -x <- plumb("C:/Users/CRI_user/Documents/rtichoke/inst/rest_api/plumber.R") -x$run(debug = TRUE, host = "0.0.0.0", port = 4242) - diff --git a/inst/rest_api/plumber.R b/inst/rest_api/plumber.R deleted file mode 100644 index f57f5f2..0000000 --- a/inst/rest_api/plumber.R +++ /dev/null @@ -1,207 +0,0 @@ -library(plumber) -library(rtichoke) -library(htmlwidgets) -library(xml2) -library(dplyr) - - -#* @serializer html -#* @post /create_summary_report -function(req, res) { - - print(req$body) - - print("probs") - print(typeof(req$body$probs)) - print(str(req$body$probs)) - print(is.list(req$body$probs)) - - print("reals") - print(typeof(req$body$reals)) - print(str(req$body$reals)) - print(is.list(req$body$reals)) - - create_summary_report( - probs = req$body$probs, - reals = req$body$reals - ) - as.character(xml2::read_html("summary_report.html")) - -} - -#* @post /create_roc_curve_list -#* @serializer json -function(req, res){ - - - # print(typeof(req$body$probs)) - # print(str(req$body$probs)) - # print(is.list(req$body$probs)) - print("probs") - - print(req$body$probs) - - print("reals") - print(req$body$reals) - # print(typeof(req$body$reals)) - # print(str(req$body$reals)) - # print(is.list(req$body$reals)) - # - print( - prepare_performance_data( - probs = req$body$probs, - reals = req$body$reals, - stratified_by = req$body$stratified_by - ) - ) - - prepare_performance_data( - probs = req$body$probs, - reals = req$body$reals, - stratified_by = req$body$stratified_by - ) |> - rtichoke:::create_rtichoke_curve_list("roc") - -} - -#* @post /create_lift_curve_list -#* @serializer json -function(req, res){ - - - # print(typeof(req$body$probs)) - # print(str(req$body$probs)) - # print(is.list(req$body$probs)) - print("probs") - - print(req$body$probs) - - print("reals") - print(req$body$reals) - # print(typeof(req$body$reals)) - # print(str(req$body$reals)) - # print(is.list(req$body$reals)) - # - print( - prepare_performance_data( - probs = req$body$probs, - reals = req$body$reals, - stratified_by = req$body$stratified_by - ) - ) - - prepare_performance_data( - probs = req$body$probs, - reals = req$body$reals, - stratified_by = req$body$stratified_by - ) |> - rtichoke:::create_rtichoke_curve_list("lift") - -} - - -#* @post /create_precision_recall_curve_list -#* @serializer json -function(req, res){ - - - # print(typeof(req$body$probs)) - # print(str(req$body$probs)) - # print(is.list(req$body$probs)) - print("probs") - - print(req$body$probs) - - print("reals") - print(req$body$reals) - # print(typeof(req$body$reals)) - # print(str(req$body$reals)) - # print(is.list(req$body$reals)) - # - print( - prepare_performance_data( - probs = req$body$probs, - reals = req$body$reals, - stratified_by = req$body$stratified_by - ) - ) - - prepare_performance_data( - probs = req$body$probs, - reals = req$body$reals, - stratified_by = req$body$stratified_by - ) |> - rtichoke:::create_rtichoke_curve_list("precision recall") - -} - -#* @post /create_gains_curve_list -#* @serializer json -function(req, res){ - - - # print(typeof(req$body$probs)) - # print(str(req$body$probs)) - # print(is.list(req$body$probs)) - print("probs") - - print(req$body$probs) - - print("reals") - print(req$body$reals) - # print(typeof(req$body$reals)) - # print(str(req$body$reals)) - # print(is.list(req$body$reals)) - # - print( - prepare_performance_data( - probs = req$body$probs, - reals = req$body$reals, - stratified_by = req$body$stratified_by - ) - ) - - prepare_performance_data( - probs = req$body$probs, - reals = req$body$reals, - stratified_by = req$body$stratified_by - ) |> - rtichoke:::create_rtichoke_curve_list("gains") - -} - - - -#* @post /prepare_performance_data -#* @serializer json -function(req, res){ - - - # print(typeof(req$body$probs)) - # print(str(req$body$probs)) - # print(is.list(req$body$probs)) - print("probs") - - print(req$body$probs) - - print("reals") - print(req$body$reals) - # print(typeof(req$body$reals)) - # print(str(req$body$reals)) - # print(is.list(req$body$reals)) - # - print( - prepare_performance_data( - probs = req$body$probs, - reals = req$body$reals, - stratified_by = req$body$stratified_by - ) - ) - - prepare_performance_data( - probs = req$body$probs, - reals = req$body$reals, - stratified_by = req$body$stratified_by - ) - -} \ No newline at end of file diff --git a/inst/rest_api/post_request_example.R b/inst/rest_api/post_request_example.R deleted file mode 100644 index f804e15..0000000 --- a/inst/rest_api/post_request_example.R +++ /dev/null @@ -1,148 +0,0 @@ -library(httr) -library(rtichoke) - -?create_roc_curve - -r <- POST("http://127.0.0.1:7644/create_summary_report", - body = jsonlite::toJSON( - list( - probs = list("First Model" = example_dat$estimated_probabilities), - reals = list("First Model" = example_dat$outcome)), - auto_unbox = TRUE), - encode = "json") - - - - -r <- POST("http://127.0.0.1:7644/create_summary_report", - body = jsonlite::toJSON( - list( - probs = list( - "First Model" = example_dat$estimated_probabilities, - "Second Model" = example_dat$random_guess - ), - reals = list(example_dat$outcome)), - auto_unbox = TRUE), - encode = "json") - - -r <- POST("http://127.0.0.1:7644/create_summary_report", - body = jsonlite::toJSON( - list( - probs = list( - "train" = example_dat %>% - dplyr::filter(type_of_set == "train") %>% - dplyr::pull(estimated_probabilities), - "test" = example_dat %>% dplyr::filter(type_of_set == "test") %>% - dplyr::pull(estimated_probabilities) - ), - reals = list( - "train" = example_dat %>% dplyr::filter(type_of_set == "train") %>% - dplyr::pull(outcome), - "test" = example_dat %>% dplyr::filter(type_of_set == "test") %>% - dplyr::pull(outcome) - )), - auto_unbox = TRUE), - encode = "json") - - -r <- POST("http://127.0.0.1:7644/roc_curve_list") |> - content() - -r - -r <- POST("http://127.0.0.1:6706/roc_curve_list", - body = jsonlite::toJSON( - list( - probs = list( - "First Model" = example_dat$estimated_probabilities, - "Second Model" = example_dat$bad_model), - reals = list( - example_dat$outcome))), encode = "json") |> - content() - -install.packages("tidyverse") -library(tidyverse) - -# Performance Data - -r |> - tibble() |> - unnest_wider(r) - -# rtichoke curve list - -r |> - tibble() |> - View() - -r$perf_dat_type -r$animation_slider_prefix - -library(purrr) - -r$animation_slider_prefix - -r$group_colors_vec - - reference_data |> - purrr::map_df(~.x[c("reference_group", "x", "y", "text")]) - -r$performance_data_ready_for_curve |> - purrr::map_df(~.x[c("reference_group", "x", "y", "text")]) - - - -r2 <- POST("http://127.0.0.1:7644/roc_curve_list2", - body = jsonlite::toJSON( - list( - probs = list("First Model" = example_dat$estimated_probabilities), - reals = list("First Model" = example_dat$outcome)), - auto_unbox = TRUE), - encode = "json") |> - content() - - - -r$performance_data_ready_for_curve -r2$performance_data_ready_for_curve - - - -r$reference_data |> - as.data.frame() - -r$reference_data |> - rtichoke:::create_plotly_curve() - -r$cookies - -names(r) - - -r$ - - - -json_rtichoke_list <- jsonlite::toJSON( - list( - probs = list("First Model" = example_dat$estimated_probabilities), - reals = list("First Model" = example_dat$outcome)), - auto_unbox = TRUE) |> - jsonlite::fromJSON() - - -prepare_performance_data( - probs = as.list(json_rtichoke_list$probs), - reals = as.list(json_rtichoke_list$reals) -) |> - rtichoke:::create_rtichoke_curve_list("roc") |> - jsonlite::toJSON() - - - -r$request$output - - -r$request$output -names(r)