A reproducible code in R + a short .pdf report with commented tables and graphs. The two files should be named STUDENT1ID STUDENT2ID.R and STU- DENT1ID [login to view URL], where STUDENT1ID and STUDENT2ID are student IDs of the members of the group. The files should be uploaded to a designated area in the [login to view URL] website.
Analysis of data of stocks in SP500. Your tasks are the following.
1. Get the data. First, you should set your individual first and the last date for your
data analysis. This is done by
[login to view URL](STUDENT1ID)
[login to view URL] <- [login to view URL]("2018-03-31") - 365 - sample(1:20,1)
[login to view URL](STUDENT2ID)
[login to view URL] <- [login to view URL]("2018-03-31") + sample(1:20,1)
One possible way to get the data is from the R library BatchGetSymbols and run the following code:
[login to view URL] <- GetSP500Stocks()
tickers <- [login to view URL]$tickers
[login to view URL] <- BatchGetSymbols(tickers = tickers,
[login to view URL] = [login to view URL],
[login to view URL] = [login to view URL]) The object [login to view URL] has now the data for SP500 stocks.
Another possibility is to use a package library(pdfetch) and get the selected data (adjusted close for individual stocks) by calling
yahoo <- pdfetch_YAHOO(tickers, fields = "adjclose",
from=[login to view URL], to = [login to view URL])
2. We are interested just in the adjusted closing price value. Organize the data in a table, where rows represent the dates and columns represent the individual stocks. The first column should be the date, the following columns should have a name that correspond to the tickers. It helps that you may get the data using two alternative procedures (and compare the resulting two tables). Clean the data (address missing values if any) and make sure that each entry in the table corresponds to the correct date and the correct ticker. Note that some data entries may be missed in download, or the dates may not be 100% consistent for all tickers. As a part of the exercise, save the corresponding table in a .csv file.
3. Perform a basic mean - variance analysis. From the table of the closing prices, create a table representing log returns. Avoid the use of for loops. Instead, apply vector operations (such as log, diff, apply). Compute the averages and standard deviations for each stock. Properly treat any missing values. Plot a frequency histogram for the averages with a density fit. Add a fit based on normal distribution. Do the same for the standard deviations. Run a command summary on both the vector of the averages and the vector of standard deviations. Plot a graph on (x, y) axis with points representing the standard deviation x and the average y for each stock. Add a ticker name to each point (this can be done by a command textxy from library(calibrate)).
4. Perform a basic CAPM analysis for each stock in your SP500 data. For each stock, run regression lm(y ⇠ x), where y is a vector of returns of a given stock and x is a vector of returns of a given benchmark, in our case the SP500 index. This means in particular that you need to get the values of the index itself (ticker bGSPC) and compute its returns. Make sure that the dates of the returns are consistent, otherwise the data would be shifted and the entire analysis would give irrelevant results. Store the resulting coe cients representing ↵ and for each stock together with the P- value corresponding to the coe cient ↵. Plot the histogram of the vector of betas with the density fit and the fit based on a normal distribution. Identify the stocks at both extremes, the ones with the smallest and the largest betas. Plot the histogram of the vector of alphas with the density fit. Identify the stocks with statistically significant alphas (from the corresponding P-values). Lastly, choose one stock that you find interesting, and plot the joint returns of the stock and the index together with the corresponding regression line.
13 pekerja bebas membida secara purata €135 untuk pekerjaan ini
Hi, I'm familiarized with R studio analysis. I read the project content and it looks intereseting. I only need data and project instruction/details to start working on it. Regards.