## Multiple Regression: polynomials {.build}

Multiple regression refers to the method of predicting one variable as a linear
function of more than one predictor.

*LA homes example*:

\[ \widehat{price} = \beta_0 + \beta_1 sqft + \beta_2 bath \]

But we could also introduc as the additional predictor a polynomial term of the
existing predictor.

\[ \widehat{price} = \beta_0 + \beta_1 sqft + \beta_2 sqft^2 \]


## LA homes{.flexbox .vcenter}

Recall what happened when we fit this model:

\[ \widehat{logprice} = \beta_0 + \beta_1 logsqft \]

```{r echo = FALSE, fig.height = 4, fig.width=6}
LA <- read.csv("http://andrewpbray.github.io/data/LA.csv")
LA <- transform(LA, log_price = log(price), log_sqft = log(sqft))
m1 <- lm(log_price ~ log_sqft, data = LA)
plot(m1, 1)
```


## Quadratic Model

We could consider adding a quadratic term to our model:

\[ \widehat{price} = \beta_0 + \beta_1 sqft + \beta_2 sqft^2 \]

```{r}
m2 <- lm(log_price ~ log_sqft + I(log_sqft^2), data = LA)
summary(m2)$coef
```


## Comparing models

```{r, echo=FALSE}
plot(log_price ~ log_sqft, data = LA)
abline(m1, col = "orange", lwd = 3)
lines(sort(LA$log_sqft), sort(m2$fit), col = "steelblue", lwd = 3)
```


## Comparing models

```{r}
summary(m1)$coef
```

```{r}
summary(m2)$coef
```


## Linear model

```{r echo=FALSE}
par(mfrow = c(2, 2))
plot(m1)
```


## Linear model with quadratic

```{r echo=FALSE}
par(mfrow = c(2, 2))
plot(m2)
```


## Model selection {.build}

The residual plots for the second (more complex) model seem slightly better, so
we're inclined to use that model.  We can also compare the explanatory power of
the models by looking at $R^2$.

```{r}
summary(m1)$adj
summary(m2)$adj
```

These two models are very similar - both are quite good in terms of validity
and explanatory power - but the quadratic one edges out the simple linear one.


## Demonstration on r^2

```{r}
m3 <- lm(log_price ~ log_sqft + rnorm(length(LA$log_price)), data = LA)
summary(m1)$r.squared
summary(m3)$r.squared
summary(m1)$adj
summary(m3)$adj
```


## Activity #7 Part I

- Revisit the RailTrail data set from Activity 4.
- Consider two models: a) SLR model to predict ridership by 
temperature, b) same approach but with added quadratic term.
- Discuss the relative merits of the two models.
- Don't submit this activity yet - to be continued on Weds.





