Browsing resource, all submissions are temporary.
The daily stock market prices are available on many internet sites. Here is a data file containing the daily stock price for the Coca-Cola Company. It was downloaded from Yahoo! Finance. The file has data up to December 30, 2016. As usual, you can download the file and place it in your R's working directory, and then load it using the command
stock <- read.csv("KOStock.csv")
Explore the data.
a. Add a column named "Change" to the data frame stock, which is the percentage of the change of the stock's closing price (in column "Close") compared to the closing price in the previous trade day (note that time is decreasing down the rows). For example, the closing prices in the first four rows of the data are stock$Close[1:4] = 41.46, 41.6, 41.39, 41.61, corresponding to the closing prices on 2016-12-30, 2016-12-29, 2016-12-28 and 2016-12-27. So set
stock
stock$Close[1:4]
stock$Change[1] = (41.46 - 41.6)/41.6 * 100 = -0.3365
stock$Change[2] = (41.6 - 41.39)/41.39 * 100 = 0.5074
stock$Change[3] = (41.39 - 41.61)/41.61 * 100 = -0.5287
and so on. We cannot do the calculation in the last row since we don't have data on the previous trade day. Set "Change" to NA in the last row. A prototype of the code is as follows.
n <- nrow(stock) # number of rows stock$Change <- NA # fill in the calculation of stock$Change in rows 1 – n-1
Make sure your first 3 rows match the values given above.
A positive value of "Change" means that the price went up. What is the proportion of days where the price went up in the data (not counting the NA in the last row, of course)? Give your answer to 4 decimal places.
b. On which date did the percentage of the price drop the most? What is the percentage of the drop? Give your second answer to 2 decimal places.
Percentage of the maximum drop (keep the minus sign) = (minimum of stock$Change)% = %
Enter the date in the form YYYY-MM-DD, e.g. 2016-01-09. Don't include quotes.
Date of maximum drop:
c. Consider a naive model of predicting whether the price will go up or not based on the values of "Change" in the previous 2 trade days: predict the price going up if the sum of "Change" in the previous 2 days is positive. For example, since stock$Change[2] + stock$Change[3] < 0, this model predicts that the price goes down on 2016-12-30.
stock$Change[2] + stock$Change[3]
Add a column named "UpModel" to the data frame. Set the value to "TRUE" if the model predicts the price goes up; otherwise set it to "FALSE". Since there are not enough data to do the calculation in the last three rows, set the values to NA in the last three rows. A prototype of the code is as follows.
stock$UpModel <- NA # fill in the calculation of stock$UpModel in rows 1 – n-3
Make sure to check that stock$UpModel[1] is FALSE, as explained above.
stock$UpModel[1]
What is the proportion of days where the model predicts the price going up? Give your answer to 4 decimal places.
d. The model gives a correct prediction if stock$UpModel is TRUE when stock$Change>0 or stock$UpModel is FALSE when stock$Change<=0. What is the proportion of days where the model gives correct predictions? Give your answer to 4 decimal places.
stock$UpModel
stock$Change>0
stock$Change<=0
e. Is the accuracy of this model's prediction much better than random guessing? yes no