## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse  = TRUE,
  comment   = "#>",
  fig.width = 7,
  fig.height = 4.2
)

## ----setup, message = FALSE---------------------------------------------------
library(shewhartr)
library(dplyr)
library(ggplot2)

## -----------------------------------------------------------------------------
fit <- shewhart_i_mr(bottle_fill, value = ml, index = observation)
broom::glance(fit)

## ----eval = FALSE-------------------------------------------------------------
# autoplot(fit)

## -----------------------------------------------------------------------------
fit_med <- shewhart_i_mr(bottle_fill, value = ml,
                         sigma_method = "median_mr")
fit_bw  <- shewhart_i_mr(bottle_fill, value = ml,
                         sigma_method = "biweight")
fit_sd  <- shewhart_i_mr(bottle_fill, value = ml,
                         sigma_method = "sd")

dplyr::bind_rows(
  broom::glance(fit)     |> mutate(method = "mr (default)"),
  broom::glance(fit_med) |> mutate(method = "median_mr"),
  broom::glance(fit_bw)  |> mutate(method = "biweight"),
  broom::glance(fit_sd)  |> mutate(method = "sd")
) |>
  select(method, sigma_hat, n_violations)

## -----------------------------------------------------------------------------
fit_xbar <- shewhart_xbar_r(tablet_weight,
                            value    = weight,
                            subgroup = subgroup)
broom::tidy(fit_xbar)

## -----------------------------------------------------------------------------
shewhart_constants(c(2, 5, 10, 25))

## -----------------------------------------------------------------------------
# Same data, but using S instead of R (preferred for n > 10)
n_per <- 5
df <- tablet_weight |>
  group_by(subgroup) |>
  filter(n() == n_per) |>     # in case of any incomplete batches
  ungroup()

fit_xbs <- shewhart_xbar_s(df, value = weight, subgroup = subgroup)
broom::glance(fit_xbs)

## ----eval = FALSE-------------------------------------------------------------
# fit_xbs_pooled <- shewhart_xbar_s(df, value = weight, subgroup = subgroup,
#                                   sigma_method = "pooled_sd")

## -----------------------------------------------------------------------------
fit_full <- shewhart_i_mr(bottle_fill, value = ml,
                          rules = c("nelson_1_beyond_3s",
                                    "nelson_2_nine_same",
                                    "nelson_3_six_trend",
                                    "nelson_5_two_of_three",
                                    "nelson_6_four_of_five"))
fit_full$violations

