Time Series Prediction Using R

I have the following R code

library(forecast) value <- c(1.2, 1.7, 1.6, 1.2, 1.6, 1.3, 1.5, 1.9, 5.4, 4.2, 5.5, 6, 5.6, 6.2, 6.8, 7.1, 7.1, 5.8, 0, 5.2, 4.6, 3.6, 3, 3.8, 3.1, 3.4, 2, 3.1, 3.2, 1.6, 0.6, 3.3, 4.9, 6.5, 5.3, 3.5, 5.3, 7.2, 7.4, 7.3, 7.2, 4, 6.1, 4.3, 4, 2.4, 0.4, 2.4) sensor<-ts(value,frequency=24) fit <- auto.arima(sensor) LH.pred<-predict(fit,n.ahead=24) plot(sensor,ylim=c(0,10),xlim=c(0,5),type="o", lwd="1") lines(LH.pred$pred,col="red",type="o",lwd="1") grid() 

Received graph prediction

But I'm not happy with the prediction. Is there a way to make the forecast look like its previous trend of values ​​(see. Chart)?

+7
source share
4 answers

As you defined the frequency as 24, I assume that you work 24 hours (daily) per cycle and therefore have approximately 2 cycles in your historical dataset. Generally speaking, this is limited sample data to initiate time series forecast. I would recommend getting a little more data, and then again I can create a forecasting model. The more data you have, the better it will take into account seasonality and thus predict future values. With limited automatic algorithms available, such as auto.arima, often something like moving averages is used by default. Your data set deserves something better than moving averages, since there is a certain seasonality in the cycle. There are a number of prediction algorithms that can help you improve the shape of a straight curve; things like holt-winters or other exponential smoothing methods can help. However, auto.arima is also a good bet (I would first try to see what I can do about it).

Getting more data and doing the same procedure will improve your chart. Personally, I prefer to use forecast over predict ; the data seems to look a little better than the chart, as it shows your confidence intervals. In the code, I also expanded the data set a bit by copying two periods, so that we got four periods. See Result below:

 library(forecast) value <- c(1.2,1.7,1.6, 1.2, 1.6, 1.3, 1.5, 1.9, 5.4, 4.2, 5.5, 6.0, 5.6, 6.2, 6.8, 7.1, 7.1, 5.8, 0.0, 5.2, 4.6, 3.6, 3.0, 3.8, 3.1, 3.4, 2.0, 3.1, 3.2, 1.6, 0.6, 3.3, 4.9, 6.5, 5.3, 3.5, 5.3, 7.2, 7.4, 7.3, 7.2, 4.0, 6.1, 4.3, 4.0, 2.4, 0.4, 2.4, 1.2,1.7,1.6, 1.2, 1.6, 1.3, 1.5, 1.9, 5.4, 4.2, 5.5, 6.0, 5.6, 6.2, 6.8, 7.1, 7.1, 5.8, 0.0, 5.2, 4.6, 3.6, 3.0, 3.8, 3.1, 3.4, 2.0, 3.1, 3.2, 1.6, 0.6, 3.3, 4.9, 6.5, 5.3, 3.5, 5.3, 7.2, 7.4, 7.3, 7.2, 4.0, 6.1, 4.3, 4.0, 2.4, 0.4, 2.4) sensor <- ts(value,frequency=24) # consider adding a start so you get nicer labelling on your chart. fit <- auto.arima(sensor) fcast <- forecast(fit) plot(fcast) grid() fcast Point Forecast Lo 80 Hi 80 Lo 95 Hi 95 3.000000 2.867879 0.8348814 4.900877 -0.2413226 5.977081 3.041667 3.179447 0.7369338 5.621961 -0.5560547 6.914950 3.083333 3.386926 0.7833486 5.990503 -0.5949021 7.368754 3.125000 3.525089 0.8531946 6.196984 -0.5612211 7.611400 3.166667 3.617095 0.9154577 6.318732 -0.5147025 7.748892 

graph with four periods

+15
source

auto.arima () returns the best ARIMA model according to AIC, AICc or BIC value. Based on your β€œvalue” dataset, he probably chose the ARMA (1,0) or AR (1) model, which, as you can see, tends to return very quickly. This will always happen with the AR (1) model in the long run, and therefore it is not very useful if you want to predict more than a couple of steps forward.

You can see how you can choose a different type of model by analyzing the acf and pacf data of your value. Then you will need to check if your alternative model is suitable for data.

+1
source

You might want to use Moving Average to more accurately display forecasts.

0
source

I suggest looking at the ltp package at https://code.google.com/p/ltp/ you can also look at the web interface at https://github.com/matteoredaelli/predictoR

-3
source

All Articles