---
title: "Motivating Example  <br> <br> <br>"
author: "Omni Analytics Group"
output:
  xaringan::moon_reader:
    includes:
      after_body: insert-logo.html
    css: [default, default-fonts, classroom.css]
    lib_dir: libs
    nature:
      ratio: 16:9
      highlightStyle: github
      highlightLines: true
      countIncrementalSlides: false
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE, fig.align = "center")

```

## Motivating Example

- Explore a real data set using R.
- The goal is to have a good understanding of using R for data management and exploration.
- Don't worry about understanding all of the coding right away.
- We will go back and explain how it all works in detail.


## CryptoPunks

The CryptoPunks are 10,000 uniquely generated characters. Each of them is officially owned by a single person on the Ethereum blockchain. Initially, they were all claimed for free. Now, you can buy, bid on, and offer punks for sale via a marketplace that is also embedded in the blockchain.

<p align="center">

<img src="images/punk-image.png" width="800px" height="200px">

</p>
---

## CryptoPunks Dataset

- Shows sales of CryptoPunks since June 23rd, 2017 to December 30th, 2020.
- The dataset includes the following:
  * Details of sales
  * Details of the Punk
  * Rarity Rank of the Punk

Note that the data may not be 100% accurate!

#### Let's get started!

--

## Load the Data

First, we load Punks using `read.csv()`:

```{r}
Punks <- read.csv("punks.csv")
```

---

## First look at data in R

Let's use R to look at the top few rows of the Punks data set. The `head()` function allows you to look at first 6 rows of the data and `tail()` allows you to look at last 6 rows.

```{r}
head(Punks)
```

---

## CryptoPunks Data Attributes
The command `str()`, short for structure, gives us a summary of each variable along with the size of the "data frame".

```{r, fig.height=4, fig.width=7}
str(Punks)
```
The Punks data frame has 17554 observations (rows) and 12 variables (columns).

---

## CryptoPunks Variables Summary

Let's summarize the values for each variable in Punks with the `summary()` command. With this command we immediately have summary statistics of each variable.


```{r, eval = FALSE}
summary(Punks)
```

<br> <br> <br> <br> <br>
<p align="center">

<img src="images/Cut_outs/Cut_out_16.png"  width="200px" height="150px">

</p>
---
```{r, echo = FALSE}
summary(Punks)
```

---

## Scatterplots

Let's look at the relationship between Crypto and USD.  First, we need to install and load ggplot2, a special package for plotting and visualizations.

```{r, eval = F}
install.packages("ggplot2")
```

```{r}
library(ggplot2)
```

Using the `qplot()` command we can create a simple scatter plot.

Do not let the `qplot()` command or any of the parameters confuse you. We will discuss this function in detail in later lessons.
---

## Scatter Plot

```{r, fig.height = 6}
qplot(Crypto, USD, geom="point", data = Punks, main = "USD vs. Crypto (ETH)")
```

---

## More Scatterplots

```{r, fig.height = 6}
qplot(Crypto, USD, geom="point", data = Punks, main = "USD vs. Crypto (ETH)", colour = Type)
```

---

## Even More Scatterplots

```{r, fig.height = 6}
qplot(Crypto, USD, geom="point", data = Punks, main = "USD vs. Crypto (ETH) with Regression Line")+
  geom_smooth(method = "lm")
```

---

## Creating A New Variable

We will make a new variable in the Punks data set to account for ratio between Crypto and USD that is, `Ratio of Currency = USD / Crypto`.

```{r}
# Creates new column Ratio.of.Currency in Punks data
Punks$Ratio.of.Currency <- Punks$USD / Punks$Crypto
```
Notice that we had to place periods between words. R does not allow users to separate phrases with spaces.

<p align="right">

<img src="images/Cut_outs/Cut_out_06.png"  width="200px" height="150px">

</p>
---

## Summary
```{r}
summary(Punks$Ratio.of.Currency)
```

One way we can interpret this as 1 ETH is worth $328.00 (at the time of writing).

## Column Names
```{r}
names(Punks) # Provides the name of each column
```

---

## Crypto Histogram

We now plot a histogram of Crypto to see its distribution.

```{r, fig.height = 5}
qplot(Crypto, data = Punks, binwidth = 2.5, main = "Histogram of Crypto") # binwidth is the length of each rectangular bar
```

---

## Most expensive Punk that was last sold in the sample data

We can look at the most expensive Punk that was last sold using `which.max()`.
```{r}
Punks[which.max(Punks$Crypto),]
```
Thus, the most expensive Punk is <b> Punk 3306 </b> sold at 189.99 ETH on December 30, 2020.

--
## Wait a minute!

--

Did you fact check this by visiting the official CryptoPunks website? Did you notice something strange when you looked up <b> Punk 3306 </b>?

--
It turns out that it's <b> Punk 3307 </b> that sold at 189.99 ETH on December 30, 2020. This is a common 'off by one' mistake made in the data science world and it is something we need to be aware of as we learn more about the data.


---
## Average Slots By Sex

Looking at the average number of slots, we noticed that <b> Girl </b> and <b> Guy </b> punks have a similar number of slots.  

```{r}
mean((Punks$Slots)[Punks$Sex == "Girl"])
mean((Punks$Slots)[Punks$Sex == "Guy"])
```

As a breakdown:

- `mean()` - provides the mean of argument
- `Punks$Slots` - selects the Slots column in the Punks dataset
- `[Punks$Sex == "XX"]` - subsets the column with the chosen Sex XX

---

## Combine Types

We can label Alien, Ape and Zombie as non_human, and similarly we can label Female and Male as human.

This code works by creating new columns **non_human** and **human** by selecting the rows that contain `c("Alien", "Ape", "Zombie")` and `c("Female", "Male")` in the Position column, respectfully

```{r}
non_human <- (Punks$Type %in% c("Alien", "Ape", "Zombie"))
head(non_human)
```

```{r}
human <- (Punks$Type %in% c("Female", "Male"))
head(human)
```
---

## Average Slots by Types

Finding the mean of each based on the newly created Types
```{r}
mean(Punks$Slots[non_human])
mean(Punks$Slots[human])
```
We can see that, **human** type punks have more slots than **non_human** type punks.

---

## Box Plots

We could compare the slots for the different types of punks with a side by side boxplot.

```{r, fig.height=6}
qplot(Type, Slots, geom = "boxplot", data = Punks, main = "Box Plot of Slots by Types")
```

---

## Your Turn

Try playing with chunks of code from this session to further investigate the Punks data:

1. Summarize the Crypto values.
2. Make a boxplot comparing Crypto for different Skins.
3. Find the average slots for the pale (Alien, Albino, Light) and dark (Dark, Mid).

<br> <br>
<p align="left">

<img src="images/Cut_outs/Cut_out_01.png"  width="200px" height="150px">

</p>
---

## Answers

### 1
```{r}
summary(Punks$Crypto)
```

---

### 2
```{r, fig.height = 6}
qplot(Skin, Crypto, geom = "boxplot", data = Punks, main = "Box Plot of Crypto by Skins")
```

---

### 3
```{r}
pale <- (Punks$Skin %in% c("Alien", "Albino", "Light"))
dark <- (Punks$Skin %in% c("Dark", "Mid"))
mean(Punks$Slots[pale])
mean(Punks$Slots[dark])

```
