## ----setup, echo=FALSE----------------------------------------------
options(width = 70)
knitr::opts_chunk$set(
  message = FALSE,
  comment = "",
  highlight = TRUE,
  prompt = TRUE,
  tidy = TRUE,
  tidy.opts = list(arrow = FALSE),
  warning = FALSE
)
suppressWarnings(suppressPackageStartupMessages(library(fitPS)))

## ----load-roux------------------------------------------------------
data("Psurveys")
roux = Psurveys$roux

## ----print-roux-----------------------------------------------------
roux

## ----fit-roux-------------------------------------------------------
fit = fit(roux, model = zetaModel())

## ----print-fit------------------------------------------------------
fit

## ----probability-function-------------------------------------------
P = probfun(fit)

## ----probability-example--------------------------------------------
P(5)

## ----fit-zero-inflated----------------------------------------------
fit.zi = fit(roux, model = zizModel())
fit.zi

## ----ex1, echo=FALSE, results='asis'--------------------------------
raw = c(roux$data$rn / sum(roux$data$rn), 0)
tableData = cbind(
  0:5,
  raw,
  fitted(fit, 6),
  fitted(fit.zi, 6)
)
colnames(tableData) = c(
  "$k$",
  "$P_k^{raw}$",
  "$P_k^{zeta}$",
  "$P_k^{ZIZ}$"
)
probabilityTable = xtable::xtable(
  tableData,
  align = "ccccc",
  digits = c(0, 0, 4, 4, 4),
  caption = "Estimated probability that k groups of glass would be found in shoes of a random member of the population based on the data of Roux et al. (2001), the raw frequencies, and those produced from the zeta and ZIZ models respectively.",
  label = "tab:ex1"
)
print(
  probabilityTable,
  type = "latex",
  include.rownames = FALSE,
  sanitize.text.function = identity,
  comment = FALSE
)

## ----confidence-intervals-------------------------------------------
ci = confint(fit)
ci$wald
ci$prof

## ----confidence-region, eval=FALSE----------------------------------
# plotUncertainty(fit.zi, level = c(0.80, 0.95))
# cr = confint(fit.zi, level = c(0.80, 0.95))

## ----boot-confidence-region, eval=FALSE-----------------------------
# boot.fit = fit(
#   roux,
#   model = zizModel(),
#   method = "bootstrap",
#   B = 2000,
#   seed = 123,
#   silent = TRUE
# )
# plotUncertainty(boot.fit, level = c(0.80, 0.95))

## ----compare-data-table, echo=FALSE, results='asis'-----------------
lau = Psurveys$lau
jackson = Psurveys$jackson
surveyTableData = cbind(
  0:1,
  lau$data$rn,
  jackson$data$rn
)
colnames(surveyTableData) = c(
  "$k$",
  "Lau et al. (1997)",
  "Jackson et al. (2013)"
)
surveyTable = xtable::xtable(
  surveyTableData,
  align = "ccrr",
  digits = 0,
  caption = "Survey results from Lau et al. (1997) and Jackson et al. (2013).",
  label = "tab:survey-comparison"
)
print(
  surveyTable,
  type = "latex",
  include.rownames = FALSE,
  sanitize.text.function = identity,
  comment = FALSE
)

## ----compare-fits---------------------------------------------------
lau = Psurveys$lau
jackson = Psurveys$jackson
fit.lau = fit(lau, model = zetaModel())
fit.jackson = fit(jackson, model = zetaModel())
confint(fit.lau)$wald
confint(fit.jackson)$wald

## ----compare-surveys------------------------------------------------
compareSurveys(lau, jackson)

