---
title: 'Stat 140: Scatterplots and Correlation Lab'
author: "What's your name?"
date: "September 22, 2017"
output:
  html_document:
    fig_height: 3
    fig_width: 5
  word_document:
    fig_height: 3
    fig_width: 5
---


```{r, include=FALSE}
# Don't delete this chunk if you are using the mosaic package
# This loads the mosaic and dplyr packages
require(mosaicData)
require(dplyr)
require(tidyr)
require(readr)
require(ggplot2)
options(digits=3)
```

# Scatterplots and correlation

- Scatterplots show the **direction**, **form**, **strength**, and **unusual features** of the relationship between two quantitative variables.
- The correlation coefficient measures the **linear** association between two quantitative variables.
- Before calculating a correlation coefficient, check:
    - Quantitative variables condition (only compute correlations between quantitative variables)
    - Straight enough condition (only compute correlations between variables with an approximately linear relationship)
    - No outliers condition (only compute correlations if there are not any outliers that do not "fit" the trend of the rest of the data)


# A First Example

## Rail Trails

Data were collected on the volume of users on the Northampton Rail Trail in Florence, Massachusetts.  Variables in the dataset include the number of crossings on a particular day (measured by a sensor near the intersection with Chestnut Street, `volume`), the average of the min and max temperature in degrees Fahrenheit for that day (`avgtemp`), and a dichotomous indicator of whether the day was a weekday or a weekend/holiday (` weekday`).

```{r}
RailTrail <- mutate(RailTrail, daytype = ifelse(weekday==1, "Weekday", "Wkend/Holiday"))
head(RailTrail)
str(RailTrail)
```

#### Make a scatter plot using the RailTrail data with the `volume` variable on the vertical axis and the `avgtemp` variable on the horizontal axis.

```{r, fig.height=5}
ggplot() +
  geom_point(mapping = aes(x = avgtemp, y = volume), data = RailTrail)
```

#### Describe the relationship between the number of crossings and `avgtemp` (average of min and max temperatures).  Be sure to describe the **direction**, **form**, **strength**, and any **unusual features**.

SOLUTION:

Direction: There is a positive association between temperature and the number of crossings on the bike path up until about 65 degrees, and after that there is a negative association between temperature and the number of crossings.

Form: The scatter plot shows a generally curved shape.

Strength: There is a moderately strong association between temperature and volume.

Unusual features: There are some days with unusually high or low numbers of crossings that don't match the rest of the data well.  Two days with temperatures around 60 degrees stand out as having low bike path volume, and one day around 65 degrees has unusually high bike path volume.


#### Report and interpret the correlation between average temp and number of crossings.  Use the `cor` function.

```{r}
cor(RailTrail$avgtemp, RailTrail$volume)
```

SOLUTION:

The correlation coefficient is 0.47, indicating a moderately strong positive association between temperature and volume.  However, from the scatter plot above, we saw that this relationship is not strictly linear.  The correlation coefficient should really only be used to describe linear relationships.


# Thinking about correlation and dependence (THEY ARE NOT THE SAME!!!)

## Arcade Revenue and Lawyers in Wyoming

Here are some data about the total revenue of arcades in the US (in millions of dollars) and the number of lawyers in Wyoming in each year from 2000 to 2009.

```{r}
Arcades_and_Lawyers <-
  read_csv("https://mhc-stat140-2017.github.io/labs/20170922_correlation/data/arcade_revenue_lawyers_Wyoming.csv")
names(Arcades_and_Lawyers) <- c("year", "arcade_revenue", "lawyers_in_Wyoming")
```

#### Make a scatter plot with `arcade_revenue` on the horizontal axis and `lawyers_in_Wyoming` on the vertical axis.

SOLUTION:

```{r}
ggplot() +
  geom_point(mapping = aes(x = arcade_revenue, y = lawyers_in_Wyoming), data = Arcades_and_Lawyers)
```

#### Describe the direction, form, and strength of the relationship between arcade revenue and the number of lawyers in Wyoming

SOLUTION:

There is a very strong, linear, positive association between arcade revenue and the number of lawyers in Wyoming.

#### Calculate the correlation between `arcade_revenue` and `lawyers_in_Wyoming`

SOLUTION:

```{r}
cor(Arcades_and_Lawyers$arcade_revenue, Arcades_and_Lawyers$lawyers_in_Wyoming)
```

The correlation coefficient is 0.989, indicating a very strong positive association between arcade revenue and the number of lawyers in Wyoming.


#### Do the correlation and relationship that you described above mean that there is a causal relationship between arcade revenue and the number of lawyers in Wyoming?

SOLUTION: No!  There is no reason to think that there is a causal relationship between these variables.


## Spurious Correlations

Go to `http://www.tylervigen.com/spurious-correlations` and browse through some of the plots there.  Become fully and deeply convinced that if two variables have a high correlation, that does not tell you anything about one variable causing the other.
