## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(TheOrdinals)

## ----data---------------------------------------------------------------------
x <- rbind(
  c(1, 2, 3, 4, 1, 1, 0, 0),
  c(2, 1, 3, 4, 1, 0, 0, 0),
  c(1, 2, 4, 3, 1, 1, 0, 0),
  c(1, 3, 2, 4, 1, 1, 1, 0)
)
x

## ----consistent---------------------------------------------------------------
is_consistent(c(1, 2, 3, 4), c(1, 1, 0, 0))  # admissible
is_consistent(c(1, 2, 3, 4), c(0, 1, 0, 0))  # not admissible

## ----universe-----------------------------------------------------------------
find_approval(c(1, 2, 2, 3))
dim(pa_universe(3))

## ----dist---------------------------------------------------------------------
round(pref_dist(x), 3)              # lambda = 0.5
round(pref_dist(x, lambda = 0.8), 3)

## ----diva-small---------------------------------------------------------------
res <- diva(x, algorithm = "quick")
res
res$d_lambda

## ----french-------------------------------------------------------------------
data(french_election_2002)
dim(french_election_2002)
fc <- diva(french_election_2002, algorithm = "quick")
fc$d_lambda

## ----french-result------------------------------------------------------------
n <- 15
cons <- fc$consensus[1, ]
data.frame(
  candidate = colnames(french_election_2002)[1:n],
  rank      = as.numeric(cons[1:n]),
  approved  = as.numeric(cons[(n + 1):(2 * n)])
)

## ----sensitivity, fig.width = 6, fig.height = 4, fig.alt = "DIVA consensus distance as a function of lambda"----
s <- diva_sensitivity(french_election_2002)
plot(s$lambda, s$d_lambda, type = "b", pch = 19,
     xlab = expression(lambda), ylab = expression(D[lambda]),
     main = "DIVA consensus distance vs lambda")

## ----f1, eval = FALSE---------------------------------------------------------
# data(formula1_1950)
# diva(formula1_1950, algorithm = "quick")

