0

I have a data.frame where each column contains the monthly returns on various stocks since 2014 and the first column contains the observation date. I need to access the returns for those stocks on specific dates (say 2018-01-31, or between 2014-02-03 and 2018-01-01) in order to compute several statistics.

DATE APPLE GM
2018-05-11 0.1070360773 0.41666667
2018-05-12 0.0566735467 -0.0123546

When I calculate the covariance matrix for the returns on specific date ranges, the assignment works correctly. However, when I try to perform the same operation for one particular date in order to multiply that row of returns by another vector I get NAs

data.returns$Date<-as.Date(data.returns$Date,format="%Y-%m-%d")
data.returns_m$Date<-as.Date(data.returns_m$Date,format="%Y-%m- 
%d")
# defining monthly dates to compute returns
date_enddec17<-as.Date("2017-12-29")
date_endjan18<-as.Date("2018-01-31")
date_endfeb18<-as.Date("2018-02-28")
date_endmar18<-as.Date("2018-03-29")
date_endapr18<-as.Date("2018-04-30")
date_endmay18<-as.Date("2018-05-31")
date_endjun18<-as.Date("2018-06-29")
date_endjul18<-as.Date("2018-07-31")
date_endaug18<-as.Date("2018-08-31")
date_endsep18<-as.Date("2018-09-28")
date_endoct18<-as.Date("2018-10-31")
date_endnov18<-as.Date("2018-11-30")
date_enddic18<-as.Date("2018-12-31")

date_list <- c(date_enddec17, date_endjan18, date_endfeb18, 
date_endmar18, date_endapr18, date_endmay18, date_endjun18, 
               date_endjul18, date_endaug18, date_endsep18, 
date_endoct18, date_endnov18, date_enddic18)

# initialising the lists for the statistics
Covariance <- list()
W_gmv <- list()
ret_m <- list()

# for loop to calculate the statistics for each date
local <- 0
for (i in date_list) {
  local <- local + 1
  Covariance[[local]] <- 
cov(na.omit(data.returns[data.returns$Date>=i-365*4 & 
data.returns$Date<=i,2:ncol(data.returns)]))
      W_gmv[[local]<-(solve(Covariance[[local]])%*%e)%*%solve(t(e)%*%solve(Covariance[[local]])%*%e)

      ret_m[[local]] <- 1 + t(as.matrix(as.numeric(data.returns_m[data.returns_m$Date==i+30,-1]))) %*%W_gmv[[local]]

}

I expected to obtain a list (ret_m) with 13 numbers, each of them representing the monthly return for that specific date, instead I got a list of 13 NAs. Surprisingly this didn't happen with the covariance calculation (first command in the loop), which worked perfectly fine.

  • Welcome to Stack Overflow. Read and include features from [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example?rq=1), especially the part about reproducing and pasting your data. The `data.returns` dataset is not available for us to demonstrate with; think of using a smaller example that can be included in your question. – wibeasley Feb 13 '19 at 17:48
  • just added a sample cut of my dataset at the beginning of the description – Jose Suarez-Lledo Feb 13 '19 at 18:03
  • the entire data set is a bit large to be reproduced here but the structure is the same – Jose Suarez-Lledo Feb 13 '19 at 18:03

0 Answers0