3

I have a data frame "gg", that looks like this:

> head(gg) 

           timestamps      value 
1 2017-04-25 16:52:00 -0.4120000 
2 2017-04-25 16:53:00 -0.4526667 
3 2017-04-25 16:54:00 -0.4586667 
4 2017-04-25 16:55:00 -0.4606667 
5 2017-04-25 16:56:00 -0.5053333 
6 2017-04-25 16:57:00 -0.5066667 

I need to plot this as a Time series data to do forecasting. The steps are as follows:

1) gg$timestamps <- as.POSIXct(gg$timestamps, format = "%Y-%m-%d %H-%M-%S") #changing "Timestamps" column 'factor' to 'as.POSIXct'.

2) gg.ts <- xts(x=gg$value, order.by = gg$timestamps) #converting the dataframe to time series (Non Regular Time series)

Now i want this gg.tsconverted to regular time series to do forecasting like this (Forecasting time series data) But i don't know how to add the timestamp values series to ts function. When i tried it throws error :

>  gg.xts <- ts(gg.ts, frequency = '1', start = c(2017-04-25 16:52:00,171))
Error: unexpected numeric constant in "gg.xts <- ts(gg.ts, frequency = '1', start = c(2017-04-25 16"

I have my whole problem listed clearly here. https://stackoverflow.com/questions/43627826/plotting-time-series-data-r-plotly-timestamp-values please help me. Thanks

Community
  • 1
  • 1
dhinar1991
  • 831
  • 5
  • 21
  • 40

1 Answers1

1

Before everything, you'll need to make sure you have the right classes for the data. Make sure gg$timestamps is in POSIXct form or date form (whichever you preference is)

gg$timestamps<- as.POSIXct(strftime(gg$timestamps,format = "%Y-%m-%d %H:%M:%S"))

All you need to do is generate a new data.frame with the time series values based on your specific interval and then merge the new data.frame with gg

tstamp <- data.frame(x = seq(head(gg$timestamps,1),tail(gg$timestamps,1),by = "sec"))
res <-merge(tstamp, gg, by.x="x",by.y="timestamps",all.x = TRUE)
xts(res$value,order.by = res$x)     # you create your xts time series this way.

Note: you'll have some NA values in it, as your original timeseries was an irregular time series.

Updated:

ArunK
  • 1,731
  • 16
  • 35
  • Hi thanks a lot for the reply. But I want this time-stamp values to sit on my x axis using "ts" (for example: 'x =xts (res,order.by = res$x) , x.ts = ts(x, freq=1, start=c(2017-04-25 16:48:00, 191))' . This x.ts throws error as i have mentioned above. The plot shows some random value on my graph in x axis. It is supposed to show the times-tamp value. Please refer to this [link] (http://stackoverflow.com/questions/10302261/forecasting-time-series-data) – dhinar1991 Apr 27 '17 at 09:48
  • The error you've got is because `start` takes in `numeric` as opposed to what you've entered. – ArunK Apr 27 '17 at 11:34
  • Is there a way to add these timestamp values to 'start' ? becoz i want these timestamp values as my x axis scales to the forecast plot. I am getting some random values sitting on the x axis scale. hope you get my point here. – dhinar1991 Apr 27 '17 at 12:14