## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  message = FALSE,
  warning = FALSE,
  fig.align = "center",
  fig.height = 6,
  fig.width = 7,
  fig.path = "fig/",
  dev = "png",
  comment = "#>"
)

# save some typing
knitr::set_alias(w = "fig.width",
                 h = "fig.height",
                 cap = "fig.cap")

set.seed(47)
.opts <- options(digits = 4)

# packages to be cited in packages.bib
.to.cite <- c("nestedLogit", "ggeffects", "ggplot2")

## ----setup--------------------------------------------------------------------
library(nestedLogit)    # Nested Dichotomy Logistic Regression Models
library(ggeffects)      # Create Tidy Data Frames of Marginal Effects
library(ggplot2)        # Data Visualisations Using the Grammar of Graphics

## ----wlf-model----------------------------------------------------------------
data(Womenlf, package = "carData")
comparisons <- logits(work = dichotomy("not.work", c("parttime", "fulltime")),
                      full = dichotomy("parttime", "fulltime"))

wlf.nested <- nestedLogit(partic ~ hincome + children,
                          dichotomies = comparisons,
                          data = Womenlf)

## ----wlf-predict-response-----------------------------------------------------
wlf.pred <- predict_response(wlf.nested, terms = c("hincome", "children"))
wlf.pred

## ----wlf-ggeffects-plot1------------------------------------------------------
plot(wlf.pred)

## ----wlf-ggeffects-plot2------------------------------------------------------
plot(wlf.pred,
     line_size = 2) +
  labs(title = "Predicted Probabilities of Work by Husband's Income",
       y = "Probability",
       x = "Husband's Income") +
  theme_ggeffects(base_size = 14) +
  theme(legend.position = "top")

## ----wlf-logit-scale----------------------------------------------------------
plot(wlf.pred,
     line_size = 2) +
  scale_y_continuous(
    transform = "logit",
    breaks = c(0.05, 0.10, 0.25, 0.50, 0.75, 0.90, 0.95)
  ) +
  labs(title = "Predicted Probabilities (logit scale)",
       y = "Probability (logit scale)",
       x = "Husband's Income") +
  theme_ggeffects(base_size = 16) +
  theme(legend.position = "top")

## ----wlf-dichotomies----------------------------------------------------------
wlf.pred.dichot <- predict_response(wlf.nested, terms = c("hincome", "children"),
                             submodel = "dichotomies")

plot(wlf.pred.dichot) +
  geom_point() +
  theme(legend.position = "inside",
        legend.position.inside = c(.40, .85))

## ----gators-model-------------------------------------------------------------
data(gators)

# setup the dichotomies
gators.dichots = logits(
     other   = dichotomy("Other", c("Fish", "Invertebrates")),
     fish_inv = dichotomy("Fish", "Invertebrates"))

as.tree(gators.dichots)

# fit the model
gators.nested <- nestedLogit(food ~ length,
                             dichotomies = gators.dichots,
                             data = gators)

## ----gators-ggeffects---------------------------------------------------------
predict_response(gators.nested, terms = "length") |>
  plot(line_size = 2)

## ----gators-plot, echo=-1-----------------------------------------------------
par(mar = c(4, 4, 1, 1) + 0.5)
plot(gators.nested, x.var = "length",
     lty=1, lwd = 4,
     label = TRUE, label.col = "black", cex.lab = 1.3)


## ----write-bib, echo=FALSE----------------------------------------------------
pkgs <- unique(c(.to.cite, .packages()))
knitr::write_bib(pkgs, file = here::here("vignettes", "packages.bib"))

## ----include = FALSE----------------------------------------------------------
options(.opts)

