Support for defining a function - aggregation, mean, t-test, table-output

I am conducting an Event study at the moment. I have like over and over again the same steps for the last parts of my study, therefore I would like to insert these steps into a function that I can just apply. The outputs should be presented in a table.

Here is a MRE of my data:

df = data.frame(
  Date_ = 1:15,
  ISIN = rep(c('CH00','CH00', 'GB00'),length.out=15,each=5),
  Return = c(0.0175, -0.0734, -0.0734, 0.0457, 0.0208,-0.0780496, 0.0688210, -0.0064685, -0.0997418, -0.0203781, 0.0056672, 0.0028146, 0.0366418, 0.0745412, 0.1555046),
  Rating_Change = c(0,0,0,1.9,0,0,0,-0.90, 0, 0, 0, 0, 3.66, 0, 0),
  Rating = c('A','A','A','A','A','A','A','A','A', 'A', 'B+','B+','B+','B+','B+'),
  event = c(0,1,1,1,1,2,2,2,2,2,1,1,1,1,1)
)
df

Then I apply the following function to filter for events, that are non-zero - in this example greater than zero.
For "s" I can define the number of the rating change.
For "w" I can define the window for the events.

event_up <- function(df, s, w) {
  data_filtered_up <- data.frame()
  for (i in 1:nrow(df)) {
    if (df[i, 4]>s) {
      data_filtered_up[i+w, 1:6] <- filter(df[i+w, 1:6])
      data_filtered_up           <- na.omit(data_filtered_up)
    }
  }
  return(data_filtered_up)
}

Now I have these steps to conduct (over and over again for different rating changes and different event windows):

TR_event_u_m3          <- event_up(df, 0, -3:-1) #0 defines that the rating change is greater than 0, "-3:-1" defines the pre-event window (3 to 1 month before event)
TR_CAR_u_m3            <- aggregate(.~TR_event_u_m3$ISIN, data=TR_event_u_m3, mean) #aggregating the ISINs to calculate cumulative abnormal returns 
TR_CAR_u_m3            <- TR_CAR_u_m3[,-2][,-2] #deleting non-necessary columns 
TR_CAAR_u_m3           <- mean(TR_CAR_u_m3$AR) #calculate the mean of all cumulative average abnormal returns (CAAR) pre event window
TR_CAAR_u_m3_t         <- t.test(TR_CAR_u_m3$AR) #make t-test for the CAAR
TR_CAAR_table[1,1]     <- TR_CAAR_u_m3 #put CAAR value in table
TR_CAAR_table[2,1]     <- TR_CAAR_u_m3_t$statistic #put t-test for CAAR value in table

The output shall be in a specific table, where the rownames describe the event windows with always following the corresponding t-test:

TR_CAAR_table           <- matrix(NA, 8, 8)
colnames(TR_CAAR_table) <- c("GSE_U", "E_U", "S_U", "G_U", "GSE_D", "E_D", "S_D", "G_D")
rownames(TR_CAAR_table) <- c("t= -3:-1","t-test -3:-1","t= -1:+1","t-test -1:+1","t= 0:+1","t-test 0:+1","t= +1:+3","t-test +1:+3")

Is there a way to integrate the above steps (aggregation, the deleting of the non-necessary columns, the mean, t-test results and the correct placement in the table) into the above mentioned function(event_up), so that I only have to apply the function and not type the steps over and over again for each event window/rating change?

I tried several attempts but there were always errors occurring and I really don't know why. So any help and support is very much appreciated!!
Thank you so much in advance, Antonia

Can you show what you tried, and what errors came up?

PS: Thanks for including a nice and small example dataset!

Hello @antonia,

I see that your previous queries https://community.rstudio.com/t/how-can-i-apply-lm-to-specific-event-for-the-pre-event-window/104946 and https://community.rstudio.com/t/event-id-pre-and-post-window-in-r/105059 are not yet answered to your satisfaction. Can you indicate why?

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.