Skip to main content

Simple Time Series Models as primitive forecast methods

Let's start with what is Time Series data. According to the famous book of Rob J Hyndman "Forecasting: Principles & Practice" it is a sequences of observations collected over time. One of the characteristic of Time series is that there is successive order for values in opposite to vector where a unique ID doesn't necessarily provide a specific order to the data. Forecasting however is estimating how the sequence of observations will continue into the future according to the book of Hyndman.

As we can imagine we can find Time Series data in various sector of business life and managing people would like to know what we can expect in the nearest future. It is crucial because by knowing in advance that something is coming we can prepare better and omit some losses. 

R program has develop Time Series forecasting pretty well. We can build advanced ARIMA models as well as Exponential Smoothing or even Neutral network models and more. Today I would like to focus on some basic primitive methods like Mean method, Naïve method, Seasonal naïve method, Drift method which works well with random data set. You can use this methods with library 'forecast' so it is better to install it right away. What is it all about?

Mean Method - returns the mean as forecast value with function meanf()

Naïve Method - returns the last observation as forecast value with function naive()

Seasonal Naïve Method - returns the last observation of the seasonal stage with function snaive()

Drift Method- carries the change over first and last observation into the future with function rwf()

Let's see how to use it now in practice. We will start with using simple time series methods on some random data set and most of the time those method are helpful in this type of data where there is no trend, no seasonality and other statistical characters of data.

library(forecast)
set.seed(60) # We want to make results reproducible

We are taking 400 randomly distributed numbers. Time series starts in Q1 of 1900 and it is quarterly data.

randomtseries= ts(rnorm(400),start=c(1900,1),frequency=4)
plot(randomtseries)

It is how random distributed data looks like. There is no particular trend, no seasonality. Now we are defining our 3 models by using functions I mentioned above. Model forecast will have 15 observations (parameter h).

meanrtsmodel<-meanf(randomtseries,h=15)
naivertsmodel<-naive(randomtseries,h=15)
driftrtsmodel<-rwf(randomtseries,h=15)

Now, each object contain the original data and 15 forecast values. We can plot original data and our " models in the same graph.



Comments

Popular posts from this blog

Random number generators, reproducibility and sampling with dplyr

Let's assume that you want to take some random observations from your data set. Dplyr helps you with the function sample_n(). To make your code reproducible you seed the ID of a “random” set of values. You need to indicate number of rows you want to extract and specify if the rows should be replaced or not. To show you how it works I will use again mtcars dataset which is included in your base R program. Let's see first six rows of this data frame.  library(dplyr) data("mtcars") head(mtcars)                    mpg cyl disp  hp drat    wt  qsec vs am gear carb Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4 Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4 Datsun 710        22.8   4  108  93 3.85 2.320 18.61...

Ggplot2 for data visualizations

 When I have started my adventure with R, immediately I've noticed that everybody was taking about ggplot2 and its importance.  Tap "ggplot2"  in google and check it by yourself.  You will see a lots of professional fancy graphs, articles, blogs and other great materials.  I was so impressed  that I was even trying to start my learning of R programming from ggplot2.  Soon I understood, that I needed some basics first and it is better to take time if you are starting from zero. Before jumping to the ggplot2 structure I will share with you some tips I find useful. First it is good to remember that there are some steps while you explore your data. Most of the time you have to collect data first,  do some pre-processing and exploration,  modelling & analysis and only after comes visualization. Of course in previous steps,  graphs also can be helpful to interpret the situation correctly however it is important that you have prepared, clea...

Basic Statistics for Time Series

What we can say about the time series data at the beginning? How we can describe it and what elements determinate the method we will use to forecast the data? For my own personal use I have prepared some notes which help me to answer questions above. I was using some definitions from the book of "Forecasting: Principles & Practice" by Rob J Hyndman like also some other blog's article like: https://towardsdatascience.com/descriptive-statistics-in-time-series-modelling Basic Statistics for Time Series When you make sure that your data has time series class, you can check the data with the basic functions we have in R. ts() is useful to build Time Series from scratch. mean() shows the average of a set of data. median() shows the middle value of the arranged set of data. plot() shows on the graph how the Time series looks like sort() sort the data quantile() function returns quantiles which are cut points dividing the range of a probability distribution into continuous ...