## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", message = FALSE)
has_randomizr <- requireNamespace("randomizr", quietly = TRUE)
library(estimatr)

## ----eval = has_randomizr, warning = FALSE------------------------------------
library(randomizr)
set.seed(3)
decl3 <- declare_ra(N = 300, conditions = c("control", "T1", "T2"))
Z3 <- conduct_ra(decl3)
dat3 <- data.frame(Y = rnorm(300) + 0.4 * (Z3 == "T1") + 0.8 * (Z3 == "T2"), Z = Z3)

horvitz_thompson(Y ~ Z, data = dat3, condition_prs = decl3,
                 condition1 = "control", condition2 = "T2")

## ----eval = has_randomizr-----------------------------------------------------
set.seed(2)
decl <- declare_ra(blocks = rep(c("a", "b", "c", "d"), each = 50), prob = 0.4)
Z <- conduct_ra(decl)
dat_ht <- data.frame(Y = rnorm(200) + 0.5 * Z, Z = Z)

horvitz_thompson(Y ~ Z, data = dat_ht, condition_prs = decl)

## -----------------------------------------------------------------------------
set.seed(343)
dat <- data.frame(
  y = rnorm(1000), x = rnorm(1000), z = rbinom(1000, 1, 0.5),
  cl = rep(1:100, each = 10), bl = rep(1:50, each = 20), bl2 = rep(1:20, times = 50)
)

# unclustered, one factor: HC2, as in 1.x
lm_robust(y ~ z + x, data = dat, fixed_effects = ~ bl)$se_type

# unclustered, two factors: still HC2, as in 1.x
lm_robust(y ~ z + x, data = dat, fixed_effects = ~ bl + bl2)$se_type

## -----------------------------------------------------------------------------
lm_robust(y ~ z + x, data = dat, fixed_effects = ~ bl,
          clusters = cl, se_type = "CR2")$std.error

## ----error = TRUE-------------------------------------------------------------
try({
starprep(lm(y ~ z, data = dat))
})

## -----------------------------------------------------------------------------
set.seed(7)
big <- data.frame(
  bl = rep(paste0("big", 1:20), each = 10),
  z = rep(rep(0:1, each = 5), times = 20)
)
small <- data.frame(
  bl = rep(paste0("sm", 1:12), each = 4),
  z = rep(c(1, 0, 0, 0), times = 12)
)
dat_bl <- rbind(big, small)
dat_bl$y <- rnorm(nrow(dat_bl)) + 0.3 * dat_bl$z

difference_in_means(y ~ z, data = dat_bl, blocks = bl)

## -----------------------------------------------------------------------------
pairs <- data.frame(bl = rep(paste0("pr", 1:12), each = 2), z = rep(c(1, 0), times = 12))
dat_pr <- rbind(big, pairs)
dat_pr$y <- rnorm(nrow(dat_pr)) + 0.3 * dat_pr$z

difference_in_means(y ~ z, data = dat_pr, blocks = bl)

## -----------------------------------------------------------------------------
difference_in_means(y ~ z, data = dat_bl, blocks = bl)$design

## ----error = TRUE-------------------------------------------------------------
try({
dat_one <- rbind(big, data.frame(bl = "sm1", z = c(1, 0, 0, 0)))
dat_one$y <- rnorm(nrow(dat_one))
difference_in_means(y ~ z, data = dat_one, blocks = bl)
})

## -----------------------------------------------------------------------------
fit <- lm_robust(y ~ z + x, data = dat)
head(round(residuals(fit), 4))
all.equal(residuals(fit) + fitted(fit), dat$y, check.attributes = FALSE)

## ----warning = TRUE-----------------------------------------------------------
dat$x_copy <- dat$x
fit <- lm_robust(y ~ x + x_copy, data = dat)

