The following code shows the application of the method in investigating the effect of misdemeanor prosecution (X) on criminal complaint in two years (Y), based on the paper Misdemeanor Prosecution (Agan et al., 2023).

# 0. Setup ----------------------------------------------------------------
df <- suffolk

# 1. Preparation ----------------------------------------------------------
# 1.1. Variable Assignment
df <- df %>%
  mutate(
    X = ng_immed_all,
    Y = anyr_twoyears_arrest2,
    groupZ = first_pros
  )

# 1.2. Construct Covariate Groups (groupW)
df <- df %>%
  mutate(
    monthID = as.integer(factor(court_month2)),
    dowID   = as.integer(factor(court_dow2)),
    groupW  = as.integer(factor(paste(dowID, monthID, sep = "_")))
  )

# 1.3. Construct Interaction Groups (groupQ / group)
df <- df %>%
  mutate(
    groupQ = as.integer(factor(paste(groupW, groupZ, sep = "_"))),
    group  = groupQ
  )

# 1.4. Filter for Group Size
df <- df %>%
  group_by(groupQ) %>%
  filter(n() >= 4) %>%
  ungroup()


# 1.5. Get residualized objects
lmX <- fixest::feols(X~1|group,data=df)
df$MX <- lmX$residuals

lmY <- fixest::feols(Y~1|group,data=df)
df$MY <- lmY$residuals

# 2. Inference ------------------------------------------------------------
# L3O CI
S_hat <- GetLM(df, X, X, groupW, group, noisy = FALSE)
JIVE <- GetLM(df, X, Y, groupW, group, noisy = FALSE)/S_hat 
JIVE #-0.144
#> [1] -0.1440279

L3OCIcoef <- GetCIcoef(df, groupW, group, X, Y, MX, MY, noisy = FALSE)
L3OCI <- GetCItypebd(L3OCIcoef)[2:3] # [-0.222, -0.066]
L3OCI
#> [1] -0.22183974 -0.06643859

# 3. Table ----------------------------------------------------------------
# 3.1. Create the matrix using calculated L3O variables
L3O_data <- matrix(c(
  L3OCI[1],                # Lower Bound
  L3OCI[2],                # Upper Bound
  JIVE,                    # Point Estimate
  L3OCI[2] - L3OCI[1]      # CI Length
), ncol = 1)

# 3.2. Add labels for row/col
rownames(L3O_data) <- c("LB", "UB", "Estimate", "CIlength")
colnames(L3O_data) <- "L3O"

# 3.3 Round and save
CItab_L3O_round <- round(L3O_data, 3)
CItab_L3O_round
#>             L3O
#> LB       -0.222
#> UB       -0.066
#> Estimate -0.144
#> CIlength  0.155