Today’s Lecture


REMINDER NOTES:
  • Remember that R is case-sensitive

  • R Code is shown in Grey Boxes and R Output is shown in White Boxes


R Packages for Lecture 2


Notes about R Packages:
  • packages only need to be installed once (using install.packages)

  • If you reinstall R (to update it), you must reinstall packages.

  • R packages need to be loaded every time you start a new R session

  • Sometimes R packages depend on other packages that R will auto-install and load.

  • This process can take a few minutes.

  • In future lectures, there will be a list of packages used at top of document.

  • Installing, loading, using packages may work slightly differently on a MAC.

  • When something doesn’t work, let me or a TA know.

  • We will help you interpret error messages, which are sometimes unclear.


Installing and loading packages in R


# In the R Markdown file I include the eval=FALSE option Reinstalling packages
# when not needed wastes time If you haven't already installed these packages,
# run this code.

install.packages(c("tidyverse", "highcharter", "quantmod"))

# Every time you start a new session load package needed.  In the R Markdown file
# I include the message=FALSE option This prevents the load R messages from
# appearing in the HTML file

library(tidyverse)
library(highcharter)
library(quantmod)
Lecture 1 DEMO and Lecture 2 DEMO are presented as HTML files:
  • These HTML file was written in R Studio using R Markdown

  • R Markdown allows users to write narrative text and include R code, output, etc. called ‘Chunks’.


R Markdown can create documents in HTML, Word, PDF and many others.
  • NOTE: Formatting these documents is VERY tedious. I do not expect perfection.

  • In BUA 455, you will gain familiarity with R Markdown (It’s addictive)

  • Learn basics of alternating text and R Code

  • Your final projects will be developed in R Markdown


In a document created in R MarkDown you can:
  • Include or hide R Code
  • Include or hide R Messages created when code is run.
  • Include or hide R Output and Data Visualizations
  • Include code and output from other languages like Python (not covered in BUA 455)
  • Format output like tables

Here are some R Chunk options and others in a formatted table.:
Polling Question:If I didn’t want you to see the R Code to create this table, what would I include in the {r} brackets that begin this chunk?
# Created 3 text variables:
R_chnk_option <- c("echo", "error", "eval", "message", "results")

R_chnk_default <- c("TRUE", "FALSE", "TRUE", "TRUE", "'markup'")

R_chnk_option_descrip <- c("If FALSE, knitr will not display the code in the code chunk above it's results in the final document.",
                           
                           "If TRUE, R displays error messages in document. If FALSE stop render when errors occur.",
                           
                           "If FALSE, knitr will not run code in chunk.",
                           
                           "If FALSE, knitr will not display any messages generated by the code.",
                           
                           "If 'hide', knitr will not display the code's results in the final document. If 'hold', knitr will delay displaying all output pieces until the end of the chunk")

# put three variables into a data frame
R_chnk_Table <- data.frame(R_chnk_option, R_chnk_default, R_chnk_option_descrip)

# used kable function in knitr package to create a basic table.
kable(R_chnk_Table, col.names=c("Option", "Default", "Description"), 
      caption="Common R Chunk Options used in R Markdown")
Common R Chunk Options used in R Markdown
Option Default Description
echo TRUE If FALSE, knitr will not display the code in the code chunk above it’s results in the final document.
error FALSE If TRUE, R displays error messages in document. If FALSE stop render when errors occur.
eval TRUE If FALSE, knitr will not run code in chunk.
message TRUE If FALSE, knitr will not display any messages generated by the code.
results ‘markup’ If ‘hide’, knitr will not display the code’s results in the final document. If ‘hold’, knitr will delay displaying all output pieces until the end of the chunk

In HW 2 you will create a basic HTML file that includes a mini table

Now Let’s grab some data to play with

quantmod package has functions such as getSymbols

Can be used to download data from financial sites like YaHoo Finance and others

# get last 3 plus years of stock price data from GrubHub:
getSymbols("Grub", from = "2018-1-1")
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## [1] "Grub"
GRUBBY <- weeklyReturn(GRUB$GRUB.Adjusted)
hchart(GRUB)
hchart(GRUBBY)

***

Is GrubHub a good investment? (Setting aside personal opinions)

How do we answer that?
  • First, simplify the question:

    • Based on the last 3+ years, what is the probability that the weekly return is greater than 0?
  • We can look at a histogram for example.

  • Today we will start using the graphics package mostly widely used in R, ggplot2

  • ggplot2 standardizes the language of data visualization

  • Later in the course we will learn more ways to create dynamic interactive visualizations.

  • Here is link to a gallery of ggplot2 graphics options

  • Here is a link to a great tutorial on modifying a ggplot2 Histogram

# first look at ggplot code:
ggplot(data = GRUBBY, aes(x = GRUBBY$weekly.returns)) + geom_histogram(color = "darkblue", 
    fill = "lightblue") + labs(title = "GrubHub 2018 to Present", x = "Week Adjusted Returns", 
    y = "Frequency") + theme_classic()
## Don't know how to automatically pick scale for object of type xts/zoo. Defaulting to continuous.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.


End of Lecture 2