# Difference-in-difference: standard errors in nonparametric estimation and linear regression

It is well known that we can use a linear regression model to calculate the DiD estimate. I followed this website and created a simulation dataset.

set.seed(200)
# set sample size
n <- 200

# define treatment effect
TEffect <- 4

# generate treatment dummy
TDummy <- c(rep(0, n/2), rep(1, n/2))

# simulate pre- and post-treatment values of the dependent variable
y_pre <- 7 + rnorm(n)
y_pre[1:n/2] <- y_pre[1:n/2] - 1
y_post <- 7 + 2 + TEffect * TDummy + rnorm(n)
y_post[1:n/2] <- y_post[1:n/2] - 1

before <- data.frame(y = y_pre, treatment = TDummy, post = 0, id = 1:n)
after <- data.frame(y = y_post, treatment = TDummy, post = 1, id = 1:n)
data <- rbind(before, after)


First, I followed Angrist and Pischke (2008, p.229) to obtain the point estimate and calculated the standard error.

# Nonparametric
did_point <-  (mean(after$$y[after$$treatment == 1]) - mean(after$$y[after$$treatment == 0])) -
(mean(before$$y[before$$treatment == 1]) - mean(before$$y[before$$treatment == 0]))

n_treated <- sum(before$$treatment) se_treated <- sqrt(var(before$$y[before$$treatment == 1]) / n_treated + var(after$$y[after$$treatment == 1]) / n_treated - 2 * var(before$$y[before$$treatment == 1], after$$y[after$treatment == 1]) / n_treated) n_control <- sum(before$$treatment == 0) se_control <- sqrt(var(before$$y[before$$treatment == 0]) / n_control + var(after$$y[after$$treatment == 0]) / n_control - 2 * var(before$$y[before$$treatment == 0], after$$y[after$treatment == 0]) / n_control)

did_se <- sqrt(se_treated^2 + se_control^2)
did_se  # 0.1947522


If I use the linear regression, the point estimate is the same but the standard error is slightly different.

fit <- lm(y ~ treatment * post, data = data)
summary(fit)  # SE is 0.19111


If I use clustered standard error, the standard error is exactly the same.

library(estimatr)
fit <- lm_robust(y ~ treatment * post, clusters = id, data = data)
fit\$std.error["treatment:post"]  # 0.1947522


Then, is it always better to use clustered standard error in DiD? Does it account for autocorrelation between pre/post in the same individual (which is captured as covariance in the nonparametric approach)?