ARIMA produced slope straight line - time-series

I am new to the time series with using the SARIMA model, and I followed the tutorial to build the model and trying to forecast the future trend. The thing goes well at the beginning but when produced the results it shows the slope straight line. and I build it on the Jupyter NoteBook
First thing I checked my data, and make the data visually, but in fact, it seems the right data then I tried to change the values of P, D, Q and failed again
https://github.com/Dongmingguoguo/Prediciton
https://github.com/Dongmingguoguo/Prediciton
Here is the GitHub link please go to check and my expectancy is to make the forecast for the next 3days.

This happen when your data doesn't have strong seasonality and model finds difficult to predict the future there fore it simply take average of your previous values and predict as future. There fore you are getting straight line.

Related

How to compare forecast based on the data of raw series itself and that of the raw series integrated of order 1?

I have a dataset of close prices, lets say Close which after doing the unit root tests I decided to go ahead with two different models, in eviews the equations will be as follows:
Close c ar(1)
and
d(Close) ma(5)
the first model takes a constant and an AR(1) process while the second one takes MA(5) process with 1st differenced series.
After getting the forecasts for both, my question is how can I compare them and decide whihc forecast is better and why? RMSE and some other statistucs are supposed to be scaler and I wonder if differencing affects this scale. The output for both the forecasts along with statistics is as shown in the picture: the two forecasts
Thankyou for your help

Does the training+testing set have to be different from the predicting set (so that you need to apply a time-shift to ALL columns)?

I know the general rule that we should test a trained classifier only on the testing set.
But now comes the question: When I have an already trained and tested classifier ready, can I apply it to the same dataset that was the base of the training and testing set? Or do I have to apply it to a new predicting set that is different from the training+testing set?
And what if I predict a label column of a time series (edited later: I do not mean to create a classical time series analysis here, but just a broad selection of columns from a typical database, weekly, monthly or randomly stored data that I convert into separate feature columns, each for one week / month / year ...), do I have to shift all of the features (not just the past columns of the time series label column, but also all other normal features) of the training+testing set back to a point in time where the data has no "knowledge" interception with the predicting set?
I would then train and test the classifier on features shifted to the past by n months, scoring against a label column that is unshifted and most recent, and then predicting from most recent, unshifted features. Shifted and unshifted features have the same number of columns, I align shifted and unshifted features by assigning the column names of the shifted features to the unshifted features.
p.s.:
p.s.1: The general approach on https://en.wikipedia.org/wiki/Dependent_and_independent_variables
In data mining tools (for multivariate statistics and machine learning), the dependent variable is assigned a role as target variable (or in some tools as label attribute), while an independent variable may be assigned a role as regular variable.[8] Known values for the target variable are provided for the training data set and test data set, but should be predicted for other data.
p.s.2: In this basic tutorial we can see that the predicting set is made different: https://scikit-learn.org/stable/tutorial/basic/tutorial.html
We select the training set with the [:-1] Python syntax, which produces a new array that contains all > but the last item from digits.data: […] Now you can predict new values. In this case, you’ll predict using the last image from digits.data [-1:]. By predicting, you’ll determine the image from the training set that best matches the last image.
I think you are mixing up some concepts, so I will try to give a general explanation for Supervised Learning.
The training set is what your algorithm LEARNS on. You split it in X (features) and Y (target variable).
The test set is a set that you use to SCORE your model, and it must contain data that was not in the training set. This means that a test set also has X and Y (meaning that you know the value of the target). What happens is that you PREDICT f(Y) based on X, and compare it with the Y you have, and see how good your predictions are
A prediction set is simply new data! This means that usually you DO NOT have a target, since the whole point of supervised learning is predicting it. You will only have your X (features) and you will predict f(X) (your estimate of the target Y) and use it for whatever you need.
So, in the end a test set is simply a prediction set for which you have a target to compare your estimation to.
For time series, it is a bit more complicated, because often the features (X) are transformations on past data of the target variable (Y). For example, if you want to predict today's SP500 price, you might want to use the average of the last 30 days as a feature. This means that for every new day, you need to recompute this feature over the past days.
In general though, I would suggest starting with NON time series data if you're new to ML, as Time Series is much harder in terms of feature engineering and data management and it is easy to make mistakes.
The question above When I have an already trained and tested classifier ready, can I apply it to the same dataset that was the base of the training and testing set? has the simple answer: No.
The question above Do I have to shift all of the features has the simple answer: Yes.
In short, if I predict a month's class column: I have to shift all of the non-class columns also back in time in addition to the previous class months I converted to features, all data must have been known before the month in that the class is predicted.
This also means: the predicting set has to be different from the dataset that contains the testing set. If you included the testing set, the training set loses valuable up-to-date data of the latest month(s) available! The term of a final "predicting set" is meant to be the "most current input to be used without a testing set" to get the "most current results" for the prediction.
This is confirmed by the following overview offered by this user who seems to have made the image, using days instead of months here, but the idea is the same:
Source: Answer on "Cross Validated" - Splitting Time Series Data into Train/Test/Validation Sets; the whole Q/A is recommended (!).
See the last line of the image and the valuable comments of that answer on "Cross Validated" to understand this.
230106:
The image shows that the last step is a training on the whole dataset, this is the "predicting set" that is the newest and that does not have a testing set.
On that image, there is one "mistake" which shows that this seemingly easy question of taking former labels as features for upcoming labels seems to be hard to be understood. I myself did not see this and posted the image without this remark: The "T&V" is in the past of the "Test". And that would be a wrong validation for a model that shall predict the future, the V must be in the "future" test block (unless you have a dataset that is not dynamically changing over time, like in physics).
You would have to change it to a "walk-forward" model, with the validation set - if at all - split k-fold from the testing set, not from the training set. That would look like this:
See also:
Can / should I use past (e.g. monthly) label columns from a database as features in an ML prediction (no time-series!)? with the "walk-forward" main image,
Splitting Time Series Data into Train/Test/Validation Sets with more insight into this and the comment that brought up the model name "walk-forward".

