Search code examples
rstatisticsreturnfinance

TIme series data in R, problems with dates


Date        T1V        T2V        T3V         T1MV          T2MV         T3MV
1997-12-31  2.631202   2.201695  -0.660092  -0.77492483   0.282662305   4.66506798
1998-01-30  2.193793   3.763458   5.565432   3.50711734   2.874381814   5.14118430 
1998-02-27  5.173496   8.727646   6.333820   2.59892279   8.363146480   9.27289259

This is the table I am working with in R. It is much bigger. Data is on monthly basis up until 2014.The different columns are just the return dates on different portfolios. I always get errors if I want to use it as a time series data. I downloaded the PerformanceAnalytics package. For example for the SharpeRatio function it gives me.

> SharpeRatio(T1V)
Error in checkData(R) : 
The data cannot be converted into a time series.  If you are trying to passin names from a data object with one column, you should use the form 'data[rows, columns, drop = FALSE]'.  Rownames should have standard date formats, such as '1985-03-15'.

when you look at the date column in the table you see that the date format is exactly this format. I tried a hundred things. It also doesn^t let me plot the charts with lines only with points.

Any help is much appreciated.

> dput(FactorR[1:5,])
    structure(list(Date = structure(1:5, .Label = c("1997-12-31", 
   "1998-01-30", "1998-02-27", "1998-03-31", "1998-04-30", "1998-05-29", 
   "1998-06-30", "1998-07-31", "1998-08-31", "1998-09-30", "1998-10-30", 
   "1998-11-30", "1998-12-31", "1999-01-29", "1999-02-26", "1999-03-31", 
   "1999-04-30", "1999-05-31", "1999-06-30", "1999-07-30", "1999-08-31", 
   "1999-09-30", "1999-10-29", "1999-11-30", "1999-12-31", "2000-01-31", 
   "2000-02-29", "2000-03-31", "2000-04-28", "2000-05-31", "2000-06-30", 
   "2000-07-31", "2000-08-31", "2000-09-29", "2000-10-31", "2000-11-30", 
   "2000-12-29", "2001-01-31", "2001-02-28", "2001-03-30", "2001-04-30", 
   .
   .
   .
   , class = "factor"), 
    T1V = c(2.631202, 2.193793, 5.173496, 8.033864, 1.369065), 
    T2V = c(2.201695, 3.763458, 8.727646, 11.375482, 3.097196
    ), T3V = c(-0.660092, 5.565432, 6.33382, 20.608638, 4.022475
    ), T1MV = c(-0.774924835, 3.507117337, 2.598922792, 16.26945887, 
    4.544096701), T2MV = c(0.282662305, 2.874381814, 8.36314648, 
    12.7091841, 1.078742371), T3MV = c(4.665067984, 5.141184302, 
    9.27289259, 10.62133318, 2.791853987), T1BTM = c(0.617378168, 
    3.498582776, 3.332624722, 8.802164975, 1.366229683), T2BTM =      c(1.101407825, 
   5.578394125, 8.910685728, 20.05317039, 1.258609942), T3BTM = c(2.454019461, 
   2.445706552, 7.991651412, 10.79096755, 5.464002646), T1MOM = c(2.99986853, 
   4.982808153, 8.657010689, 10.60637296, 4.44333707), T2MOM = c(0.011102554, 
 3.184165606, 7.55229158, 11.9341773, 0.328377299), T3MOM = c(1.161834369, 
3.355709694, 4.025659592, 17.12665788, 3.55822744), Rm = c(1.390935, 
3.840895, 6.744987, 13.262647, 2.753486), SMB = c(-5.439992819, 
-1.634066965, -6.673969798, 5.648125694, 1.752242715), HML = c(-1.836641293, 
1.052876225, -4.65902669, -1.988802574, -4.097772963), MOM = c(1.838034161, 
1.62709846, 4.631351096, -6.520284921, 0.885109629)), .Names = c("Date", 
"T1V", "T2V", "T3V", "T1MV", "T2MV", "T3MV", "T1BTM", "T2BTM", 
"T3BTM", "T1MOM", "T2MOM", "T3MOM", "Rm", "SMB", "HML", "MOM"
), row.names = c(NA, 5L), class = "data.frame")

Solution

  • Two things are wrong:

    • Your Date column doesn't contain dates but factors.
    • SharpeRatio doesn't know how to convert your data.frame to a time series object.

    By doing the conversion manually, we can specify which column to use as time index and on-the-fly convert it to Date:

    library(PerformanceAnalytics)
    
    FactorR_xts <- xts(x = FactorR[, -1], # use all columns except for first column (date) as data
                       order.by = as.Date(FactorR$Date) # Convert Date column from factor to Date and use as time index
                       )
    
    SharpeRatio(FactorR_xts)