## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7,
                      fig.height = 4.5, dpi = 120)

## ----setup--------------------------------------------------------------------
library(trialSizing)

## ----data, message = FALSE----------------------------------------------------
grid_mat <- function(t)
  as.matrix(uniformity_trial[uniformity_trial$trial == t,
                             grep("^col", names(uniformity_trial))])

tab1 <- calc_cv_shapes(grid_mat("T1"))
X   <- tab1$x
CV1 <- tab1$cv
CV2 <- calc_cv_shapes(grid_mat("T2"))$cv
CV3 <- calc_cv_shapes(grid_mat("T3"))$cv

## ----fit----------------------------------------------------------------------
fit <- fit_lrp(x = X, cv = CV1, step = 0.01)
fit

## ----summary------------------------------------------------------------------
summary(fit)

## ----access-------------------------------------------------------------------
fit$coefficients
fit$parameters["Breakpoint"]
round(fit$residuals[1:5], 3)

## ----predict------------------------------------------------------------------
predict(fit, newx = c(1, 5, 12, 15))

## ----plot---------------------------------------------------------------------
plot(fit, title = "Trial 1")

## ----plot-ptbr----------------------------------------------------------------
plot(fit, title = "Ensaio 1", decimal_mark = ",", cond_word = "se")

## ----save, eval = FALSE-------------------------------------------------------
# plot(fit, title = "Trial 1",
#      save = TRUE, file = "trial1.tiff", format = "tiff",
#      dpi = 300, width = 18, height = 12, units = "cm")

## ----multi--------------------------------------------------------------------
trials <- rbind(
  data.frame(x = X, cv = CV1, trial = "Trial 1"),
  data.frame(x = X, cv = CV2, trial = "Trial 2"),
  data.frame(x = X, cv = CV3, trial = "Trial 3")
)

res <- fit_lrp(trials, x = "x", cv = "cv", trial = "trial", step = 0.01)
res$summary

## ----multi-access-------------------------------------------------------------
res$fits[["Trial 2"]]

## ----multi-plot, eval = FALSE-------------------------------------------------
# plot(res, decimal_mark = ",", cond_word = "se", label_size = 3)

## ----error, error = TRUE------------------------------------------------------
try({
fit_lrp(trials, x = "x", cv = "CV", trial = "trial")
})

## ----competing----------------------------------------------------------------
fit2 <- fit_lrp(X, CV2, step = 0.01)

## ----competing-table----------------------------------------------------------
lm2 <- fit2$local_minima
lm2[c("breakpoint", "SSE", "SSE_excess")] <-
  round(lm2[c("breakpoint", "SSE", "SSE_excess")], 3)
lm2

## ----profile, fig.height = 3.5------------------------------------------------
prof <- fit2$sse_profile
plot(prof$breakpoint, prof$SSE, type = "l",
     xlab = "Breakpoint", ylab = "SSE", las = 1)
abline(v = fit2$parameters["Breakpoint"], col = "forestgreen", lwd = 2)
abline(v = fit2$local_minima$breakpoint, col = "red", lty = 2)

## ----compat-------------------------------------------------------------------
soy_x  <- c(1, 2, 4, 8, 2, 4, 8, 16, 4, 8, 16, 32, 5, 10, 20, 40)
soy_cv <- c(18.699092, 14.130115, 10.321934, 7.990773, 12.690754, 9.995547,
            7.916291, 7.588785, 9.276139, 7.130636, 4.777755, 4.279987,
            8.411412, 5.818440, 3.431264, 3.335962)

seeded <- fit_lrp(soy_x, soy_cv, start = 10.17, step = 0.01)
seeded$compat

## ----search-range-basin-------------------------------------------------------
fit_lrp(soy_x, soy_cv, search_range = c(8, 20), step = 0.01)$parameters["Breakpoint"]

## ----search-range-------------------------------------------------------------
fit_lrp(X, CV1, search_range = c(5, 12), step = 0.01)$parameters["Breakpoint"]

## ----step---------------------------------------------------------------------
fit_lrp(X, CV1, step = 0.01)$parameters["Breakpoint"]

## ----method-------------------------------------------------------------------
fit_lrp(X, CV1, method = "ramp", step = 0.01)$parameters["Breakpoint"]

## ----local-min-tol------------------------------------------------------------
fit_lrp(X, CV2, local_min_tol = 0.02, step = 0.01)$local_minima$competing

