Agenda: Analyzing a particular Sector within the S&P500 Index We have chosen to deeply analyze the HEALTH TECHNOLOGY Sector.
S&P 500 Data - PRELIMINARY SETUP
We will continue our analysis of the S&P 500. Load the data, as described in the chapter Live Case: S&P500 (1 of 3)
# Read S&P500 stock data present in a Google Sheet.library(gsheet)prefix <-"https://docs.google.com/spreadsheets/d/"sheetID <-"11ahk9uWxBkDqrhNm7qYmiTwrlSC53N1zvXYfv7ttOCM"url500 <-paste(prefix,sheetID) # Form the URL to connect tosp500 <-gsheet2tbl(url500) # Read it into a tibble called sp500
No encoding supplied: defaulting to UTF-8.
Rename columns, as described in the chapter Live Case: S&P500 (1 of 3).
suppressPackageStartupMessages(library(dplyr))# Define a mapping of new column namesnew_names <-c("Date", "Stock", "StockName", "Sector", "Industry", "MarketCap", "Price", "Low52Wk", "High52Wk", "ROE", "ROA", "ROIC", "GrossMargin", "OperatingMargin", "NetMargin", "PE", "PB", "EVEBITDA", "EBITDA", "EPS", "EBITDA_YOY", "EBITDA_QYOY", "EPS_YOY", "EPS_QYOY", "PFCF", "FCF", "FCF_QYOY", "DebtToEquity", "CurrentRatio", "QuickRatio", "DividendYield", "DividendsPerShare_YOY", "PS", "Revenue_YOY", "Revenue_QYOY", "Rating")# Rename the columns using the new_names vectorsp500 <- sp500 %>%rename_with(~ new_names, everything())
Remove Rows containing no data or Null values, as described in the chapter Live Case: S&P500 (1 of 3).
# Check for blank or null values in the "Stock" columnhasNull <-any(sp500$Stock ==""|is.null(sp500$Stock))if (hasNull) { # Remove rows with null or blank values from the dataframe tibble sp500 <- sp500[!(is.null(sp500$Stock) | sp500$Stock ==""), ]}
The S&P500 shares are divided into multiple Sectors. Thus, model Sector as a factor() variable, as described in the chapter Live Case: S&P500 (1 of 3).
sp500$Sector <-as.factor(sp500$Sector)
Stock Ratings: The S&P500 shares have Technical Ratings such as {Buy, Sell, ..}. Model the data column Rating as a factor() variable, as described in the chapter Live Case: S&P500 (1 of 3).
sp500$Rating <-as.factor(sp500$Rating)
Low52WkPerc: Create a new column to track Share Prices relative to their 52 Week Low, as described in the chapter Live Case: S&P500 (1 of 3).