1

I am new to R and have been trying to create a ts object for the stock market hourly data on which I can apply time series analysis.

an example of my data looks like below (I'll take hourly volume data for a stock in this case):

                   Date Volume
1   2018-03-01 10:30:00 143432
2   2018-03-01 11:30:00  93522
3   2018-03-01 12:30:00 152178
4   2018-03-01 13:30:00 117424
5   2018-03-01 14:30:00 268167
6   2018-03-01 15:30:00 245504
7   2018-03-01 15:59:00 288977
8   2018-03-02 10:30:00 230484
9   2018-03-02 11:30:00 265244
10  2018-03-02 12:30:00 183313
11  2018-03-02 13:30:00 130850
12  2018-03-02 14:30:00 139846
13  2018-03-02 15:30:00 257797
14  2018-03-02 15:59:00 261628
15  2018-03-05 10:30:00 140620
16  2018-03-05 11:30:00 171228
17  2018-03-05 12:30:00 118685
18  2018-03-05 13:30:00 107209
19  2018-03-05 14:30:00 116918
20  2018-03-05 15:30:00 225035

above data was created using:

temp <- read.csv("somefile.csv")

what I wanted to create was a ts object that captures the right frequency of the data so I could plot them correctly and analyse them using time series analysis. For example I can decompose the trend, seasonality from the ts object.

I've done some googling and stackoverflowing, I couldn't see any post mentioning same thing. I have tried using method in this post, but to no avail. I can't create a seq that's cutomised to 7 hours a day.

what is a correct way to do this in r?

stucash
  • 1,078
  • 1
  • 12
  • 23
  • not very clear, but i understand that you have a time series that is irregularly sampled (with 7h non missing per day), you can't use most of the classic ts methods. maybe check this https://stackoverflow.com/q/12623027/3871924. But if you have evidence that the night is irrelevant to your data generating process, you might skip it completely and treat it as such, setting the frequency to 7 as you suggest. – agenis May 31 '18 at 21:48
  • @agenis indeed; the night is irrelevant as the stock market is only open for hours that's illustrated in the snippet. if I set it to 7, I think the `Date` column is then irrelevant as well ? – stucash May 31 '18 at 21:55
  • 1
    ts is normally used for monthly, quarterly or yearly data. Use zoo or xts for hourly data. – G. Grothendieck May 31 '18 at 21:55
  • @G.Grothendieck thanks; I managed to create xts object by `xtemp <- xts(x = temp$Volume, order.by = as.POSIXct(temp$Date))`. but plotting looks very unsmooth. – stucash May 31 '18 at 22:00
  • Use `chart_Series` in quantmod. – G. Grothendieck May 31 '18 at 22:08
  • @G.Grothendieck thanks for the help! – stucash May 31 '18 at 22:10

0 Answers0