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

## ----sim----------------------------------------------------------------------
# library(ixsurface)
# 
# # Mixed: 2 continuous + 1 categorical factor
# dat = sim_factorial(n = 300, design = "mixed", seed = 42)
# str(dat)
# 
# # Continuous: 3 continuous factors
# dat_cont = sim_factorial(n = 300, design = "continuous", seed = 42)
# 
# # Categorical: 3 categorical factors
# dat_cat = sim_factorial(n = 300, design = "categorical", seed = 42)

## ----surface-basic------------------------------------------------------------
# fit = lm(y ~ temp * pressure * catalyst, data = dat)
# 
# # One surface per catalyst level
# interaction_surface(fit, x = "temp", y = "pressure", facet_by = "catalyst")

## ----surface-full-------------------------------------------------------------
# interaction_surface(
#   fit, x = "temp", y = "pressure", facet_by = "catalyst",
#   show_points = TRUE, show_crossings = TRUE, show_contour = TRUE,
#   alpha = 0.5,
#   labs = list(x = "Temperature (C)", y = "Pressure (psi)", z = "Yield"),
#   title = "Mixed Design: temp x pressure | catalyst"
# )

## ----surface-single-----------------------------------------------------------
# interaction_surface(fit, x = "temp", y = "pressure",
#                     show_points = TRUE, alpha = 0.7)

## ----glm----------------------------------------------------------------------
# dat$success = rbinom(nrow(dat), 1, plogis((dat$y - 50) / 5))
# gfit = glm(success ~ temp * pressure * catalyst, data = dat, family = binomial)
# 
# interaction_surface(gfit, x = "temp", y = "pressure", facet_by = "catalyst",
#                     labs = list(z = "P(success)"))

## ----continuous-facet---------------------------------------------------------
# fit_cont = lm(y ~ temp * pressure * speed, data = dat_cont)
# 
# interaction_surface(fit_cont, x = "temp", y = "pressure", facet_by = "speed",
#                     n_bins = 4, bin_method = "quantile",
#                     show_crossings = TRUE)

## ----find-crossings-----------------------------------------------------------
# cx = find_crossings(fit, "temp", "pressure", "catalyst")
# head(cx)
# #>         cx       cy       cz               pair_label
# #> 1  150.000 10.81633 44.66789 catalyst=A vs catalyst=B
# #> 2  152.041 10.81633 44.67123 catalyst=A vs catalyst=B
# #> ...
# 
# table(cx$pair_label)

## ----plot-crossings-----------------------------------------------------------
# plot_crossings(fit, "temp", "pressure", "catalyst",
#                labs = list(x = "Temp (C)", y = "Press (psi)", z = "Yield"),
#                marker_size = 4, marker_opacity = 0.8)

## ----grid---------------------------------------------------------------------
# plots = interaction_surface_grid(fit, n = 20)
# names(plots)
# #> [1] "temp__pressure"    "temp__catalyst"    "pressure__catalyst"
# 
# # View individual plots
# plots$temp__pressure

## ----metadata-----------------------------------------------------------------
# p = interaction_surface(fit, x = "temp", y = "pressure",
#                         facet_by = "catalyst", n = 20)
# meta = attr(p, "ixsurface_meta")
# meta$n_surfaces
# #> [1] 3
# meta$surface_labels
# #> [1] "catalyst=A" "catalyst=B" "catalyst=C"

