General Instructions

In the first part of the seminar we will see how to create a time series object as well as how to perform a decomposition analysis. A big difference with the analyses we have performed so far is that when dealing with time series analysis the DataFrame structure is not suitable. For time series analysis, we have to create a time series object. The first goal of the seminar is to understand how to create a time series object. This can be done with the function ts().

Step 1: Import Data

Download the iphonesales.csv from Minerva. This file contains monthly iphone sales for the period from third quarter of 2007 until the fourth quarter of 2016.

Task: Import the data on R studio.

Step 2: Create Time Series

For the time series data, you have to define the vector of the values that we want to create a time series. Then, to define the initial observation and the last observation. This is done with the arguments start and end . Another required argument is the frequency. The frequency very much relates to the time intervals. In our case, the first observation is the third quarter of 2007, and the last observation is the fourth quarter of 2016. The frequency is 4. For monthly observations the frequency will be 12. If you use frequency 12 then the ts will understand that the first observation will be the March of 2007 instead of the Q3 of 2007.

Task: Create the time series object. I have named it ipts

ipts <- ts(data$Sales,start=c(2007, 3), end=c(2016, 4), frequency=4)

If you have done correct then the object should have the following form

ipts 
##       Qtr1  Qtr2  Qtr3  Qtr4
## 2007              0.27  1.12
## 2008  2.32  1.70  0.72  6.89
## 2009  4.36  3.79  5.21  7.37
## 2010  8.74  8.75  8.40 14.10
## 2011 16.24 18.65 20.34 17.07
## 2012 37.04 35.06 26.03 26.91
## 2013 47.79 37.43 31.24 33.80
## 2014 51.03 43.72 35.20 39.27
## 2015 74.47 61.17 47.53 48.05
## 2016 74.78 51.19 40.40 45.51

Step 3: Plot Time Series

let us plot the time series data to see how it looks like.

Task: Plot the time series using the function plot()

plot(ipts)

You can see that now we have the observations plotted across time.

plot(ipts)

For the next step, we have to install the package forecast because we are going to use a function which is called MA(), and this function belongs to the forecast package.

Step 4: Install Package

Task: Install and load the package forecast

Step 5: Calculate Moving Average Smoother

What the function does is to calculate the moving average. You can type the help of the ma function and you can see that this belongs to the forecast package. MA computes a simple moving average smoother for a given time series. The syntax takes 3 arguments, the time series, the order, and an argument center (True or False).Center equals True is for even orders.

Task: Calculate the moving average for an order 4. Call the new object masales

masales<-ma(ipts, 4)

Step 6: Check The Differences

Check the new object. What do you observe about the number of observation compared to the initial time series?

masales<-ma(ipts, 4)

We don’t have values for the the third quarter of 2007 and the fourth quarter of 2007 and I don’t have values also for the last two periods, the third and the fourth quarter of 2016.

Let’s see how it differs from the original time series by plotting the following

plot(ipts)
lines(masales, col="red", lwd=3)

As I have said, this dampens the fluctuations, so we don’t have these big ups and downs and in many cases, this help us to find the right trend behind the time series.

Step 7: Additive Decomposition

So what the decomposition does, the first step is, since we have calculated the trend, is to remove the trend from the original time series object. However, there are functions in R that do this and all of the steps altogether. So, a function that is going to decompose, the time series is the function decompose.

The syntax is once again to use the time series, the original time series and to select the type of decomposition, if you remember, we have additive and multiplicative. So, if I select additive and plot the decomposition, this is going to, show this figure where the time series has been decomposed into different components.

Task: Use additive decomposition

de <- decompose(ipts, type=“additive”)

Now Plot the decomposition with the function plot.

plot(de)

Lets plot now the original data adding also lines with the trend and seasonal components.

Step 8: plot data with trend and seasonal components

plot(ipts)
lines(de$trend, col=2)
lines(de$seasonal, col=3)

Step 9: Multiplicative Decomposition

Decompose with multiplicative method and see how the plot now differs.

de <- decompose(ipts, type="multiplicative")                
plot(de)

Forecasting Future Sales

Now that we have decomposed the time series into trend, seasonal, and residual components, we can use this information to forecast future values.

Step 10: Extract Trend Component

To forecast future sales, we start by extracting the trend component from the decomposition. Since some trend values might be NA due to the smoothing process, we remove them.

trend_values <- na.omit(de$trend)  
time_index <- 1:length(trend_values)

Step 11: Fit a Linear Model

We fit a linear regression model to the trend component to capture its pattern over time.

trend_model <- lm(trend_values ~ time_index)

Step 12: Predict Future Trend

We extend the trend for the next 8 quarters (2 years) and generate future trend predictions.

future_time <- (length(trend_values) + 1):(length(trend_values) + 8)
future_trend <- predict(trend_model, newdata = data.frame(time_index = future_time))

Step 13: Extract and Extend Seasonality

We extract the last full seasonal cycle and repeat it for the forecasted period.

