## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 6,
                      fig.height = 4)

## ----setup--------------------------------------------------------------------
library(softcats)

## ----data---------------------------------------------------------------------
d <- sim_softcats(n = 3000, K = 3, seed = 1)
head(d[, c("y", "x1", "x2", "x3", "prob_A", "prob_B", "prob_C")])
attr(d, "tau")

## ----fit----------------------------------------------------------------------
fit <- soft_lm(y ~ x1 + x2 + x3, probs = c("prob_A", "prob_B", "prob_C"),
               data = d, learner = lrn_lm(degree = 3), seed = 1)
summary(fit)

## ----hard---------------------------------------------------------------------
h <- compare_hard(fit)
h

## ----hard-numbers, include = FALSE--------------------------------------------
est_hard <- h$estimates[h$estimates$method == "hard (cross-fitted)", ]
lim <- drop(h$distortion %*% attr(d, "tau"))
cov_hard <- h$coverage$coverage
f2 <- function(v) formatC(v, digits = 2, format = "f")

## ----learners-----------------------------------------------------------------
fit_rf <- soft_lm(y ~ x1 + x2 + x3, probs = ~ prob_A + prob_B + prob_C,
                  data = d, learner = lrn_ranger(num.trees = 200),
                  n_rep = 5, seed = 1)
coef(fit_rf)

fit_mix <- update(fit, learner = list(m = lrn_ranger(num.trees = 200),
                                      r = lrn_lm(degree = 3)))
coef(fit_mix)

## ----spectrum-----------------------------------------------------------------
score_spectrum(fit)
plot(fit, which = "spectrum")

## ----labelled-----------------------------------------------------------------
lab <- d[d$labelled, ]
calibration_check(probs = ~ prob_A + prob_B + prob_C, group = "group",
                  data = lab, controls = ~ x1 + x2 + x3)
exclusion_test(y ~ x1 + x2 + x3, group = "group", extra = ~ z, data = lab)

## ----sensitivity--------------------------------------------------------------
drift_sensitivity(fit, delta = 0.02)

## ----check--------------------------------------------------------------------
soft_check(fit, labelled = lab, group = "group", extra = ~ z)

## ----check-bad----------------------------------------------------------------
bad <- sim_softcats(n = 6000, K = 2, miscalibration = 1.5,
                    direct_effect = 0.5, labelled_share = 0.4, seed = 2)
fit_bad <- soft_lm(y ~ x1 + x2 + x3, probs = c("prob_A", "prob_B"),
                   data = bad, learner = lrn_lm(degree = 3), seed = 2)
soft_check(fit_bad, labelled = bad[bad$labelled, ], group = "group",
           extra = ~ z)

## ----recalibrate--------------------------------------------------------------
o <- sim_softcats(n = 4000, K = 3, seed = 1)
P <- as.matrix(o[, c("prob_A", "prob_B", "prob_C")])
o[, c("over_A", "over_B", "over_C")] <- P^2 / rowSums(P^2)
fit_over <- soft_lm(y ~ x1 + x2 + x3, probs = c("over_A", "over_B", "over_C"),
                    data = o, learner = lrn_lm(degree = 3), seed = 1)
rc <- recalibrate(fit_over, labelled = o[o$labelled, ], group = "group")
rc

## ----recalibrate-numbers, include = FALSE-------------------------------------
re <- rc$estimates

## ----recalibrate-predict------------------------------------------------------
o[, c("rec_A", "rec_B", "rec_C")] <- predict(rc, o)
coef(update(fit_over, probs = c("rec_A", "rec_B", "rec_C")))

## ----recalibrate-bad----------------------------------------------------------
rc_bad <- recalibrate(fit_bad, labelled = bad[bad$labelled, ],
                      group = "group")
rc_bad$estimates

## ----kappa--------------------------------------------------------------------
b <- sim_softcats(n = 3000, K = 2, seed = 3)
fit_b <- soft_lm(y ~ x1 + x2 + x3, probs = b$prob_B, data = b,
                 learner = lrn_lm(degree = 3), seed = 3)
kc <- kappa_curve(fit_b, thresholds = seq(0.5, 0.9, by = 0.1))
kc[, c("threshold", "share_positive", "kappa", "estimate_hard",
       "estimate_soft")]
plot(kc)

## ----design-------------------------------------------------------------------
set.seed(1)
d$w <- runif(nrow(d), 0.5, 2)
d$area <- rep(seq_len(300), each = 10)
fit_w <- soft_lm(y ~ x1 + x2 + x3, probs = ~ prob_A + prob_B + prob_C,
                 data = d, weights = ~ w, cluster = ~ area,
                 learner = lrn_lm(degree = 3), seed = 1)
summary(fit_w)$coefficients

## ----compat-------------------------------------------------------------------
tidy(fit, conf.int = TRUE)
glance(fit)

## ----compat-other, eval = requireNamespace("lmtest", quietly = TRUE)----------
lmtest::coeftest(fit)

