For Loop for Event Study using the EvReturn Function from R Package "Erer"

StackOverflow https://stackoverflow.com/questions/23328453

  •  10-07-2023
  •  | 
  •  

سؤال

forgive me if this question is trivial for some of you but I am a complete newbie to R...

I want to use the function "evReturn()" from the R Package "erer". It is a function to calculate cumulative abnormal returns and looks like this for one firm and one market index:

hh <- evReturn(y = dataset, firm = "Firm1", 
y.date = "date", index = "Index1", est.win = 250, 
digits = 3, event.date = 19990505, event.win = 5)

This works just fine if I have a matrix with 3 columns (Date, Stock returns of firm 1 and index return of index 1).

Now the case is a bit more complicated. I have appr. 3000 firms and 3000 different indices. So in column 1, I have the dates. In columns 2-3001, I have the stock returns for all firms. In columns 3002-6001 I have the returns of the indices.

I thought I could run the evReturn() function for all firms and all indices with a for loop like this

 > for(i in 2:3001) 
   {hh <- evReturn(y=numt,firm=i,event.date=20140102,y.date="Date",
   index=i+3000,event.win=2,est.win=230,digits=3)}

But it only returns

Error in [.data.frame`(y, (loca - event.win - est.win):
(loca - event.win -  : undefined columns selected

I am aware of several posts that resolve the error of "undefined columns selected" but none helps here. Has anyone an idea how I could create a for loop to let the evReturn() run through all my columns?

Any help is appreciated!

Cheers, Paul

هل كانت مفيدة؟

المحلول

The problem is, that firm as well as index have to be the corresponding column names of your data frame, not the column indices. You can access the i-th column name of data frame numt with names(numt)[i]. So your for loop should look something like

for(i in 2:3001) {
  hh <- evReturn(y=numt,firm=names(numt)[i],event.date=20140102,y.date="Date",
  index=names(numt)[i+3000],event.win=2,est.win=230,digits=3)
}

I want to mention that this loop overwrites hh at every iteration step. But since this could be intentional and you do some additional computation inside the loop, I'll leave it that way.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top