Plotting Time Series data : R, Plotly, Timestamp values

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Plotting Time Series data : R, Plotly, Timestamp values

Dhivya Narayanasamy
hi,
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.

1) I am changing the class of "Timestamps" column from factor to as.POSIXct.

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

2) Now converting the dataframe to time series

gg.ts <- xts(x=gg$value, order.by = gg$timestamps)

3) Now fitting the time series model using auto.arima
     
fitting <- auto.arima(gg.ts)

4) Forecasting

fore <- forecast(fitting, h=30, level = c(80,95))

5) I am using plotly to this forecast model (Inspired from here : https://plot.ly/r/graphing-multiple-chart-types/#plotting-forecast-objects)

plot_ly() %>%
  add_lines(x = time(gg.ts), y = gg.ts,
            color = I("black"), name = "observed") %>%
  add_ribbons(x = time(fore$mean), ymin = fore$lower[, 2], ymax = fore$upper[, 2],
              color = I("gray95"), name = "95% confidence") %>%
  add_ribbons(x = time(fore$mean), ymin = fore$lower[, 1], ymax = fore$upper[, 1],
              color = I("gray80"), name = "80% confidence") %>%
  add_lines(x = time(fore$mean), y = fore$mean, color = I("blue"), name = "prediction")


The plot comes out wrong: 1) x axis labels are wrong. It shows some irrelevant values on axis. 2) the plot is not coming out.



I referred to this link (http://stackoverflow.com/questions/10302261/forecasting-time-series-data) for correcting the x axis lables. unfortunately it throws error for timestamp values, like this:

> gg.xts <- ts(new_gg, frequency = 1, start = (2017-04-25 16:52:00))

Error: unexpected numeric constant in "gg.xts <- ts(new_gg, frequency = 1, start = (2017-04-25 16"

Please help me. Thank you in advance. Regards,Dhivya