---
title: "Code for Session 1"
author: "Dr. Richard M. Crowley"
date: ""
output:
  html_notebook
---

Note that the directories used to store data are likely different on your computer, and such references will need to be changed before using any such code.

```{r helpers, warning=FALSE, message=F}
library(knitr)
library(kableExtra)
html_df <- function(text, cols=NULL, col1=FALSE, full=F) {
  if(!length(cols)) {
    cols=colnames(text)
  }
  if(!col1) {
    kable(text,"html", col.names = cols, align = c("l",rep('c',length(cols)-1))) %>%
      kable_styling(bootstrap_options = c("striped","hover"), full_width=full)
  } else {
    kable(text,"html", col.names = cols, align = c("l",rep('c',length(cols)-1))) %>%
      kable_styling(bootstrap_options = c("striped","hover"), full_width=full) %>%
      column_spec(1,bold=T)
  }
}
```

```{r, eval=TRUE, warning=FALSE, message=FALSE}
# Google ngrams for Data TODO
library(ngramr)
library(ggplot2)
ngd <- c("(Analytics + analytics)", "(Machine learning + machine learning)")
```

```{r, eval=TRUE, fig.width=7, fig.height=2.8}
ggram(ngd, year_start=1960, geom = "area", google_theme=F, smoothing = 3) + theme(legend.position="bottom", legend.direction="horizontal")
```

```{r graph1, warning=FALSE, message=FALSE, fig.height = 5, fig.width = 4.3}
library(readr)
df <- read_csv("../../Data/Session_1-1.csv")
df <- df[df$indfmt != "FS",]  # remove FS codes
df <- df[order(df$gvkey,df$datadate),]
df$ni_lag <- c(NA, df$ni[1:nrow(df)-1])
df <- df[floor(df$datadate/10000) == 2017,]
df <- df[!(is.na(df$ni) | is.na(df$ni_lag)),]
library(ggplot2)
suppressPackageStartupMessages(library(plotly))
cor <- cor(x=df$ni, y=df$ni_lag, method="pearson")
set.seed(11)
df_s <- df[sample(nrow(df), 400),]
```

```{r graph1_part2, warning=FALSE, message=FALSE, fig.height = 5, fig.width = 4.3}
plot <- ggplot(df_s, aes(x=ni_lag, y=ni)) + 
  geom_point(shape=1, aes(text=sprintf("Ticker: %s", tic))) + 
  geom_smooth(aes(x=ni_lag, y=ni), method=lm, se=T) + 
  labs(x="2016 Net Income ($M USD)",
       y="2017 Net Income ($M USD)")
ggplotly(plot, tooltip="text")
```

```{r graph2, warning=FALSE, message=FALSE}
library(readr)
# import GDP data
dfg <- read_csv("../../Data/per-capita-gni-and-per-capita-gdp-at-current-market-prices-annual-2019.08.csv")
dfg <- dfg[dfg$level_1 == "Per Capita GDP",]
dfg <- dfg[order(dfg$year),]

# import unemployment data
dfu <- read_csv("../../Data/overall-unemployment-rate-annual-2019.08.csv")
dfu <- dfu[order(dfu$year),]

# merge into one dataset
df <- data.frame(year=1993:2016,
                 growth=dfg[dfg$year>1992,]$value / dfg[dfg$year > 1991 & dfg$year < 2016,]$value - 1,
                 unemploy=dfu[dfu$year>1992,]$unemployment_rate,
                 unemploy_lag=dfu[dfu$year<2016,]$unemployment_rate)

library(ggplot2)
suppressPackageStartupMessages(library(plotly))
cor <- cor(x=df$growth, y=df$unemploy, method="pearson")


```

```{r graph2_part2, warning=FALSE, message=FALSE, fig.height = 5, fig.width = 4.3}
plot <- ggplot(df, aes(x=unemploy, y=growth)) + 
  geom_point(shape=1, aes(text=sprintf("Year: %i", year))) + 
  geom_smooth(method=lm, se=T) + 
  labs(x="Unemployment rate",
       y="GDP Growth")
ggplotly(plot, tooltip="text")
```

