```{r, echo=FALSE, warning=FALSE, message=FALSE}
require(knitr)
opts_chunk$set(warning=FALSE,message=FALSE)
```


## Homework #2
- Aim for production quality plots here: title, axis labels, no overplotting.
- Can set figure options in r chunk (`fig.height`, `fig.width`, `fig.align`).
- Questions 7 and 8 are best answered with 2 plots, but one could suffice.
- Be sure stuff compiles right on the last question.  Don't forget to submit
images on moodle!

```{r, echo=FALSE}
require(mosaic)
download.file("http://www.openintro.org/stat/data/nc.RData", destfile = "nc.RData")
load("nc.RData")
```


## Numerical descriptors | Two (numerical) Variables {.build}

1. **Shape**: linear, quadratic
2. **Direction**: positive/neg in slope/curvature
3. **Strength**: how tightly clustered?

A measure of the strength of a linear relationship: r, the correlation coefficient
(`cor()`).


## Graphical descriptors | Scatterplot (base) {.build}

```{r}
plot(x = nc$fage, y = nc$mage)
```


## Graphical descriptors | Scatterplot (base) {.build}

```{r}
plot(mage ~ fage, data = nc)
```


## Graphical descriptors | Scatterplot (ggplot2) {.build}

```{r}
ggplot(nc, aes(x = fage, y = mage)) + geom_point()
```


## ggplot2 sandbox

```{r, eval = FALSE}
mplot(nc)
mplot(mtcars, default = "histogram")
```


## Two univariate distributions

```{r submit-time-data, echo=FALSE}
d <- read.csv("submit-times.csv")
d[,1] <- as.character(d[,1])
d[,2] <- as.character(d[,2])
d <- d[d[,1] != "-" & d[,2] != "-",]

a <- d[,1]
datestamp <- unlist(strsplit(a, ", "))[seq(2, length(a)*3, 3)]
timestamp <- unlist(strsplit(a, ", "))[seq(3, length(a)*3, 3)]
x <- paste(datestamp, timestamp)
dt <- strptime(x, "%d %B %Y %I:%M %p")
dt <- as.POSIXlt(x, format = "%d %B %Y %I:%M %p")
a <- dt$min*60 + dt$hour*60*60 + dt$mday*24*60*60

b <- d[,2]
datestamp <- unlist(strsplit(b, ", "))[seq(2, length(b)*3, 3)]
timestamp <- unlist(strsplit(b, ", "))[seq(3, length(b)*3, 3)]
x <- paste(datestamp, timestamp)
dt <- strptime(x, "%d %B %Y %I:%M %p")
dt <- as.POSIXlt(x, format = "%d %B %Y %I:%M %p")
b <- dt$min*60 + dt$hour*60*60 + dt$mday*24*60*60

d <- data.frame(activity = a, homework = b)
dd <- data.frame(submit_time = c(a, b), assmnt = c(rep("activity", length(a)),
                                                   rep("homework", length(b))))
```

```{r}
par(mfrow = c(2,1))
plot(density(d$activity), xlim = c(1000000, 1500000), main = "")
plot(density(d$homework), xlim = c(1000000, 1500000), main = "")
```


## Two univariate distributions

```{r, echo=FALSE}
plot(density(d$activity), xlim = c(1000000, 1500000), ylim = c(0, 1.5*10^(-5)),
     main = "", col = "orange")
lines(density(d$homework)$x, density(d$homework)$y, col = "steelblue")
abline(v = 14*24*60*60 + 15*60*60)
text(1330000, 1.35*10^(-5), "Homework due")
```


## One bivariate distribution: scatterplot

```{r}
ggplot(data=d, aes(x=activity, y=homework)) + geom_jitter()  + theme(legend.position="none") + labs(title="")
```


## Contour plot

```{r, echo=FALSE}
# install.packages("ks")
require(ks)
bd <- kde(as.matrix(d))
plot(bd)
```


## Image plot / heat map

```{r, echo=FALSE}
plot(bd, display = "image")
```


## Perspective plot

```{r, echo=FALSE}
plot(bd, display = "persp")
```


## Getting beyond 2 dimensions {.build}

![iris](http://mirlab.org/jang/books/dcpr/image/iris.gif)

**Fisher's Irises**: Is there a relationship between sepal length and sepal width 
of irises?  I.e., if you have measurements on one, can you predict what the other
will be?


## Getting beyond 2 dimensions

```{r}
ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width)) + geom_jitter()  + theme(legend.position="none") + labs(title="Iris Data") 
```


## Getting beyond 2 dimensions {.build}

**Fisher's Irises**: Is there a relationship between sepal length and sepal width 
of irises?  I.e., if you have measurements on one, can you predict what the other
will be?

No...but what about if you incorporate a third variable: species?


## Getting beyond 2 dimensions {.build}

```{r}
ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width)) + geom_point()  + facet_wrap(~Species, ncol=4) + theme(legend.position="none") + labs(title="") 
```


## Getting beyond 2 dimensions {.build}

You can add a third (categorical) variable by:

1. **Color**: mapping the category of that observation to a discrete color scale.

2. **Faceting**: separate out the data and make a scatterplot for each category.
(preferable if the data is dense).

*Key idea*: Once you get into >2 dimenions, you can describe the structure jointly
or conditioning/controlling for certain variables.


## Getting beyond 2 dimensions {.build}

You can add a third (numerical) variable by:

1. **Color**: map the variable of that observation to a continuous color scale.

2. **Size**: map the variable to the size of that point.

3. **Euclidian dimension**: add a third dimension!


## 3D scatterplot

```{r, echo = FALSE}
#install.packages("scatterplot3d")
library(scatterplot3d)
scatterplot3d(iris$Sepal.Length, iris$Sepal.Width, iris$Petal.Length)
```


## 3D scatterplot

```{r, echo=FALSE, eval=FALSE}
#install.packages("rgl")
library(rgl)
plot3d(iris$Sepal.Length, iris$Sepal.Width, iris$Petal.Length, col="red", size=3)
```


## 3D density plot

```{r, echo=FALSE, eval=FALSE}
fhat <- kde(x=iris[,1:3])
plot(fhat)
```


## Visualizations in yet higher dimensions

[http://www.gapminder.org/world](http://www.gapminder.org/world)

1. How many variables/columns/dimensions are displayed?

2. Is there any structure that is persistent over an entire variable?

3. How would you characterize the relationship between fertility and life
expectancy during the 1950s and 1960s when comparing the industrialized western
nations to the rest of the world?


## High dimension, high art

[http://www.memo.tv/forms/](http://www.memo.tv/forms/)




