This is the second part of the exercises dedicated to analysis of stock prices. In this part we will provide exercises for plotting, fitting linear model and predicting stock prices.

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 use data frames from exercise 6 and 7 as input for the exercises in part 2.

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

**Exercise 1**

Plot a polygon showing closing prices of stock X for the last 90 trading days.

**Exercise 2**

Suppose you have equal amounts of each stock in the data frame. Calculate and plot the average daily return of your portfolio. (Tip: *daily return on stock is calculated in part 1, exercise 7.*)

**Exercise 3**

Plot pairwise scatter plots comparing returns on all stocks in data set.

**Exercise 4**

Fit a linear model of the form `Y=a+bX`

to closing prices of YHOO and plot it on a polygon together with actual closing prices of YHOO.

**Exercise 5**

How much of the variation in closing prices of GE is explained by the linear model of the form `Y=a+bX`

that fits to daily returns of GE.(*Tip: you need to calculate r ^{2}.*)

**Exercise 6**

When you fit a simple linear model of the form `Y=a+bX`

to closing prices of YHOO in 2016, is the coefficient b statistically significant on the level of `α=0.05`

?

- Yes
- No

**Exercise 7**

Find the linear model `Y=a+bX`

that fits to closing prices of GOOG in 2016.

**Exercise 8**

Calculate 95% confidence interval for linear model `Y=a+bX`

that fits to closing prices of GOOG in 2016. Save the value in a variable for later use.

**Exercise 9**

With fit linear model `Y=a+bX`

, predict GOOG closing price with 95% confidence interval for ten days ahead of the last recorded price, based on closing prices in 2016. Save the predicted values in a variable for later use.

**Exercise 10**

Plot on the same graph:

- the closing price of GOOG in 2016
- fitted values for model
`Y=a+bX`

for closing prices of GOOG in 2016 (from exercise 7) - 95% confidence interval for model
`Y=a+bX`

for closing price of GOOG in 2016 (from exercise 8) - prediction of GOOG closing price for next ten days with 95% confidence interval (from exercise 9)

Image by Maklay62 (Pixabay post) [CC0 Public Domain ], via Pixabay.

**What's next:**

- Explore all our (>1000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below

Anonymous says

Hey

In the solution to Question 9, solutions have used interval=”prediction”. When I have already fit a linear model, I know what the mean value is. So, why can’t I use interval=”confidence”

Miodrag Sljukic says

It depends what you are looking for. Confidence intervals tell you about how well you have determined the mean. On the other hand, prediction intervals tell you where you can expect to see the next data point sampled. Here you can find some interesting points on this discussion.