```{r graph3, eval=TRUE, warning=FALSE, message=FALSE}
drown <- c(109,102,102,98,85,95,96,98,123,94,102) # US CDC data, 1999 to 2009 (falling in)
drown2 <- c(421,465,494,538,430,530,511,600,582,605,603)
ic <- c(16.1, 15.8, 16.2, 15.9 ,14.6 ,15.1 ,15.3, 14.8, 14.2 ,13.9 ,14 ,13.2 ,13.2 ,13 ,12.5 ,12.9 ,13.1) # Statista, 2000 to 2016
icprice <- c(126.858,128.467,130.858,131.733,134.808,137.508,144.592,150.575,155.500,161.742,164.375,173.408,179.033,175.467,178.250,177.575,179.233,183.373,192.768,196.637,195.055,209.161,215.524,215.489,215.412,219.201,219.452,219.903,219.593) # CPI data for ice cream and related products, 1990-2018
cancerthursday <- c(80262,80994,81321,81988,81390,82682,82109,82516,82663,83166,84978,84742) # CDC (tylervigen.com)
# https://futurism.com/correlation-vs-causation-2/
df <- data.frame(drown=drown2[2:length(drown2)],
                 icecream=ic[1 : length(drown2)-1],
                 icecreamprice=icprice[11:(length(drown2)+9)],
                 year=2000:2009)
df$drown <- df$drown / mean(df$drown)
df$icecream <- df$icecream / mean(df$icecream)
df$icecream2 <- df$icecream*df$icecreamprice / mean(df$icecream*df$icecreamprice)
df$icecream3 <- df$icecreamprice / mean(df$icecreamprice)
cor_unit <- cor(x=df$icecream, y=df$drown)
cor_rev <- cor(x=df$icecream2, y=df$drown)
cor_price <- cor(x=df$icecreamprice, y=df$drown)
library(ggplot2)

library(reshape2)
dm <- melt(df[c("drown", "icecream2", "year")],id.var=3)
plot1 <- ggplot(dm, aes(x=year, y=value, colour=variable)) + 
  geom_point(shape=1) +
  geom_smooth(method=loess, se=F) +
  labs(x="Year",
       y="% of average year") + 
  scale_x_continuous(breaks=c(2000,2003,2006,2009)) + 
  scale_colour_discrete(name="Statistic",
                        breaks=c("icecream2","drown"),
                        labels=c("Ice cream revenue","Drowning in pools"))
dm <- melt(df[c("drown", "icecream3", "year")],id.var=3)
plot2 <- ggplot(dm, aes(x=year, y=value, colour=variable)) + 
  geom_point(shape=1) +
  geom_smooth(method=loess, se=F) +
  labs(x="Year",
       y="% of average year") + 
  scale_x_continuous(breaks=c(2000,2003,2006,2009)) + 
  scale_colour_discrete(name="Statistic",
                        breaks=c("icecream3","drown"),
                        labels=c("Ice cream volume sold","Drowning in pools"))
dm <- melt(df[c("drown", "icecream", "year")],id.var=3)
plot3 <- ggplot(dm, aes(x=year, y=value, colour=variable)) + 
  geom_point(shape=1) +
  geom_smooth(method=loess, se=F) +
  labs(x="Year",
       y="% of average year") + 
  scale_x_continuous(breaks=c(2000,2003,2006,2009)) + 
  scale_colour_discrete(name="Statistic",
                        breaks=c("icecream","drown"),
                        labels=c("Ice cream price","Drowning in pools"))
```

```{r graph3_part2, warning=FALSE, message=FALSE, fig.height = 4, fig.width = 4.3}
suppressPackageStartupMessages(library(plotly))
ggplotly(plot1)
```

```{r graph3_part4, warning=FALSE, message=FALSE, fig.height = 4, fig.width = 4.3}
suppressPackageStartupMessages(library(plotly))
ggplotly(plot3)
```

```{r graph3_part3, warning=FALSE, message=FALSE, fig.height = 4, fig.width = 4.3}
suppressPackageStartupMessages(library(plotly))
ggplotly(plot2)
```

```{r graph3_part5, warning=FALSE, message=FALSE, fig.height = 4, fig.width = 4.3}
suppressPackageStartupMessages(library(plotly))
ggplotly(plot2)
```

```{r, eval=TRUE}
# Addition uses '+'
1 + 1

# Subtraction uses '-'
2 - 1

# Multiplication uses '*'
3 * 3

# Division uses '/'
4 / 2
```

```{r, eval=TRUE}
# Exponentiation uses '^'
5 ^ 5

# Modulus (aka the remainder) uses '%%'
46 %% 6

# Integer division uses '%/%'
46 %/% 6

```

```{r eval=TRUE}
# Store 2 in 'x'
x <- 2

# Check the value of x
x

# Store arithmetic in y
y <- x * 2

# Check the value of y
y
```

```{r, eval=TRUE}
# Previous value of x and y
paste(x, y)

# Change x, then recheck the value
# of x and y
x <- 200

paste(x, y)
```

```{r Singtel, eval=TRUE}
# Data from Singtel's earnings reports, in Millions of SGD
singtel_2017 <- 3831.0
singtel_2018 <- 5430.3

# Compute growth
growth <- singtel_2018 / singtel_2017 - 1

# Check the value of growth
growth
```

```{r, eval=TRUE}
company_name <- "Google"  # character
company_name

company_name <- 'Google'  # character
company_name

tech_firm <- TRUE  # boolean
tech_firm

earnings <- 12662  # numeric, $M USD
earnings
```

```{r}
library(tidyverse)
df <- read.csv("../../Data/Session_1-2.csv", stringsAsFactors=FALSE)
# Filter to firms with at least $1M USD revenue, known net income, and fiscal year of 2017
clean_df <- df %>% filter(fyear==2017, !is.na(revt), !is.na(ni), revt > 1)
tech_df <- clean_df %>%
  filter(gsector==45) %>%
  mutate(revenue = revt,
         earnings = ni,
         margin = ni/revt)
earnings_2017 <- tech_df$ni
revenue_2017 <- tech_df$revt
names_2017 <- tech_df$conm
names(earnings_2017) <- names_2017
names(revenue_2017) <- names_2017
```

