## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.align = "center",
  fig.width = 6,
  fig.height = 4
)

## ----install, eval = FALSE----------------------------------------------------
# # install.packages("pak")
# pak::pkg_install("PPaccioretti/ofemeantest")

## ----load---------------------------------------------------------------------
library(ofemeantest)
data("ofe_f2")

head(ofe_f2)

## ----quick, message = FALSE, warning = FALSE----------------------------------
res <- ofemt(
  data = ofe_f2,
  y = "Yield_tn",
  x = "Treatment",
  cellsize = 9,
  min_per_cell = 4,
  n_p = 2000,
  n_s = 200,
  alpha = 0.05
)
res

## ----stepwise, message = FALSE, warning = FALSE-------------------------------
g <- make_ofe_grid(
  data = ofe_f2,
  x = "Treatment",
  cellsize = 9,
  min_per_cell = 4
)
names(g)


plot_grid_selection(g, data = ofe_f2)

res2 <- ofemt(
  data = ofe_f2,
  y = "Yield_tn",
  x = "Treatment",
  grid = g,
  n_p = 2000,
  n_s = 200
)

## ----engine, eval = FALSE-----------------------------------------------------
# plot_grid_selection(g, data = ofe_f2) +
#   ggplot2::labs(subtitle = "Lote 2, campaña 21/22")
# 
# plot_grid_selection(g, data = ofe_f2, engine = "base")

## ----tuning, eval = FALSE-----------------------------------------------------
# op <- par(mfrow = c(1, 2))
# plot_grid_selection(
#   make_ofe_grid(ofe_f2, x = "Treatment", cellsize = 9, min_per_cell = 4),
#   data = ofe_f2
# )
# # Shift the origin by half a cell and rotate to follow the strips
# plot_grid_selection(
#   make_ofe_grid(
#     ofe_f2,
#     x = "Treatment",
#     cellsize = 9,
#     min_per_cell = 4,
#     shift = c(4.5, 4.5),
#     angle_deg = 10,
#     buffer = 5
#   ),
#   data = ofe_f2
# )
# par(op)

## ----keep, eval = FALSE-------------------------------------------------------
# res_full <- ofemt(
#   ofe_f2,
#   y = "Yield_tn",
#   x = "Treatment",
#   cellsize = 9,
#   min_per_cell = 4,
#   keep_components = "full"
# )
# 
# # No further arguments needed — every layer comes from the object itself
# plot(res_full)
# 
# # The per-cell medians and residuals that fed the spatial diagnostics
# head(res_full$cell_medians)

## ----seed, eval = FALSE-------------------------------------------------------
# identical(
#   ofemt(
#     ofe_f2,
#     y = "Yield_tn",
#     x = "Treatment",
#     cellsize = 9,
#     min_per_cell = 4,
#     seed = 7L
#   ),
#   ofemt(
#     ofe_f2,
#     y = "Yield_tn",
#     x = "Treatment",
#     cellsize = 9,
#     min_per_cell = 4,
#     seed = 7L
#   )
# )
# #> TRUE

## ----hist, eval = FALSE-------------------------------------------------------
# # Requires the optional 'ggplot2' package.
# plot_pvalue_hist(res)

## ----hist_adj, eval = FALSE---------------------------------------------------
# res_bonf <- ofemt(
#   ofe_f2,
#   y = "Yield_tn",
#   x = "Treatment",
#   cellsize = 9,
#   min_per_cell = 4,
#   p_adjust_method = "bonferroni"
# )
# 
# plot_pvalue_hist(res_bonf) # adjusted
# plot_pvalue_hist(res_bonf, which = "raw") # unadjusted

