## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 6, fig.height = 4,
                      message = FALSE)

## ----bp-----------------------------------------------------------------------
library(iop)
data(bp)
round(prop.table(table(bp$violence)), 3)

## ----bp-fit-------------------------------------------------------------------
f_op   <- violence ~ loggdppc + parliament + disaster + major_oil + major_primary
f_ziop <- violence ~ loggdppc + parliament + disaster + major_oil + major_primary |
  loggdppc + parliament + disaster + major_oil + major_primary
m_op   <- oprobit(f_op, data = bp)
m_ziop <- iop(f_ziop, data = bp, inflate = "bottom")
summary(m_ziop)

## ----bp-test------------------------------------------------------------------
inflation_test(m_ziop)
compare_models(op = m_op, ziop = m_ziop)

## ----bp-pred------------------------------------------------------------------
head(predict(m_ziop, type = "prob"), 3)
summary(predict(m_ziop, type = "inflated"))
summary(predict(m_ziop, type = "posterior")[bp$violence == "none"])

## ----bp-fd--------------------------------------------------------------------
first_difference(m_ziop, "loggdppc", from = 7, to = 9)
first_difference(m_ziop, "loggdppc", from = 7, to = 9, stage = "inflation")

## ----bp-corr------------------------------------------------------------------
m_ziopc <- iop(f_ziop, data = bp, inflate = "bottom", correlated = TRUE)
coef(m_ziopc)["rho"]
confint(m_ziopc, parm = "rho")
lr_test(m_ziop, m_ziopc)

## ----bp-logit-----------------------------------------------------------------
m_ol   <- ologit(f_op, data = bp)
m_ziol <- iol(f_ziop, data = bp, inflate = "bottom")
compare_models(op = m_op, ziop = m_ziop, ol = m_ol, ziol = m_ziol)

## ----pta----------------------------------------------------------------------
data(pta)
table(pta$flexibility)
m_tiop <- iop(flexibility ~ depth * democracy + gdp + gdppc + trade + gattwto + members +
                democratization | gdp + gdppc + democracy + democratization,
              data = pta, inflate = "top")
summary(m_tiop)

## ----repression---------------------------------------------------------------
data(repression)
m_rep <- iop(repression ~ negxpol * oilrent | dom_media + civil_war,
             data = repression, inflate = "top")
inflation_test(m_rep)$vuong

## ----middle-------------------------------------------------------------------
set.seed(1)
d <- riop(1000, beta = c(0.8, -0.5), tau = c(-0.8, 0.8), gamma = c(0.2, 1),
          inflate = "middle", labels = c("disagree", "neutral", "agree"))
table(d$y)
m_miop <- iop(y ~ x1 + x2 | z1, data = d, inflate = "middle")
m_miop

## ----middle-post--------------------------------------------------------------
summary(predict(m_miop, type = "posterior")[d$y == "neutral"])

## ----affairs, eval = requireNamespace("AER", quietly = TRUE)------------------
data("Affairs", package = "AER")
m_aff <- iop(affairs ~ age + yearsmarried + religiousness + rating | gender + children,
             data = Affairs, inflate = "bottom")
m_aff
colMeans(predict(m_aff, type = "zeros"))       # zeros from the inflation process / the ordered stage
inflation_test(m_aff)

## ----nmes, eval = requireNamespace("AER", quietly = TRUE)---------------------
data("NMES1988", package = "AER")
m_nmes <- iop(health ~ age + gender + married + chronic + adl + income + insurance | school + age,
              data = NMES1988, inflate = "middle")
round(summary(m_nmes)$coefficients[, 1:2], 3)
ame(m_nmes, vars = c("chronic", "insurance"))