seasonal_pattern <- tail(de$seasonal, frequency(ipts))  # Get last seasonal cycle
future_seasonal <- rep(seasonal_pattern, length.out = length(future_time))

Step 14: Compute Final Forecast

Using the trend and seasonal components, we compute the final forecast using a multiplicative model.

future_forecast <- future_trend * future_seasonal

Step 15: Create a Time Series Object

We structure the forecast into a time series format.

future_ts <- ts(future_forecast, start=c(2017, 1), frequency=4)

Step 16: Plot the Forecast

Finally, we plot the original time series along with the forecasted values to visualize the trend.

plot(ipts, xlim=c(2007, 2020), ylim=range(ipts, future_forecast), 
     main="iPhone Sales Forecast", ylab="Sales", xlab="Year")
lines(de$trend, col="blue", lwd=2)  # Existing trend
lines(future_ts, col="red", lwd=2, lty=2)  # Forecasted values

Step 17: Display Forecasted Values

To inspect the forecasted values, we print them.

future_forecast
##        1        2        3        4        5        6        7        8 
## 77.43709 61.49730 49.47431 66.42526 87.14285 68.97102 55.30960 74.03544

Part 2: Analyse stock market data in R

In the first part of the seminar you will perform a technical analysis of the stock market (time series) data in R. To start you will need to select several public companies (i.e. companies that trade on the major stock exchanges) to investigate. Yahoo Finance https://uk.finance.yahoo.com/ and google finance https://www.google.co.uk/finance may help you find some companies to examine. A few companies and their tickers are listed below:

Trading Ticker Company Name
GE General Electric
PG Proctor and Gamble
MSFT Microsoft
PFE Pfizer
AMD Advanced Micro Devices
AAPL Apple
DELL Dell Computers
GRPN Groupon
FB Facebook
CSCO Cisco Systems
INTC Intel
EZJ Easyjet
BP BP
HSBA HSBC
RR Rolls Royce
MKS Marks and Spencer

A trading ticker name is a unique name which can be used to identify a stock. You will need this to collect the data on the company. To collect the data in R we will use the quantmod package https://www.quantmod.com/ which you will need to install and load first:

Now we can download data for the companies you are interested using the following command:

getSymbols('AAPL', src="yahoo", from=as.Date("2018-01-01"), to=as.Date("2019-01-01"))

This will download data for Apple (AAPL), you can modify the ticker name and dates above to download data for any company and time period. If you call back the name of your stock this should return a time series object:

You will see that six columns of information are received: AAPL.Open – The opening price of the stock on each day. AAPL.High - The highest price of the stock on each day. AAPL.Low - The lowest price of the stock on each day. AAPL.Close - The closing price of the stock on each day. AAPL.Volume - The amount of shares bought/sold of the stock on each day. AAPL.Adjusted - The closing price after adjustments for all applicable splits & dividend distributions

You can visualise all this information using a candlestick chart:

candleChart(AAPL, up.col = "black", dn.col = "red", theme = "white")

A black candlestick indicates a day where the closing price was higher than the open (i.e. a gain) and a red candlestick indicates a day where the open was higher than the close (i.e. a loss). The candleChart function allows you to add a simple moving average with: addSMA(n = 10)

However, if we wanted to do this ourselves with the SMA function we could use the following code with the close data:

s10 <- SMA(AAPL[, “AAPL.Close”], n=10)

Then we can plot this with plot(AAPL[, “AAPL.Close”], main = “Apple Stock”) and lines(s10, col=2). You should now see your closing stock data with a smoothed simple moving average line in red.

Similarly we can look at a 10-day weighted moving average with the following code:

w10 <- WMA(AAPL[, "AAPL.Close"], n=10, w= c(0.4,0.3,0.1,0.07,0.05,0.03,0.02,0.01,0.01,0.01))


plot(AAPL[, "AAPL.Close"], main = "Apple Stock") 
lines(w10, col=2)

Next, we are going to use Bollinger Bands. These are composed of three lines which can be used to indicate the direction and strength of a trend. The middle line by default is just a simple moving average (set at 20-day) and the upper and lower bands are the +/- the standard deviation. For more information on Bollinger Bands take a look at: https://www.fidelity.com/learning-center/trading-investing/technical-analysis/technical-indicator-guide/bollinger-bands

To start, replot the candlestick chart with:

candleChart(AAPL, up.col = “black”, dn.col = “red”, theme = “white”)

Then we can add the Bollinger Bands with:

addBBands(sd = 2, maType = “SMA”, draw = ‘bands’)

Does your share price ever touch the upper and lower bands? If it touches the lower band it usually means selling activity will be high and if it touches the upper band it usually means purchasing activity will be high. So with the example share price above, it is likely at the most recent observation purchasing activity will be high.

The addBBands function also allows you to set the type of moving average method you wish to use. To use weighted moving average try: addBBands(sd = 2, maType = “WMA”, draw = ‘bands’)