-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhhi.R
48 lines (35 loc) · 1.07 KB
/
hhi.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
# HHI
library(tidyverse)
load("~/Desktop/final_tix.rda")
# Calculate HHIs
temp_HHI <- NULL
for (y in unique(final_tix$year)) {
for (q in unique(final_tix$quarter)) {
temp_HHI <- final_tix %>%
filter(year == y, quarter == q) %>%
group_by(route, TkCarrier) %>%
summarise(car_pass = sum(db1b_pass),
.groups = "keep") %>%
ungroup() %>%
group_by(route) %>%
mutate(tot_pass = sum(car_pass), s2 = (car_pass/tot_pass)^2) %>%
summarise(year = y,
quarter = q,
HHI = sum(s2),
.groups = "keep")
temp_HHI <- rbind(temp_HHI, temp_HHI)
if (y == 1993 & q == 1) {
hhi <- temp_HHI
} else {
load("~/Desktop/final_tix.rda")
hhi <- rbind(hhi,temp_HHI)
}
}
}
# Join hhi to final_tix
hhi$concat <- paste(hhi$route, hhi$year, hhi$quarter)
hhi <- hhi[!duplicated(hhi$concat),]
final_tix <- final_tix %>%
inner_join(hhi %>% select(-concat),
by = c("route", "year", "quarter"))
save(final_tix, file = "~/Desktop/final_tix.rda")