-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path1f. Rejections.R
190 lines (167 loc) · 6.93 KB
/
1f. Rejections.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
library(tidyverse)
library(fixest)
library(ggthemes)
library(patchwork)
library(furrr)
library(fastDummies)
library(progressr)
# set plot theme
theme_set(theme_clean() + theme(plot.background = element_blank(),
legend.background = element_blank()))
# load in compustat data
comp <- read_rds(here::here("Data", "simulation_data.rds"))
# estimate the fixed effects regression of ROA on firm and year fixed effects
mod <- feols(roa ~ 1 | gvkey + fyear, cluster = "incorp", data = comp)
# get firm and years and state of incorporation
shell <- comp %>% select(gvkey, fyear)
# get the firm and year fes, as well as the standard deviation of ROA
firm_fes <- fixef(mod)$gvkey
n_firm_fes <- length(fixef(mod)$gvkey)
year_fes <- fixef(mod)$fyear
n_year_fes <- length(fixef(mod)$fyear)
sd_roa <- sd(comp$roa)
## Show that 0 treatment effects will end up significant with heterogeneity
run_sim <- function(i, p) {
# need this for progress bar to work
p()
# pull firm FE from empirical distribution with replacement and
# randomly assign state of incorporation
sim_firm_fe <- tibble(
gvkey = unique(shell$gvkey),
firm_fe = sample(firm_fes, n_firm_fes, replace = TRUE),
incorp = sample(state.abb, n_firm_fes, replace = TRUE)
)
# pull year FE from the empirical distribution with replacement
sim_year_fe <- tibble(
fyear = unique(shell$fyear),
year_fe = sample(year_fes, n_year_fes, replace = TRUE)
)
# merge in the FE to the firm/year/state observations and add in residuals from the
# empirical distribution. ROA is the linear combination of the FEs and the residual
data <- shell %>%
left_join(sim_firm_fe, by = "gvkey") %>%
left_join(sim_year_fe, by = "fyear") %>%
mutate(resid = sample(mod$residuals, length(mod$residuals), replace = TRUE),
roa = firm_fe + year_fe + resid)
# randomly assign the state of incorporation into treatment groups
random_states <- sample(state.abb, length(state.abb), replace = FALSE)
# function for different trend decay rates
get_t <- function(val) {
# simulate treatment effects centered at 0
taus <- rnorm(3, 0, val*sd_roa)
# put data together
dt <- data %>%
mutate(
# figure out treatment group based on random ordering of states of incorporation
group = case_when(
incorp %in% random_states[1:17] ~ 1989,
incorp %in% random_states[18:35] ~ 1998,
incorp %in% random_states[35:50] ~ 2007
),
# add in treatment effects - varying percent of standard deviation of ROA added per year
delta_base = case_when(
fyear >= group & group == 1989 ~ taus[1],
fyear >= group & group == 1998 ~ taus[2],
fyear >= group & group == 2007 ~ taus[3],
TRUE ~ 0
),
# true treatment effect is the cumulative sum of this - dynamic trend break treatment effect
# vary the power variable which represents the decay rate
delta = if_else(delta_base == 0, 0, delta_base * (fyear - group + 1)),
# new ROA is the sum of the old ROA and the treatment effect
treat_roa = roa + delta,
# make indicator variable for obs when treatment is turned on for the TWFE regs
treat = ifelse(fyear >= group, 1, 0),
# make a relative-to-treatment year variable
rel_year = fyear - group,
# get a first treat variable for CS
first_treat = group)
feols(treat_roa ~ treat | gvkey + fyear, cluster = "incorp", data = dt) %>%
broom::tidy(conf.int = TRUE) %>%
select(statistic) %>%
# add in variables for the size of the break, and the amount of decay
mutate(val = val)
}
# estimate for a sequence of values
map_dfr(seq(0, 0.1, by = 0.0025), get_t) %>%
mutate(sim = i)
}
# parallelize and do 500 simulations
x <- 1:500
options(future.globals.maxSize= 891289600)
set.seed(20210915)
plan(multisession, workers = 6)
with_progress({
p <- progressor(steps = length(x))
out <- future_map_dfr(
.x = x,
.f = run_sim,
p = p,
.options = furrr_options(globals = c("mod", "shell", "firm_fes", "n_firm_fes",
"year_fes", "n_year_fes", "sd_roa"),
packages = c("tidyverse", "fixest", "e1071", "did", "fastDummies"),
seed = TRUE))
})
# make a plot that we can highlight under the curve with tstats bw -1.96 and 1.96
# first get the data for the full curve using ggplot_build
p1data <- ggplot_build(
out %>%
mutate(insig = if_else(abs(statistic) < 1.96, 1, 0)) %>%
filter(val == 0.03) %>%
ggplot(aes(x = statistic)) + geom_density()
)$data[[1]]
# get percent insignificant
percent_insig <- out %>%
filter(val == 0.03) %>%
mutate(insig = if_else(statistic %>% between(-1.96, 1.96), 1, 0)) %>%
summarize(m = mean(insig)) %>%
pull(m)
# make label for the plot
plotlabel <- glue::glue(scales::label_percent(accuracy = .1)(percent_insig),
" insignificant \n t-Stats")
# now make the plot - gray for the whole curve, red for between
# -1.96 and 1.96
p1 <- p1data %>%
ggplot(aes(x = x, y = y)) +
geom_area(fill = "#4B5F6C", color = "darkgray", alpha = 1/3) +
geom_area(
data = p1data %>% filter(x %>% between(-1.96, 1.96)),
aes(x = x, y = y),
fill = "#A7473A",
alpha = 3/4
) +
annotate("label", x = 14, y = 0.045, label = plotlabel,
size = 2) +
geom_curve(aes(x = 14, y = .05, xend = 0, yend = 0.055),
color = "#A7473A",
arrow = arrow(length = unit(0.03, "npc"))) +
scale_x_continuous(breaks = c(-20, -10, -1.96, 0, 1.96, 10, 20),
labels = c(-20, -10, -1.96, 0, 1.96, 10, 20)) +
scale_y_continuous(limits = c(0, 0.06)) +
labs(x = "t-Statistic", y = "Density") +
theme(axis.title.y = element_text(hjust = 0.5, vjust = 0.5, angle = 360, size = 8),
axis.title.x = element_text(size = 8),
axis.text = element_text(size = 5))
# plot 2
p2data <- out %>%
mutate(insig = if_else(abs(statistic) < 1.96, 1, 0)) %>%
group_by(val) %>%
summarize(insig = mean(insig))
p2 <- p2data %>%
ggplot(aes(x = val, y = insig)) +
geom_line() +
geom_hline(yintercept = 0.95, color = "#A7473A", linetype = "dashed") +
labs(x = "Percent of ROA Standard Deviation", y = "Percent \n Insignificant") +
theme(axis.title.y = element_text(hjust = 0.5, vjust = 0.5, angle = 360,
size = 8),
axis.title.x = element_text(size = 8),
axis.text = element_text(size = 5)) +
scale_x_continuous(breaks = seq(0, .1, length.out = 5),
labels = scales::percent) +
scale_y_continuous(limits = c(0, 1),
labels = scales::percent)
# save
ggsave(p1, filename = here::here("Figs_Tables", "rejection_plots_1.png"),
dpi = 800, width = 4, height = 3)
ggsave(p2, filename = here::here("Figs_Tables", "rejection_plots_2.png"),
dpi = 800, width = 4, height = 3)