Stock Prices Analysis part 3 – Exercises

August 9, 2016

(This article was first published on R-exercises, and kindly contributed to R-bloggers)


This is the third and the final part of the exercises dedicated to analysis of stock prices. In this part we will provide exercises for testing the type of distribution of stock prices and analysing and predicting stock prices using ARIMA models.

You dont need to be an expert stock’s trader in order to understand the examples, but you should go through part 1, since we shall use some of the solutions as starting point for some of the exercises.

The data set for the exercises is the same as for part 1 and you can download it here. If you solved the exercises from part 1, you can reuse data frames from exercise 6 and 7 as input for the exercises in part 3.

In this set of exercises we use two packages: tseries and forecast. If you haven’t already installed them, do it using the following code:

install.packages(c("tseries", "forecast"))

and before proceeding with the exercise set load them into the session using the following code:


Answers to the exercises are available here. If you have different solution, feel free to post it.

Exercise 1

Test for normality of the distribution of closing prices of YHOO using histogram.

Exercise 2

Test for normality of the distribution of closing prices of YHOO using normal q-q plot.

Exercise 3

Test for normality of distribution of closing prices of YHOO using Kolmogorov-Smirnov and Shapiro tests.

Based on four tests, what can you say about the closing prices of YHOO?

  1. Closing prices of YHOO are approximately normaly distributed
  2. The distribution of closing prices of YHOO does not conform to the normal distribution

Exercise 4

Plot on the same chart:

  1. Closing prices of GE
  2. 12 days moving average of the closing prices of GE
  3. 50 days moving average of the closing prices of GE

Exercise 5

Create ts object and display closing prices of BAC on correliogram. Is the time series stationary?

  • Yes
  • No

Exercise 6

Is the time series of the closing prices of BAC stationary when you differentiate it, based on the correliogram?

  • Yes
  • No

Exercise 7

Find the best fitting ARIMA model for closing prices of BAC.

Exercise 8

Display ACF graph for the residuals of the model from exercise 7.

Exercise 9

Use the first 80% of a time series of prices of BAC to make a prediction for the rest 20% of data. Save the values in a variable.

Exercise 10

Using data the exercise 9 and function accuracy from the forecast package, test the accuracy of the predictions. Is the prediction acceptable? (Tip: Prediction is acceptable if the field RMSA from the result of accuray is less than or equal to standard deviation of test data.).

To leave a comment for the author, please follow the link and comment on their blog: R-exercises. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)