## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = FALSE, comment = "",
                      fig.width = 7, fig.height = 4.5, dpi = 96,
                      dev.args = list(bg = "transparent"))
# Console colour carries no meaning on a rendered page. pkgdown turns it on for
# its own build, and the escape sequences then reach the reader as literal text,
# so colour is switched off here for a plain vignette render and a site build
# alike. The fixed width keeps printed output inside the documentation column.
options(cli.num_colors = 1, cli.hyperlink = FALSE, crayon.enabled = FALSE,
        width = 80)

# Figures on the package website sit on a warm off-white page in light mode and
# are inverted by pkgdown in dark mode, so an opaque background would read as a
# pale slab one way and a black plate the other. Two things paint one. The
# device canvas is made transparent by `dev.args` above, and theme_depictr()
# then inherits theme_minimal()'s white plot.background, which is drawn over
# that canvas, so it is cleared as each figure is printed. This is deliberately
# a vignette-level choice: theme_depictr() keeps its opaque background, which is
# what a figure saved for a paper wants.
transparent_bg <- ggplot2::theme(
  plot.background  = ggplot2::element_rect(fill = NA, colour = NA),
  panel.background = ggplot2::element_rect(fill = NA, colour = NA)
)
knit_print.ggplot <- function(x, ...) knitr::normal_print(x + transparent_bg)
knit_print.patchwork <- function(x, ...) knitr::normal_print(x & transparent_bg)

library(depictr)
has_simr <- requireNamespace("simr", quietly = TRUE)

## -----------------------------------------------------------------------------
fit <- lm(yield ~ rainfall + fertiliser + soil_ph, data = crop_yield)

## ----fig.height = 6-----------------------------------------------------------
residual_diagnostics_plot(fit, title = "Crop-yield model")

## -----------------------------------------------------------------------------
influence_plot(fit)

## ----fig.height = 3.2---------------------------------------------------------
set.seed(1)
collinear <- crop_yield
collinear$soil_moisture <- 0.05 * collinear$rainfall +
  rnorm(nrow(collinear), sd = 2)
vif_plot(lm(yield ~ rainfall + soil_moisture + fertiliser, data = collinear))

## -----------------------------------------------------------------------------
gfit <- glm(adverse_event ~ biomarker + age + arm,
            data = clinical_trial, family = binomial)

## -----------------------------------------------------------------------------
binned_residual_plot(gfit, title = "Binned residuals: adverse-event model")

## ----fig.height = 6-----------------------------------------------------------
residual_diagnostics_plot(gfit, title = "Adverse-event model")

## ----fig.width = 5, fig.height = 5--------------------------------------------
roc_curve_plot(gfit, youden = TRUE)

## ----fig.width = 5, fig.height = 5--------------------------------------------
pr_curve_plot(gfit, f1 = TRUE)

## -----------------------------------------------------------------------------
reduced <- glm(adverse_event ~ biomarker, data = clinical_trial,
               family = binomial)
models <- list(Full = gfit, `Biomarker only` = reduced)

## ----fig.width = 5, fig.height = 5--------------------------------------------
roc_curve_plot(models, youden = TRUE, legend_inside = TRUE)

## ----fig.width = 5, fig.height = 5--------------------------------------------
pr_curve_plot(models, f1 = TRUE)

## ----fig.width = 5, fig.height = 5--------------------------------------------
gain_plot(models, legend_inside = TRUE)

## ----fig.height = 3.6---------------------------------------------------------
lift_plot(models, legend_inside = TRUE)

## -----------------------------------------------------------------------------
tp <- threshold_plot(gfit, title = "Metrics across the decision threshold")
tp
attr(tp, "thresholds")   # the Youden and max-F1 thresholds

## ----fig.width = 5, fig.height = 5--------------------------------------------
calibration_plot(gfit, bins = 6)

## ----fig.width = 5, fig.height = 4.5------------------------------------------
confusion_matrix_plot(gfit, threshold = "youden", normalise = "row")

## -----------------------------------------------------------------------------
draws <- readRDS(
  system.file("extdata", "lexdec_draws.rds", package = "depictr")
)
posterior_plot(draws[c("conditionunrelated", "modalityauditory",
                       "word_frequency")],
               labels = c(conditionunrelated = "condition",
                          modalityauditory = "modality",
                          word_frequency = "word frequency"),
               style = "interval", title = "Posterior estimates (ms)")

## ----eval = has_simr----------------------------------------------------------
pc <- readRDS(
  system.file("extdata", "powercurve_lexdec.rds", package = "depictr")
)
power_curve_plot(pc, x_lab = "Number of participants",
                 title = "Power for the word-frequency effect")

## ----eval = !has_simr, echo = !has_simr---------------------------------------
# # Summarising a powerCurve object needs the 'simr' package. Without it we read
# # the same five points from the summary stored alongside the object, as a tidy
# # data frame, which power_curve_plot() also accepts.
# pc_df <- readRDS(system.file("extdata", "powercurve_lexdec_summary.rds",
#                              package = "depictr"))

## ----eval = !has_simr, echo = !has_simr, fig.height = 4.5---------------------
# power_curve_plot(pc_df, x_lab = "Number of participants",
#                  title = "Power for the word-frequency effect")

## ----fig.height = 3.2---------------------------------------------------------
panel <- arrange_plots(
  qq_plot(fit), influence_plot(fit),
  ncol = 2, title = "Diagnostics", tag_levels = "A"
)
panel

## ----eval = FALSE-------------------------------------------------------------
# save_plot("figures/diagnostics.png", panel, width = 7, height = 3.2, dpi = 300)

