Closed iangow closed 1 year ago
@vickyyin1493 @shizhe75 See above.
We were wondering why are we renaming start_date as ex_day_index in the following code:
nyse_splits <- nyse_splits_raw %>% get_event_dates(pg, permno = "permno", event_date = "dclrdt") %>% inner_join(nyse_splits_raw, by = join_by(permno, dclrdt)) %>% rename(ex_day_index = start_date) %>% select(permno, ex_day_index, dclrdt, facshr, split, ex_day_index)
When we proceeded, this caused problems with the calculation for day_rel_ex as ex_day_index is a date rather than an integer.
ℹ In argument: day_rel_ex = day_index - ex_day_index
.
Caused by error in -.Date
:
! can only subtract from "Date" objects
Backtrace:
-.Date
(day_index, ex_day_index)@iangow @shizhe75
You might need to use get_event_rets()
, not get_event_dates()
to get relative_td
and rename that to ex_day_index
.
Alternatively, the function allows one to turn a date into a "trading date":
> farr::get_trading_dates(pg)
# A tibble: 25,549 × 2
date td
<date> <int>
1 1925-12-31 1
2 1926-01-02 2
3 1926-01-04 3
4 1926-01-05 4
5 1926-01-06 5
6 1926-01-07 6
7 1926-01-08 7
8 1926-01-09 8
9 1926-01-11 9
10 1926-01-12 10
# ℹ 25,539 more rows
# ℹ Use `print(n = ...)` to see more rows
Actually looking at your code, you might just use day_indexes
(inner_join()
should now work), as it's effectively the same thing as farr::get_trading_dates(pg)
.
This is related to #5, but more focused.
Here you need to zoom in on the specific issue. The following code does this. Nothing extraneous; just the code needed to reproduce the issue.
From the following, it seems that there are cases where
dclrdt
is not onday_indexes
. For example, the announcements occur on a weekend or another non-trading date. This is precisely the issue addressed by the code below, which comes from here. This was not discussed in the FFJR chapter, as it is introduced later.But there's a function
get_event_dates()
in thefarr
package that implements this. So you could use this.Using this gives you announcement dates for all rows.
Minimal code to produce issue
Created on 2023-06-08 with reprex v2.0.2
Code with correction
You can see at the bottom here that I have all rows without needing a
left_join
. If you check you will see no rows withNA
values inex_day_index
. This may be what is causing your later issue when you are usingleft_join
.Created on 2023-06-08 with reprex v2.0.2