1

I have a dataset that has daily prices from Jan 1 2009 to Jan 1 2019 and I want to transform it into a time series. When I use monthly data, the ts() function works as expected:

> head(monthlyts)
     Jan    Feb    Mar    Apr    May    Jun    Jul    Aug    Sep    Oct Nov    Dec
1999 0.8811 0.8854 0.9251 0.8940 0.8746 0.8521 0.8522 0.8799 0.9143 0.8951 0.9123 0.8862
2000 0.8665 0.8934 0.8900 0.8709 0.8463 0.8185 0.8319 0.8266 0.8677 0.8697 0.8346 0.8575

but when I try it with daily prices it appears completely differently:

> head(dailyts)
Time Series:
Start = 2009 
End = 2009.01368925394 
Frequency = 365.25 
  Price
[1,] 0.8990
[2,] 0.8990
[3,] 0.9014
[4,] 0.9004
[5,] 0.9041
[6,] 0.8986

The code I'm using for both is the same so I'm not sure what the issue is.

monthlyts <- ts(mprices['Price'], frequency=12, start=c(2009,1))

dailyts <- ts(dprices['Price'], frequency=365.25, start=c(2009,1))

There's no change in the data either, both .csv files are dl'd from the same website and are the same timeframe, just one is monthly and one is daily.

Any ideas on how to get the daily time series properly?

Here's some test data that's representative of the problem

data <- as.data.frame(sample(seq(from=0, to=1, by=0.0001), size = 730, replace = TRUE))
colnames(data) <- 'data'
datats <- ts(data, frequency=365, start=c(2009,1))
head(datats)

It should output two rows of data labelled 2009 and 2010 with 365 columns in each row.

Y Ahmed
  • 157
  • 1
  • 1
  • 6
  • Looks like the ts function is not especially well-suited for daily data. See here for a better explanation: https://stackoverflow.com/questions/8437620/analyzing-daily-weekly-data-using-ts-in-r/8438069#8438069 – Andrew Mar 03 '19 at 18:07
  • Possible duplicate of [Analyzing Daily/Weekly data using ts in R](https://stackoverflow.com/questions/8437620/analyzing-daily-weekly-data-using-ts-in-r) – Andrew Mar 03 '19 at 18:09
  • Thanks for the link, I had missed that thread. I had considered the issue of leap years and had made my `frequency=365.25` to combat it but perhaps that doesn't work. I'll try removing a day from each leap year to see if that changes things and report back. – Y Ahmed Mar 03 '19 at 18:50
  • Absolutely, I saw that and it is too bad the ts function doesn’t account for leap years already. Alternatively, it looks like there may be other packages. If you post a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) people will be able to help you out. – Andrew Mar 03 '19 at 19:07
  • Removed the leap day from the relevant years but still no luck. I've added some example code to the question if anyone can figure it out. – Y Ahmed Mar 04 '19 at 16:07

0 Answers0