# 1. Install quantmod if needed
# install.packages('quantmod')
# 2. Load quantmod library
library(quantmod)
# 3. Get price data for security
tsla = getSymbols('TSLA', src='google', from= '2016-07-20', auto.assign=FALSE)
# 4. Take a look at the first few rows of downloaded data
head(tsla)
# 5. Clean up data to focus on what we are interested in
tsla_close = tsla$TSLA.Close
# 6. Verify the new data source
head(tsla_close)
# 7. Calculate the daily change in price close-to-close, store in data.fame
# store column as matrix to avoid numerical calculation errors
tsla_change = as.data.frame(as.matrix(diff(log(tsla_close))))
# 8. Confirm data transformation
head(tsla_change)
# 9. Get quick table of summary statistics
summary(tsla_change)
# 10. More comprehensive summary statistics with psych library, transpose table
# decribe function returns the most common descriptives but there are many other packages
# and functions to explore if you need something else
# default on trimmed mean is .1 --> excludes upper/lower 10% of obs
# use quant parameter to display percentiles, i.e. quant= c(0.05,0.25,0.75,.95)
library(psych)
t(describe(tsla_change))
# 11. Show histgram with builtin graphing tools
hist(tsla_change[,1], breaks=25, xlab="Change", main="TSLA Price Change", col='aliceblue')
# 12. Use ggplot2 library to create plots
# not quite as intuitive, but give nicer plot by default
# start with ggplot function and typically the data, although you can have other parameters
# then you add layers to your plot
library(ggplot2)
ggplot(data=tsla_change) + geom_histogram(aes(x=TSLA.Close), fill='blue')
# am going to stick with the defaults, but you can see ggplot is giving us some helpful advice
# 13. Density plot gives a slightly different smoothed representation of the data
ggplot(data=tsla_change) + geom_density(aes(x=TSLA.Close), fill='blue')