```{r, eval=TRUE}
# Calculating profit margin for all public US tech firms
# 715 tech firms in Compustat with >1M sales in 2017

# Data:
#    earnings_2017: vector of earnings, $M USD
#    revenue_2017: vector of revenue, $M USD
#    names_2017: a vector of tickers (strings)

# Namining the vectors
names(earnings_2017) <- names_2017
names(revenue_2017) <- names_2017

earnings_2017[1:6]
revenue_2017[1:6]
```

```{r, eval=TRUE}
# Summarizing vectors
summary(earnings_2017)
```

```{r, eval=TRUE}
summary(revenue_2017)
```

```{r, eval=TRUE}
# Calculating profit margin
margin <- earnings_2017 / revenue_2017
summary(margin)
```

```{r, eval=TRUE}
# Worst, midpoint, and best profit margin firms in 2017. Our names carried over :)
margin[order(margin)][c(1,length(margin)/2,length(margin))]
```

```{r}
columns <- c("Google", "Microsoft",
             "Goldman")
rows <- c("Earnings","Revenue")

firm_data <- matrix(data=
  c(12662, 21204, 4286, 110855,
    89950, 42254), nrow=2)
# Equivalent:
# matrix(data=c(12662, 21204, 4286,
#   110855, 89950, 42254), ncol=3)

# Apply names
rownames(firm_data) <- rows
colnames(firm_data) <- columns

# Print the matrix
firm_data
```

```{r}
firm_data[2, 3]
firm_data[, c("Google", "Microsoft")]
firm_data[1,]
firm_data["Revenue", "Goldman"]
```

```{r}
indcode <- c(45,45,40)
jpdata <- c(17370, 115475)
# Preloaded: industry codes as indcode (vector)
```

```{r}
# Preloaded: industry codes as indcode (vector)
#     - GICS codes: 40=Financials, 45=Information Technology
#     - See: https://en.wikipedia.org/wiki/Global_Industry_Classification_Standard
# Preloaded: JPMorgan data as jpdata (vector)

mat <- rbind(firm_data,indcode)  # Add a row
rownames(mat)[3] <- "Industry"  # Name the new row
mat
mat <- cbind(firm_data,jpdata)  # Add a column
colnames(mat)[4] <- "JPMorgan"  # Name the new column
mat
```

```{r}
# Ignore this code for now...
model <- summary(lm(earnings ~ revenue, data=tech_df))
#Note that this function is hiding something...
model
```

```{r}
model["r.squared"]
model[["r.squared"]]
model$r.squared
```

```{r}
earnings <- c(12662, 21204, 4286)
company <- c("Google", "Microsoft", "Goldman")
names(earnings) <- company
earnings["Google"]
earnings[["Google"]]
#Can't use $ with vectors
```

```{r}
str(model)
```

```{r}
library(tidyverse)  # Imports most tidy packages
# Base R data import -- stringsAsFactors is important here
df <- read.csv("../../Data/Session_1-2.csv", stringsAsFactors=FALSE)
df <- subset(df, fyear == 2017 & !is.na(revt) & !is.na(ni) &
             revt > 1 & gsector == 45)
df$margin = df$ni / df$revt
summary(df)
```

```{r, message=F}
# Tidy import
df <- read_csv("../../Data/Session_1-2.csv") %>%
  filter(fyear == 2017,      # fiscal year
         !is.na(revt),       # revenue not missing
         !is.na(ni),         # net income not missing
         revt > 1,           # at least 1M USD in revenue
         gsector == 45) %>%  # tech firm
  mutate(margin = ni/revt)   # profit margin
summary(df)
```

```{r, message=F}
df <- mutate(
        filter(
          read_csv("../../Data/Session_1-2.csv"),
          fyear == 2017,      # fiscal year
          !is.na(revt),       # revenue not missing
          !is.na(ni),         # net income not missing
          revt > 1,           # at least 1M USD in revenue
          gsector == 45),  # tech firm
        margin = ni/revt)   # profit margin
summary(df)
```

```{r}
# To see the arguments a function takes, run:
args(data.frame)
```

```{r}
add_two <- function(n) {
  n + 2
}

add_two(500)
```

```{r}
mult_together <- function(n1, n2=0, square=FALSE) {
  if (!square) {
    n1 * n2
  } else {
    n1 * n1
  }
}

mult_together(5,6)
mult_together(5,6,square=TRUE)
mult_together(5,square=TRUE)
```

```{r, warning=F, message=F}
FXRate <- function(from="USD", to="SGD", dt=Sys.Date()) {
  options("getSymbols.warning4.0"=FALSE)
  require(quantmod)
  data <- getSymbols(paste0(from, "/", to), from=dt-3, to=dt, src="oanda", auto.assign=F)
  return(data[[1]])
}
date()
FXRate(from="USD", to="SGD")  # Today's SGD to USD rate
FXRate(from="SGD", to="CNY")  # Today's SGD to CNY rate
FXRate(from="USD", to="SGD", dt=Sys.Date()-90)  # Last quarter's SGD to USD rate
```

