## 

Without using your book, derive expressions for $\hat{\beta}_0$ and $\hat{\beta}_1$ by
solving for them in the normal equations:

\[
\sum_{i=1}^n y_i = n\hat{\beta}_0 + \hat{\beta}_1 \sum_{i=1}^n x_i \\
\sum_{i=1}^n x_i y_i = \hat{\beta}_0 \sum_{i=1}^n x_i + \hat{\beta}_1 \sum_{i=1}^n x_i^2
\]

*note*: $\frac{1}{n}\sum_{i=1}^n x_i$ can be rewritten $\bar{x}$.

## Poverty and Graduation

```{r echo=FALSE}
poverty <- read.delim("http://andrewpbray.github.io/poverty.txt", header = TRUE)
plot(poverty$Poverty, poverty$Graduates, pch = 16, col = "steelblue")
```


## Which line?

```{r, eval=FALSE}
download.file("http://www.openintro.org/stat/data/mlb11.RData",
              destfile = "mlb11.RData")
load("mlb11.RData")
plot_ss(poverty$Poverty, poverty$Graduates)
```


## Linear models in R {.build}

The workhorse function: `lm()`

```{r, eval=TRUE}
m1 <- lm(Graduates ~ Poverty, data = poverty)
```

The formula notation is read: "I'd like to express y as a function of x". It 
creates a rich object of class `lm`.

```{r}
class(m1)
names(m1)
```


## Linear models in R {.build}

You can extract info about your model using

1. **Attributes**: reference the attributes found with `names()` by using the `$`
operator.

2. **Summary**: the most useful information can be displayed using the `summary()`
command.

3. **Print**: print your model object to get the basic coefficient estimates.


## summary(m1)

```{r, echo=FALSE, size='footnote'}
summary(m1)
```


## Slope interpretation {.build}

The slope, $\beta_1$, tells you that a one unit increase in the $x$ is associated
with a $\beta_1$ unit increase in the $y$, on average.

A one percentage point increase in a state's poverty rate is associated with a 
`r m1$coef[2]` decrease in the state's graduation rate, on average.

Why the "on average"?

\[ \hat{E}(Y|X) = \hat{\beta}_0 + \hat{\beta}_1 * x \]

*note*: sign of $\beta_1$ is often more interesting than magnitude (effected by
scaling).


## Intercept interpretation {.build}

**Mathematically**: The expected value of $y$ when $x$ is zero.

**Contextually**: Sometimes, the "start-up" value of the $y$.

Does the slope have meaning in the poverty vs grad rate example?

<div class="centered">
*Not really*.
</div>


## Activity 3: SLR on quakes

<div class="centered">
![fiji](http://www.widescenes.com/Fiji%20Gallery%20Images/Fiji2115_Large.jpg)
</div>

Recall the last question from homework 1: would you expect a relationship between 
the magnitude of an earthquake and the number of stations that detect it?

