Dhivya Narayanasamy
2017-Apr-27 06:51 UTC
[R] Problem in conversion of regulate time series and forecasting using Date Time [Timestamp values]:R
Hi, I am new to R. Kindly help me with the plot that gives wrong x-axis values. 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) 3) fitting <- auto.arima(gg.ts) #fitting the time series model using auto.arima 4) fore <- forecast(fitting, h=30, level = c(80,95)) #Forecasting 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. Also I tried to convert "gg.ts" to a regulate time series which throws error :> gg.xts <- ts(gg.ts, frequency = '1', start = ('2017-04-25 16:52:00'))Error in 1/frequency : non-numeric argument to binary operator Please help me how to use Date Time values in converting to regulate time series for forecasting. Regards> Dhivya[[alternative HTML version deleted]]
Jim Lemon
2017-Apr-27 21:49 UTC
[R] Problem in conversion of regulate time series and forecasting using Date Time [Timestamp values]:R
Hi Dhivya, I'm not that familiar with the "gg.ts" function, but you are passing character values to the "frequency" and "start" arguments. If there is no automatic conversion to numeric values, that would cause the error. Similarly, your "timestamps" variable may have been read in as a factor, which often causes trouble with date conversions. Try as.character(gg$timestamps) instead of just gg$timestamps. Jim On Thu, Apr 27, 2017 at 4:51 PM, Dhivya Narayanasamy <dhiv.shreya at gmail.com> wrote:> Hi, > I am new to R. Kindly help me with the plot that gives wrong x-axis > values. 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) > > 3) fitting <- auto.arima(gg.ts) #fitting the time series model using > auto.arima > > 4) fore <- forecast(fitting, h=30, level = c(80,95)) #Forecasting > > 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. > Also I tried to convert "gg.ts" to a regulate time series which throws > error : > >> gg.xts <- ts(gg.ts, frequency = '1', start = ('2017-04-25 16:52:00')) > Error in 1/frequency : non-numeric argument to binary operator > > Please help me how to use Date Time values in converting to regulate time > series for forecasting. > > > Regards >> Dhivya > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Dhivya Narayanasamy
2017-Apr-28 04:32 UTC
[R] Problem in conversion of regulate time series and forecasting using Date Time [Timestamp values]:R
Hi Jim, Thank you for the reply. 'gg.ts' is actually the object name of the time series I am using here. Also I have changed my timestamp class from factor to POSIXct (gg$timestamps <- as.POSIXct(gg$timestamps, format = "%Y-%m-%d %H-%M-%S") . When i plot this time series on graph, the x axis scales shows random value rather than showing timestamp value. Is there any way to correct the graph to make it show the timestamp value on x axis? I use plotly function for plotting. Regards| Mit freundlichen Gr??en, Dhivya Narayanasamy Contact No: +91-8438505020 On Fri, Apr 28, 2017 at 3:19 AM, Jim Lemon <drjimlemon at gmail.com> wrote:> Hi Dhivya, > I'm not that familiar with the "gg.ts" function, but you are passing > character values to the "frequency" and "start" arguments. If there is > no automatic conversion to numeric values, that would cause the error. > Similarly, your "timestamps" variable may have been read in as a > factor, which often causes trouble with date conversions. Try > as.character(gg$timestamps) instead of just gg$timestamps. > > Jim > > > On Thu, Apr 27, 2017 at 4:51 PM, Dhivya Narayanasamy > <dhiv.shreya at gmail.com> wrote: > > Hi, > > I am new to R. Kindly help me with the plot that gives wrong x-axis > > values. 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) > > > > 3) fitting <- auto.arima(gg.ts) #fitting the time series model using > > auto.arima > > > > 4) fore <- forecast(fitting, h=30, level = c(80,95)) #Forecasting > > > > 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. > > Also I tried to convert "gg.ts" to a regulate time series which throws > > error : > > > >> gg.xts <- ts(gg.ts, frequency = '1', start = ('2017-04-25 16:52:00')) > > Error in 1/frequency : non-numeric argument to binary operator > > > > Please help me how to use Date Time values in converting to regulate time > > series for forecasting. > > > > > > Regards > >> Dhivya > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]