Search code examples
reventsanalysis

Managing nested loops or loop with lapply


my question may seem trivial but I can not get a solution. I have a list containing 5 dataframes named by stock (my real list contains 250 stocks). Each dataframe has the same number and name of columns:

  1. Date: time series formatted as character
  2. returns: time series of stock returns
  3. DJSTOXX: time series of index returns
  4. EPS: Earnings announcement dates, which contains also NAs, formatted as characters.

For each dataframe I would like to run the evReturn function from the ererpackage, which returns a list with different measures regarding cumulative abnormal returns around certain dates. The main inputs of the function are y which should be a dataframe, containing a column for dates (time series), a column with returns, a column with index returns and a column with the earning announcement dates. As you can see below, this code run for the firm NESN the event analysis for each dates where an announcement has been made. Here I set for each date in the time series equal to the EPS dates, omitting NAs, do the function evReturn. It saves the output in hh3 which is a list where for each EPS date the event return is computed.

hh3 <- list()
for (i in na.omit(dflist$NESN$Date[dflist$NESN$Date == dflist$NESN$EPS])){
  hh3[[i]] <- evReturn(y = dflist$NESN, firm = "return", event.date = i, y.date = "Date",
                       index = "DJSTOXX", event.win = 2, est.win = 50, digits = 3)
}

Now my question is: How can I set it in order to run this code for each stock in the list? Therefore, I am expecting the resulting list to have 5 list (1 for each stock) with a list for each EPS dates.

As you can see in the evReturn function I explicitly set $NESN but I want to set it as for each dataframe in dflist do the function.

I have tried with lapply the following:

lapply(dflist, function(x){
  for (i in na.omit(x$Date[x$Date == x$EPS])){
    dflist2[[i]] <- evReturn(y = x, firm = "return", event.date = i, y.date = "Date",
                             index = "DJSTOXX", event.win = 2, est.win = 50, digits = 3)
  }})

But it returns:

Error in xj[i] : only 0's may be mixed with negative subscripts

I thus tried to nest 2 loops as:

for(j in seq_along(dflist)){
  for (i in na.omit(dflist$j$Date[dflist$j$Date == dflist$j$EPS])){
    hh7[[j]][i] <- evReturn(y = dflist$j, firm = "return", event.date = i, y.date = "Date",
                         index = "DJSTOXX", event.win = 2, est.win = 50, digits = 3)
  }}

But it returns hh7 as a list of length 0.

Any help is highly appreciated since it seems I am missing something.

Thank you


Solution

  • Hard to be sure how to help because you don't provide any sample data, but you should be able to do something like this:

    get_ev_return <- function(d) {
      dates = na.omit(d[d$Date = d$EPS, "Date"])
      lapply(dates, \(date) {
        evReturn(y=d,firm="return", event_date=date, y.date = "Date", index="DJSTOXX", event.win=2, est.win=50, digits=3)
      })
    }
    
    lapply(dflist, get_ev_return)
    

    Before running the lapply(), test get_ev_return() on NESN, and see if it works for one frame. Also, you'll need to check that dates within the get_ev_return() function contains any dates.. Maybe you have one or more data.frames where there are no rows where Date == EPS.... Again, this is the problem with not providing any sample data in your question.