## ----setup, include = FALSE----------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## Initialize some stuff:
library("riskyr")  # loads the package
op <- par(no.readonly = TRUE)

## ----load_riskyr, message = FALSE----------------------------------------
library("riskyr")  # loads the package

## ----create_scenario_minimal_prob----------------------------------------
# Create a minimal scenario (from probabilities):
my_scenario <- riskyr(prev = .45, 
                      sens = .98,
                      spec = .46)

## ----create_scenario_min_freq--------------------------------------------
# Create a minimal scenario (from frequencies):
my_scenario_2 <- riskyr(hi = my_scenario$hi, 
                        mi = my_scenario$mi,
                        fa = my_scenario$fa,
                        cr = my_scenario$cr)

## ----verify_equal_prob_freq----------------------------------------------
all.equal(my_scenario, my_scenario_2)

## ----create_scenario_custom----------------------------------------------
# Create a scenario with custom labels: 
my_scenario <- riskyr(scen_lbl = "Identifying reoffenders", 
                      popu_lbl = "prison inmates", 
                      cond_lbl = "reoffending",
                      cond_true_lbl = "reoffends", cond_false_lbl = "does not reoffend",
                      dec_lbl = "test result",
                      dec_pos_lbl = "predict to\nreoffend", dec_neg_lbl = "predict to\nnot reoffend",
                      hi_lbl = "reoffender found", mi_lbl = "reoffender missed",
                      fa_lbl = "false accusation", cr_lbl = "correct release",
                      prev = .45,  # prevalence of being a reoffender. 
                      sens = .98,  # p( will reoffend | offends again )
                      spec = .46,  # p( will not reoffend | does not offend again )
                      fart =  NA,  # p( will reoffend | does not offend gain )
                      N = 753,     # population size
                      scen_src = "(a ficticious example)")

## ----plot_default, include = TRUE, fig.align = "center", fig.width = 7.2, fig.height = 6.0----
plot(my_scenario)

## ----plot_area_sq, eval = FALSE, fig.align = "center", fig.width = 7.0, fig.height = 5.8----
#  plot(my_scenario, area = "sq", f_lbl = "nam", p_lbl = "mix")  # show frequency names
#  plot(my_scenario, area = "hr", f_lbl = "num", p_lbl = "num")  # only numeric labels

## ----prism_practice, echo = FALSE, eval = FALSE, fig.align = "center"----
#  plot(my_scenario, area = "hr")
#  plot(my_scenario, area = "no", by = "cdac")
#  plot(my_scenario, area = "hr", by = "acdc", f_lbl = "nam", p_lbl = "num", f_lwd = .5, col_pal = pal_bw)

## ----icons, fig.align = "center", fig.width = 7.2, fig.height = 5.2------
plot(my_scenario, type = "icons")

## ----full_summary--------------------------------------------------------
summary(my_scenario)

## ----summary_prob, include = FALSE---------------------------------------
summary(my_scenario, summarize = "prob")

## ----tree, fig.align = "center", fig.width = 7.2, fig.height = 5.0-------
plot(my_scenario, type = "tree", by = "dc")  # plot tree diagram (splitting N by decision)

## ----plotting_curve, fig.align = "center", fig.width = 7.0, fig.height = 5.6----
plot(my_scenario, type = "curve", uc = .05)

## ----scenario_table, echo = FALSE, results = "asis"----------------------
library(knitr)
scen_table <- df_scenarios[,
                           c("scen_lbl", "cond_lbl", "N", "prev",
                             "sens", "spec", "fart")]
scen_table[, -c(1:2)] <- round(scen_table[, -c(1:2)], 3)
names(scen_table) <- c("Scenario", "Condition", "N", "prev", "sens", "spec", "fart")
knitr::kable(scen_table)

## ----s10_select----------------------------------------------------------
s10 <- scenarios$n10  # assign pre-defined Scenario 10 to s10.

## ----s10_info------------------------------------------------------------
# Show basic scenario information: 
s10$scen_lbl  # shows descriptive label:
s10$cond_lbl  # shows current condition:
s10$dec_lbl   # shows current decision:
s10$popu_lbl  # shows current population:
s10$scen_apa  # shows current source: 

## ----s10_summary---------------------------------------------------------
summary(s10) # summarizes key scenario information:

## ----s10_icons, eval = FALSE, fig.width = 7.2, fig.height = 4.8----------
#  plot(s10, type = "tab")                   # plot 2x2 table
#  plot(s10, type = "icons", cex_lbl = .75)  # plot an icon array
#  plot(s10, type = "prism", area = "sq")    # plot a network/prism diagram
#  plot(s10, type = "area")                  # plot an area plot
#  plot(s10, type = "bar", dir = 2)          # plot a bar chart

## ----s10_prism_1, fig.align = "center", fig.width = 7.2, fig.height = 5.8----
plot(s10, 
     by = "cddc",      # perspective: upper half by condition, lower half by decision 
     area = "hr",      # frequency boxes as horizontal rectangles (scaled to N)
     p_lbl = "num")    # probability labels: numeric only

## ----s10_prism_2, eval = FALSE-------------------------------------------
#  plot(s10, by = "cdac", area = "sq")
#  plot(s10, by = "ac", area = "hr")

## ----s10_curve, eval = FALSE, fig.align = "center", fig.width = 7.2, fig.height = 6.0----
#  plot(s10, type = "curve",
#       what = "all",  # plot "all" available curves
#       uc = .05)      # with a 5%-uncertainty range

## ----opar_set, echo = FALSE----------------------------------------------
# opar <- par(no.readonly = TRUE)  # save plot settings.
# par(mfrow = c(1, 2))           # 1 row with 2 plots:

## ----s10_planes, fig.align = "center", fig.show = "hold", fig.width = 5.8, fig.height = 4.0----
## Plot plane of PPV and NPV as functions of sens and spec (for given prev): 
plot(s10, type = "plane", what = "PPV", cex_lbl = .7)  # PPV by sens x spec (fixed prev)
plot(s10, type = "plane", what = "NPV", cex_lbl = .7)  # NPV by sens x spec (fixed prev)

## ----opar_reset, echo = FALSE--------------------------------------------
# par(op)  # reset plot settings.

## ----s9_summary----------------------------------------------------------
# Select Scenario 9: 
s9 <- scenarios$n9  # assign pre-defined Scenario 9 to s9. 

# Basic scenario information: 
s9$scen_lbl  # shows descriptive label:
s9$popu_lbl  # shows current population:

