---
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}
# Your code goes here
```

#### 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:



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

```{r}
# Your code goes here.
```

SOLUTION:

\vspace{2cm}


# 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}
# Your code goes here.
```

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

SOLUTION:

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

SOLUTION:

```{r}
# Your code goes here
```


#### 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:


## 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.