ARIMA Nodes in KNIME how to use?

I'm new to KNIME and trying to use ARIMA for extrapolation of my time series data. But I've failed to make ARIMA Predictor to do it's work.
Input data are of the following format
year,cv_diff
2011,-4799.099999999977
2012,60653.5
2013,64547.5
2014,60420.79999999993
And I would like to predict values for example for 2015 and 2016 years.
I'm using String to Date/Time node to convert year to date. In ARIMA Learner I can choose only cv_diff field. And this is the first question: for option 'Column containing univariate time series' should I set year column or variable that I'm going to predict? But in my case I have only one option - cv_diff variable. After that I connect Learner's output with ARIMA Predictor's input and execute. Execution is failing with ' ERROR ARIMA Predictor 2:3 Execute failed: The column with the defined time series was not found. Please configure the node anew.'
Help me to understand which variable should I set for Learner and Predictor? Should it be non-timeseries variable? And how then Arima nodes will understand which column to use as time series?
You should set the cv_diff as the time series variable and connect the input to the predictor too. (And do not try to set too large values for the parameters as with so little data points, learning will not work.)
Here is an example:
Finally, I've figured it out. Option 'Column containing univariate time series' for ARIMA Learner node seems little bit confusing especially for those unfamiliar with time series analysis. I should't have provided any time series field explicitly, because ARIMA treats variable on which it is going to make prediction as collected in equal time intervals and it doesn't matter what kind of intervals they are.
I've found a good explanation of what 'univariate time series' means
The term "univariate time series" refers to a time series that
consists of single (scalar) observations recorded sequentially over equal time increments. Some examples are monthly CO2 concentrations and southern
oscillations to predict el nino effects.
Although a univariate time series data set is usually given as a single column of numbers, time is in fact an implicit variable in the time series. If the data are equi-spaced, the time variable, or index, does not need to be explicitly given. The time variable may sometimes be explicitly used for plotting the series. However, it is not used in the time series model itself.
So, I should choose cv_diff variable for both Learner and Predictor and do not provide any timestamps or any other time related columns.
One more thing that I didn't understand. That I should train on some series of data and then provide another SERIES for which I want predictions. That is little bit different from other Machine Learning workflows when you need to provide only new data and there is no notion of series at all.

What ML package (RNN model) has the capacity to predict with data of less time steps than training data

(Since my original question is probably not getting an answer because of too specific about one package, I will ask another general.)
According to the RNN Model, we have an input and output for every step. Let's say a model trained with data of 6 time steps. Of course if I use test data of 6 time steps, I will get outputs, and I have succeed in that. But theoretically, if I only have data of first 3 time steps, I should get an output from the 3rd output node too (without re-train a model with first 3 time steps). But I found at least "keras" package can't do this.
Is there any packages that support such prediction? Better in python language and better to have LSTM layer.
As far as I understand your problem you have two options, depending on your goal.
You can pad sequences in the end with zeros so they are the correct dimension but in the output you just use the first n steps according to your test data dimension.
You can use a stateful implementation

Using Random Forest for time series dataset

For a time series dataset, I would like to do some analysis and create prediction model. Usually, we would split data (by random sampling throughout entire data set) into training set and testing set and use the training set with randomForest function. and keep the testing part to check the behaviour of the model.
However, I have been told that it is not possible to split data by random sampling for time series data.
I would appreciate if someone explain how to split data into training and testing for time series data. Or if there is any alternative to do time series random forest.
Regards
We live in a world where "future-to-past-causality" only occurs in cool scifi movies. Thus, when modeling time series we like to avoid explaining past events with future events. Also, we like to verify that our models, strictly trained on past events, can explain future events.
To model time series T with RF rolling is used. For day t, value T[t] is the target and values T[t-k] where k= {1,2,...,h}, where h is the past horizon will be used to form features. For nonstationary time series, T is converted to e.g. the relatively change Trel. = (T[t+1]-T[t]) / T[t].
To evaluate performance, I advise to check the out-of-bag cross validation measure of RF. Be aware, that there are some pitfalls possibly rendering this measure over optimistic:
Unknown future to past contamination - somehow rolling is faulty and the model using future events to explain the same future within training set.
Non-independent sampling: if the time interval you want to forecast ahead is shorter than the time interval the relative change is computed over, your samples are not independent.
possible other mistakes I don't know of yet
In the end, everyone can make above mistakes in some latent way. To check that is not happening you need to validate your model with back testing. Where each day is forecasted by a model strictly trained on past events only.
When OOB-CV and back testing wildly disagree, this may be a hint to some bug in the code.
To backtest, do rolling on T[t-1 to t-traindays]. Model this training data and forecast T[t]. Then increase t by one, t++, and repeat.
To speed up you may train your model only once or at every n'th increment of t.
Reading Sales File
Sales<-read.csv("Sales.csv")
Finding length of training set.
train_len=round(nrow(Sales)*0.8)
test_len=nrow(Sales)
Splitting your data into training and testing set here I have considered 80-20 split you can change that. Make sure your data in sorted in ascending order.
Training Set
training<-slice(SubSales,1:train_len)
Testing Set
testing<-slice(SubSales,train_len+1:test_len)

Resources