## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = FALSE
)

## ----setup-lr-----------------------------------------------------------------
# library(QCA)
# library(qcaERT)
# 
# data(LR)
# 
# conditions <- c("DEV", "URB", "LIT", "IND", "STB")
# outcome <- "SURV"
# dir_exp <- rep("1", length(conditions))
# 
# thresholds <- list(
#   DEV = findTh(LR$DEV, groups = 7),
#   URB = findTh(LR$URB, groups = 4),
#   LIT = findTh(LR$LIT, groups = 4),
#   IND = findTh(LR$IND, groups = 4),
#   STB = findTh(LR$STB, groups = 4),
#   SURV = findTh(LR$SURV, groups = 4)
# )
# 
# dat <- LR
# dat$DEV <- calibrate(LR$DEV, type = "fuzzy", thresholds = thresholds$DEV)
# dat$URB <- calibrate(LR$URB, type = "fuzzy", thresholds = thresholds$URB)
# dat$LIT <- calibrate(LR$LIT, type = "fuzzy", thresholds = thresholds$LIT)
# dat$IND <- calibrate(LR$IND, type = "fuzzy", thresholds = thresholds$IND)
# dat$STB <- calibrate(LR$STB, type = "fuzzy", thresholds = thresholds$STB)
# dat$SURV <- calibrate(LR$SURV, type = "fuzzy", thresholds = thresholds$SURV)

## ----calib-spec---------------------------------------------------------------
# calib_spec <- list(
#   DEV = list(
#     raw = "DEV",
#     type = "fuzzy",
#     method = "direct",
#     thresholds = thresholds$DEV
#   ),
#   URB = list(
#     raw = "URB",
#     type = "fuzzy",
#     thresholds = thresholds$URB
#   ),
#   LIT = list(
#     raw = "LIT",
#     type = "fuzzy",
#     thresholds = thresholds$LIT
#   ),
#   IND = list(
#     raw = "IND",
#     type = "fuzzy",
#     thresholds = thresholds$IND
#   ),
#   STB = list(
#     raw = "STB",
#     type = "fuzzy",
#     thresholds = thresholds$STB
#   )
# )
# 
# calib_spec_outcome <- calib_spec
# calib_spec_outcome$SURV <- list(
#   raw = "SURV",
#   type = "fuzzy",
#   thresholds = thresholds$SURV
# )

## ----conditions---------------------------------------------------------------
# calib.test(
#   raw.data = LR,
#   calib.data = dat,
#   outcome = "SURV",
#   conditions = c("DEV", "URB", "LIT", "IND", "STB"),
#   calib_spec = calib_spec,
#   test.conditions = c("DEV", "URB"),
#   unit_step = NULL,
#   unit_step_divisor = 10,
#   max_steps = 5,
#   incl.cut = 0.8,
#   n.cut = 1,
#   solution = "all",
#   dir.exp = rep("1", 5),
#   progress = TRUE
# )

## ----outcome-calibration------------------------------------------------------
# calib_outcome <- calib.test(
#   raw.data = LR,
#   calib.data = dat,
#   outcome = outcome,
#   conditions = conditions,
#   calib_spec = calib_spec_outcome,
#   test.conditions = NULL,
#   test.outcome = TRUE,
#   unit_step = NULL,
#   unit_step_divisor = 10,
#   max_steps = 5,
#   incl.cut = 0.8,
#   n.cut = 1,
#   solution = "all",
#   dir.exp = dir_exp,
#   progress = TRUE
# )
# 
# calib_outcome
# as.data.frame(calib_outcome)

## ----anchors------------------------------------------------------------------
# calib.test(
#   raw.data = LR,
#   calib.data = dat,
#   outcome = "SURV",
#   conditions = conditions,
#   calib_spec = calib_spec,
#   test.conditions = "DEV",
#   anchors_to_test = c("E1", "C1", "I1", "I2", "C2", "E2"),
#   unit_step = NULL,
#   unit_step_divisor = 10,
#   max_steps = 5,
#   incl.cut = 0.8,
#   n.cut = 1,
#   solution = "all",
#   dir.exp = dir_exp,
#   progress = TRUE
# )

## ----scale-aware--------------------------------------------------------------
# calib_out <- calib.test(
#   raw.data = LR,
#   calib.data = dat,
#   outcome = outcome,
#   conditions = conditions,
#   calib_spec = calib_spec,
#   test.conditions = c("DEV", "URB"),
#   unit_step = NULL,
#   unit_step_divisor = 10,
#   max_steps = 5,
#   incl.cut = 0.8,
#   n.cut = 1,
#   solution = "all",
#   dir.exp = dir_exp,
#   progress = TRUE
# )

## ----indirect-----------------------------------------------------------------
# calib_spec_indirect <- list(
#   A = list(
#     raw = "A_raw",
#     type = "fuzzy",
#     method = "indirect",
#     thresholds = c(10, 20, 30, 40)
#   )
# )

## ----altset-------------------------------------------------------------------
# altset_out <- altset.test(
#   raw.data = LR,
#   calib.data = dat,
#   outcome = outcome,
#   conditions = conditions,
#   calib_spec = calib_spec,
#   test.conditions = c("DEV", "URB"),
#   unit_step = NULL,
#   unit_step_divisor = 10,
#   calib_max_steps = 5,
#   incl.cut = 0.8,
#   incl_step = 0.02,
#   incl_max_steps = 5,
#   n.cut = 1,
#   ncut_step = 1,
#   ncut_max_steps = 2,
#   n_draws = 50,
#   seed = 123,
#   solution = "all",
#   dir.exp = dir_exp,
#   progress = TRUE
# )
# 
# altset_out
# as.data.frame(altset_out)
# altset_out$summary

## ----altset-outcome-----------------------------------------------------------
# altset_outcome <- altset.test(
#   raw.data = LR,
#   calib.data = dat,
#   outcome = outcome,
#   conditions = conditions,
#   calib_spec = calib_spec_outcome,
#   test.conditions = c("DEV", "URB"),
#   test.outcome = TRUE,
#   unit_step = NULL,
#   unit_step_divisor = 10,
#   calib_max_steps = 5,
#   incl.cut = 0.8,
#   incl_step = 0.02,
#   incl_max_steps = 5,
#   n.cut = 1,
#   ncut_step = 1,
#   ncut_max_steps = 2,
#   n_draws = 50,
#   seed = 456,
#   solution = "all",
#   dir.exp = dir_exp,
#   progress = TRUE
# )
# 
# as.data.frame(altset_outcome)

## ----calib-plots--------------------------------------------------------------
# plot(calib_out, solution_type = "conservative")
# plot(calib_out, solution_type = "conservative", type = "heatmap")
# plot(calib_out, solution_type = "conservative", type = "trace", set = "DEV", anchor = "E1", direction = "lower")

