irudnyts / estudy2

An Implementation of Parametric and Nonparametric Event Study
http://irudnyts.github.io/estudy2/
14 stars 6 forks source link

Converting CSV of results into S3:returns object for statistical tests #13

Closed willnicolle closed 2 years ago

willnicolle commented 2 years ago

Hi!,

Thanks for getting back to my email. As requested, I'm replying with my issue on here.

I have coded up a FamaFrench model on Python which is working, and I'd like to use the results in R and apply statistical tests to them from estudy2. I understand I need to create an object similar to that returned by the apply_market_model() function (ie, a list object with at least dates, normal, abnormal and predicted returns).

My question is this: How can I import the results into R (e.g. from a csv) and reformat them so that they are suitable to be passed to the parametric and non-parametric statistical tests?

I've attached some sample data (random) in the format I can export the CSVs in.

EDIT: I've got to the point where I can import all the relevant CSVs into R and reformat them as per the picture below, but the parametric/nonparametric test throws this error: _Error in if (list_of_returns[[i]]$estimation_end >= eventstart) { : argument is of length zero --> I guess I need to reformat the data so the function knows how to deal with it. Any help greatly appreciated!

Thanks, Will

ISIN3.csv ISIN2.csv ISIN1.csv

Screenshot of my object that is trying to mimic the S3 object returned by apply_market_model image

willnicolle commented 2 years ago

SOLVED - using the below code to import the outputs of the F3FF model and restructure them for the stats tests.

import FF3F outputs as list of CSVs

temp = list.files(pattern="*csv") #create list of csvs is relevant file

Function applied to each CSV of company returns and restructure them as needed

imp_funct2 <- function(x) { c(data <- read.csv(x, header = TRUE), data3 <- list(observed = zoo::zoo(data[,3], data[,2]),#list columns as zoo object - NOTE: Position of columns depends on your data predicted = zoo::zoo(data[,1], data[,2]), #ditto abnormal = zoo::zoo(data[,4], data[,2]), #ditto estimation_start = as.Date('2019-03-07'), #set manually estimation_end = as.Date('2019-06-12'), #set manually estimation_length = 100), #set manually class(data3) <- "returns", #define class as 'returns' return(data3)) }

securities_returns = lapply(temp, imp_funct2) #apply function to returns csvs

eg - now this can be passed to statistical tests

CARs <- car_rank_test( securities_returns, car_start = as.Date('2019-06-13'), car_end = as.Date('2019-06-27'), percentage = 90 )