**R-exercises**, and kindly contributed to R-bloggers)

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.

**leave a comment**for the author, please follow the link and comment on their blog:

**R-exercises**.

R-bloggers.com 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...