Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #719 - refactor PD_Assess() #725

Merged
merged 3 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions R/AE_Assess.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
#' @return `list` `lData`, a named list with:
#' - each data frame in the data pipeline
#' - `dfTransformed`, returned by [gsm::Transform_EventCount()]
#' - `dfAnalyzed`, returned by [gsm::Analyze_Poisson()] or [gsm::Analyze_Wilcoxon()]
#' - `dfAnalyzed`, returned by [gsm::Analyze_Poisson()], [gsm::Analyze_Wilcoxon()], or [gsm::Analyze_Identity()]
#' - `dfFlagged`, returned by [gsm::Flag()]
#' - `dfSummary`, returned by [gsm::Summarize()]
#' - `dfBounds`, returned by [gsm::Analyze_Poisson_PredictBounds()] when strMethod == 'poisson'
#' - `dfBounds`, returned by [gsm::Analyze_Poisson_PredictBounds()] only when strMethod == 'poisson'
#' - `list` `lCharts`, a named list with:
#' - `scatter`, a ggplot2 object returned by [gsm::Visualize_Scatter()]
#' - `barMetric`, a ggplot2 object returned by [gsm::Visualize_Score()] using strType == "metric"
Expand All @@ -48,7 +48,6 @@
#' ae_assessment_wilcoxon <- AE_Assess(dfInput, strMethod = "wilcoxon")
#'
#' @importFrom cli cli_alert_success cli_alert_warning cli_h2 cli_text
#' @importFrom purrr map map_dbl
#' @importFrom yaml read_yaml
#' @importFrom glue glue
#'
Expand All @@ -60,14 +59,15 @@ AE_Assess <- function(dfInput,
lMapping = yaml::read_yaml(system.file("mappings", "AE_Assess.yaml", package = "gsm")),
strGroup = "Site",
bQuiet = TRUE) {

# data checking -----------------------------------------------------------
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this additional structure with the comments

stopifnot(
"strMethod is not 'poisson', 'wilcoxon', or 'identity'" = strMethod %in% c("poisson", "wilcoxon", "identity"),
"strMethod must be length 1" = length(strMethod) == 1,
"strGroup must be one of: Site, Study, or CustomGroup" = strGroup %in% c("Site", "Study", "CustomGroup"),
"bQuiet must be logical" = is.logical(bQuiet)
)


lMapping$dfInput$strGroupCol <- lMapping$dfInput[[glue::glue("str{strGroup}Col")]]

lChecks <- CheckInputs(
Expand All @@ -78,7 +78,7 @@ AE_Assess <- function(dfInput,
)


# set vThreshold if NULL ------------------------------------------------
# set thresholds and flagging parameters ----------------------------------
if (is.null(vThreshold)) {
vThreshold <- switch(
strMethod,
Expand All @@ -88,6 +88,15 @@ AE_Assess <- function(dfInput,
)
}

strValueColumnVal <- switch(
strMethod,
poisson = NULL,
wilcoxon = "Estimate",
identity = "Score"
)


# begin running assessment ------------------------------------------------
if (!lChecks$status) {
if (!bQuiet) cli::cli_alert_warning("{.fn AE_Assess} did not run because of failed check.")
return(list(
Expand All @@ -98,10 +107,8 @@ AE_Assess <- function(dfInput,
}else{
if (!bQuiet) cli::cli_h2("Initializing {.fn AE_Assess}")

########################################
## Save Data pipeline results to lData
########################################

# dfTransformed -----------------------------------------------------------
if (!bQuiet) cli::cli_text("Input data has {nrow(dfInput)} rows.")
lData <- list()
lData$dfTransformed <- gsm::Transform_Rate(
Expand All @@ -113,7 +120,8 @@ AE_Assess <- function(dfInput,
if (!bQuiet) cli::cli_alert_success("{.fn Transform_Rate} returned output with {nrow(lData$dfTransformed)} rows.")


# refactor to only analyze in elseif --------------------------------------

# dfAnalyzed --------------------------------------------------------------
if (strMethod == "poisson") {
lData$dfAnalyzed <- gsm::Analyze_Poisson(lData$dfTransformed, bQuiet = bQuiet)
if (!bQuiet) cli::cli_alert_success("{.fn Analyze_Poisson} returned output with {nrow(lData$dfAnalyzed)} rows.")
Expand All @@ -127,26 +135,18 @@ AE_Assess <- function(dfInput,
}


# only parameter changed for Flag is strValueCol --------------------------
strValueColumnVal <- switch(
strMethod,
poisson = NULL,
wilcoxon = "Estimate",
identity = "Score"
)

# dfFlagged ---------------------------------------------------------------
lData$dfFlagged <- gsm::Flag(lData$dfAnalyzed, vThreshold = vThreshold, strValueColumn = strValueColumnVal)
if (!bQuiet) cli::cli_alert_success("{.fn Flag} returned output with {nrow(lData$dfFlagged)} rows.")


# dfSummary ---------------------------------------------------------------
lData$dfSummary <- gsm::Summarize(lData$dfFlagged)
if (!bQuiet) cli::cli_alert_success("{.fn Summarize} returned output with {nrow(lData$dfSummary)} rows.")

########################################
## Save Charts to lCharts
##
## - strGroupLabel defaults to strGroup for now
## - may want to move this out to be more flexible
########################################

# visualizations ----------------------------------------------------------
lCharts <- list()

if(!hasName(lData, 'dfBounds')) lData$dfBounds <- NULL
Expand All @@ -160,6 +160,8 @@ AE_Assess <- function(dfInput,
lCharts$barScore <- Visualize_Score(dfFlagged = lData$dfFlagged, strType = "score", vThreshold = vThreshold)
if (!bQuiet) cli::cli_alert_success("{.fn Visualize_Score} created a chart.")


# return data -------------------------------------------------------------
return(list(
lData = lData,
lCharts = lCharts,
Expand Down
Loading