\(\tau_t(r)\) | \(r = 1\) | \(r = 2\) | \(r = 3\) |
---|---|---|---|
\(t = 1\) | 1 | 2 | 3 |
\(t = 2\) | 4 | 5 | 6 |
\(t = 3\) | 7 | 8 | 9 |
set.seed(1) N_g <- 100 T <- 3 G <- T N <- G * N_g tau <- tidyr::expand_grid( t = 1:T, g = 1:G ) %>% dplyr::mutate( tau = 1:length(t) )
df <- tidyr::expand_grid( t = 1:T, i = 1:N ) %>% dplyr::mutate( g = ceiling(i / 100) ) %>% dplyr::left_join( tau, by = c("g", "t") )
df <- df %>% dplyr::mutate( y_0 = 0, y_1 = tau + rnorm(length(t)) ) %>% dplyr::mutate( d = (g > 1) * (t >= g), y = y_1 * d + y_0 * (1 - d) )
model_ols <- lm( data = df, formula = y ~ I((g == 2)&(t == 2)) + I((g == 2) & (t == 3)) + I((g == 3) & (t == 3)) + as.factor(g) + as.factor(t) ) modelsummary::modelsummary(model_ols, coef_omit = "as.factor")
Model 1 | |
---|---|
(Intercept) | 0.000 |
(0.056) | |
I((g == 2) & (t == 2))TRUE | 4.961 |
(0.112) | |
I((g == 2) & (t == 3))TRUE | 8.000 |
(0.125) | |
I((g == 3) & (t == 3))TRUE | 9.010 |
(0.112) | |
Num.Obs. | 900 |
R2 | 0.969 |
R2 Adj. | 0.969 |
AIC | 1776.7 |
BIC | 1819.9 |
Log.Lik. | -879.352 |
F | 3980.057 |
model_twfe <- lfe::felm( data = df, formula = y ~ I((g == 2)&(t == 2)) + I((g == 2) & (t == 3)) + I((g == 3) & (t == 3)) | i + t ) modelsummary::modelsummary(model_twfe)
Model 1 | |
---|---|
I((g == 2) & (t == 2))TRUE | 4.961 |
(0.110) | |
I((g == 2) & (t == 3))TRUE | 8.000 |
(0.123) | |
I((g == 3) & (t == 3))TRUE | 9.010 |
(0.110) | |
Num.Obs. | 900 |
R2 | 0.980 |
R2 Adj. | 0.970 |
did_g2t2 <- df %>% dplyr::filter( (g == 1 & t <= 2) | (g == 2 & t <= 2) ) %>% lfe::felm( data = ., formula = y ~ d | g + t ) modelsummary::modelsummary(did_g2t2)
Model 1 | |
---|---|
d | 4.961 |
(0.117) | |
Num.Obs. | 400 |
R2 | 0.932 |
R2 Adj. | 0.931 |
did_g2t3 <- df %>% dplyr::filter( (g == 1 & (t == 1 | t == 3)) | (g == 2 & (t == 1 | t == 3)) ) %>% lfe::felm( data = ., formula = y ~ d | g + t ) modelsummary::modelsummary(did_g2t3)
Model 1 | |
---|---|
d | 8.000 |
(0.110) | |
Num.Obs. | 400 |
R2 | 0.976 |
R2 Adj. | 0.976 |
did_g3t3 <- df %>% dplyr::filter( (g == 1) | (g == 3) ) %>% lfe::felm( data = ., formula = y ~ d | g + t ) modelsummary::modelsummary(did_g3t3)
Model 1 | |
---|---|
d | 9.010 |
(0.077) | |
Num.Obs. | 600 |
R2 | 0.983 |
R2 Adj. | 0.983 |
set.seed(1) N_g <- 100 T <- 3 G <- T N <- G * N_g tau <- tidyr::expand_grid( t = 1:T, g = 1:G ) %>% dplyr::mutate( tau = 1:length(t) )
df <- tidyr::expand_grid( t = 1:T, i = 1:N ) %>% dplyr::mutate( g = ceiling(i / 100) ) %>% dplyr::left_join( tau, by = c("g", "t") )
df <- df %>% dplyr::mutate( y_0 = rnorm(length(t)), y_1 = tau + rnorm(length(t)) ) %>% dplyr::mutate( d = (g > 1) * (t >= g), y = y_1 * d + y_0 * (1 - d) )
model_ols <- lm( data = df, formula = y ~ I((g == 2)&(t == 2)) + I((g == 2) & (t == 3)) + I((g == 3) & (t == 3)) + as.factor(g) + as.factor(t) ) modelsummary::modelsummary(model_ols, coef_omit = "as.factor")
Model 1 | |
---|---|
(Intercept) | 0.113 |
(0.087) | |
I((g == 2) & (t == 2))TRUE | 4.930 |
(0.173) | |
I((g == 2) & (t == 3))TRUE | 8.255 |
(0.194) | |
I((g == 3) & (t == 3))TRUE | 9.199 |
(0.173) | |
Num.Obs. | 900 |
R2 | 0.927 |
R2 Adj. | 0.927 |
AIC | 2563.0 |
BIC | 2606.2 |
Log.Lik. | -1272.477 |
F | 1625.561 |
model_twfe <- lfe::felm( data = df, formula = y ~ I((g == 2)&(t == 2)) + I((g == 2) & (t == 3)) + I((g == 3) & (t == 3)) | i + t ) modelsummary::modelsummary(model_twfe)
Model 1 | |
---|---|
I((g == 2) & (t == 2))TRUE | 4.930 |
(0.173) | |
I((g == 2) & (t == 3))TRUE | 8.255 |
(0.193) | |
I((g == 3) & (t == 3))TRUE | 9.199 |
(0.173) | |
Num.Obs. | 900 |
R2 | 0.952 |
R2 Adj. | 0.927 |
did_g2t2 <- df %>% dplyr::filter( (g == 1 & t <= 2) | (g == 2 & t <= 2) ) %>% lfe::felm( data = ., formula = y ~ d | g + t ) modelsummary::modelsummary(did_g2t2)
Model 1 | |
---|---|
d | 4.922 |
(0.186) | |
Num.Obs. | 400 |
R2 | 0.834 |
R2 Adj. | 0.833 |
did_g2t3 <- df %>% dplyr::filter( (g == 1 & (t == 1 | t == 3)) | (g == 2 & (t == 1 | t == 3)) ) %>% lfe::felm( data = ., formula = y ~ d | g + t ) modelsummary::modelsummary(did_g2t3)
Model 1 | |
---|---|
d | 8.251 |
(0.197) | |
Num.Obs. | 400 |
R2 | 0.925 |
R2 Adj. | 0.925 |
did_g3t3 <- df %>% dplyr::filter( (g == 1 & (t == 1 | t == 3)) | (g == 3 & (t == 1 | t == 3)) ) %>% lfe::felm( data = ., formula = y ~ d | g + t ) modelsummary::modelsummary(did_g3t3)
Model 1 | |
---|---|
d | 9.190 |
(0.211) | |
Num.Obs. | 400 |
R2 | 0.932 |
R2 Adj. | 0.931 |
did_g2t2 <- df %>% dplyr::filter( (g == 1 & t <= 2) | (g == 2 & t <= 2) | (g == 3 & t <= 2) ) %>% lfe::felm( data = ., formula = y ~ d | g + t ) modelsummary::modelsummary(did_g2t2)
Model 1 | |
---|---|
d | 4.930 |
(0.165) | |
Num.Obs. | 600 |
R2 | 0.781 |
R2 Adj. | 0.780 |
did_g2t3 <- df %>% dplyr::filter( (g == 1 & t <= 3) | (g == 2 & (t == 1 | t == 3)) | (g == 3 & (t <= 2)) ) %>% lfe::felm( data = ., formula = y ~ d | g + t ) modelsummary::modelsummary(did_g2t3)
Model 1 | |
---|---|
d | 8.255 |
(0.192) | |
Num.Obs. | 700 |
R2 | 0.888 |
R2 Adj. | 0.887 |
did_g3t3 <- df %>% dplyr::filter( (g == 1 & t <= 3) | (g == 2 & t == 1) | (g == 3) ) %>% lfe::felm( data = ., formula = y ~ d | g + t ) modelsummary::modelsummary(did_g3t3)
Model 1 | |
---|---|
d | 9.199 |
(0.176) | |
Num.Obs. | 700 |
R2 | 0.905 |
R2 Adj. | 0.904 |
model_full <- lm(data = df, formula = y ~ -1 + as.factor(g):as.factor(t)) modelsummary::modelplot(model_full)
anova(model_ols, model_full)
## Analysis of Variance Table ## ## Model 1: y ~ I((g == 2) & (t == 2)) + I((g == 2) & (t == 3)) + I((g == ## 3) & (t == 3)) + as.factor(g) + as.factor(t) ## Model 2: y ~ -1 + as.factor(g):as.factor(t) ## Res.Df RSS Df Sum of Sq F Pr(>F) ## 1 892 890.91 ## 2 891 890.90 1 0.0071465 0.0071 0.9326