 
---
title: "Lab 5: Linear Regression"
author: "Insert Name"
output:
  pdf_document
 
---

```{r, include = FALSE, message = FALSE}
library(ggplot2)
library(dplyr)
library(nycflights13)
library(knitr)
```

# Instructions

**Due: Before lab Thursday or Friday March 4/5**

Read through the following document (it may be helpful to have both the .rmd and .pdf files open side-by-side). Interspersed throughout the text are 12 **Exercises**. Answer these questions in the .rmd file by inserting a code chunk or typing text between the stars directly below the exercise statement.



# Background
For this problem set you will first run through an example of a simple linear regression, answering a few questions on the way. Then you will work through a regression analysis independently.

We will look at some demographic data from the `fivethirtyeight` package recorded for 48 voting areas in the US states just after the 2016 presidential election. We will investigate what variables within those regions might be tied to the percentage of US voters that supported Donald Trump, and in turn, which variables might be useful to predict Trump support in other regions (i.e. to a wider US population).

# Setup
## Load packages
We will read the data in with the `readr` package, explore the data using the `dplyr` package and visualize it using the `ggplot2` package. The `moderndive` package includes some nice functions to show regression model outputs.


```{r echo = FALSE}
library(dplyr)
library(ggplot2)
library(readr)
library(moderndive)
```


## The data
Run the following in a code chunk to load the data from where it is published on the web.

```{r}
trump <- read_csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vT8qHdvTPaRc62hU94ShBcSh04HP3c11b6XZIPMiUDGuwPtifpP7QhHdSHS2YgTRMRTgfUmBYq-L3ZT/pub?gid=1217616678&single=true&output=csv")
```

Take a moment to look at the data in the viewer or by using `glimpse()`. The explanatory variables include:

1. `hs_ed` - the percentage of the adults in the region with a high school education.
2. `poverty`- the percentage of the “white” households in the region in poverty.
3. `non_white`- the percentage of humans in a region that identify as a person of color.

The outcome variable `trump_support` is the percentage of votes for Trump in 2016 in each region.

Observe that all percentages are expressed as values between 0 and 100, and not 0 and 1.

# An Example/Demo
## Visualization
We will start by investigating the relationship between white poverty levels and support for Trump.

We’ll do this by creating a scatterplot with `trump_support` as the outcome variable on the y-axis and `poverty` as the explanatory variable on the x-axis. Note the use of the `geom_smooth()` function, that tells R to add a regression line. While the points do scatter/vary around the blue regression line, of all possible lines we can draw in this point of clouds, the blue line is the “best-fitting” line in that in minimizes the sum of the squared residuals (see moderndive 6.3.3 for a fuller explanation).


```{r}
ggplot(data = trump, aes(y = trump_support, x = poverty)) + 
  geom_point() + 
  geom_smooth(method = "lm", se = FALSE) +
  labs(x = "Percentage of white households in poverty", 
       y = "Percentage of voters supporting Trump", 
       title = "White poverty and Trump support in the US") 
```


### Exercise 1
Does the relationship appear to be positive or negative? Does it look to be reasonably linear?

***********************************************

***********************************************



## The correlation coefficient (R)
We can numerically quantify the strength of the linear relationship between the two variables with the correlation coefficient. The following tells R to `summarize()` the correlation coefficient between the numerical variables `poverty` and `trump_support`. Note that the correlation coefficient only exists for pairs of numerical variables.


```{r}
trump %>%
  summarise(r = cor(trump_support, poverty))

```


## Running a linear regression model
In R we can fit a linear regression model (a regression line), like so:

```{r}
poverty_mod <- lm(trump_support ~ poverty, data = trump)
```

Note that:

- the function `lm()` is short for “linear model”
- the first argument is a formula in the form `y ~ x` or in other words `outcome variable ~ explantory variable`.
- the second argument is the data frame in which the outcome and explanatory variables can be found.
- we SAVED THE MODEL RESULTS as an object called `poverty_mod`

This object poverty_mod contains all of the information we need about the linear model that was just fit and we’ll be accessing this information again later.

## Get the regression table
The `get_regression_table()` function from the moderndive package will output a regression table. Let’s focus on the value in the second column: an estimate for 1) an intercept, and 2) a slope for the `poverty` variable. We’ll revisit what the other columns mean in a future problem set.

```{r}
get_regression_table(poverty_mod)
```


We can interpret the `intercept` and `poverty` slope like so:

- When the poverty level is 0, the predicted average Trump support is 30.81%
- For every increase in poverty level of 1 percentage point, there is an **associated increase** in Trump support of 2.059 percentage points.

 

### Exercise 2
We found a positive correlation coefficient. Is it reasonable for us to conclude that social policies that increase white poverty will cause an increase in Trump support? Explain why or why not?

***********************************************

***********************************************

## Making predictions
Based on the R output of our model, the following is our least squares regression line for the linear model. Let $Y$ denote the `trump_support` variable and $X$ denote the `poverty` variable:

$$\hat{Y}=30.806+2.059 X$$

We can use the line from our graph of the `poverty`, `trump_support` relationship to visually make predictions...for instance at 15% white poverty, the line shows a value of just over 60% Trump support.

To get a more accurate prediction, we could actually plug 15% into the regression equation like so:

```{r}
y_hat = 30.8064 + 2.0591 * 15
y_hat
```


### Exercise 3

What percent of Trump support would you expect at a value of 6% white poverty?

***********************************************

***********************************************


### Exercise 4
Do you think it is a good idea to predict Trump support at 85% white poverty, based on this regression equation? Explain your reasoning.

***********************************************

***********************************************


## Residuals
Recall that model residuals $(E)$ are the difference between the observed values in your data set and the values predicted by the line:

$$E = Y - \hat{Y}$$ 

For instance, below, one data point is highlighted in red...the residual is the difference between the y value of the data point (here 69), and the y value predicted by the line (roughly 59). Here the residual is roughly 10 (69-59=10). The regression equation has under-estimated Trump support, compared to this data point.

```{r}
ggplot(data = trump, aes(y = trump_support, x = poverty)) + 
  geom_point() + 
  geom_point(x = 14, y = 69, color = "red", size = 3)+
  geom_segment(x = 14, y = 59.5, xend = 14, yend = 69, color = "black", size =.35, linetype = "dotted")+
  geom_smooth(method = "lm", se = FALSE) +
  labs(x = "Percentage of white households in poverty", 
       y = "Percentage of voters supporting Trump", 
       title = "White poverty and Trump support in the US") 
```


The following function `get_regression_points()` gives you the *fitted* (also known as *predicted*) value for every data point, and the residual for every data point. The first row in the output is the first data point...you see that Trump support was 30%, white poverty was 7% the regression equation predicted 45.22 % Trump support, and the residual was -15.22 (30-45.22).

```{r}
get_regression_points(poverty_mod)
```

## Residual Plots

It is often easier to determine the size of residuals by way of a residual plot, which displays points with the same horizontal value as in the scatterplot, but with vertical values equal to the correspond residual. A residual plot can be created in ``ggplot` using the data from the `get_regression_points` function.

```{r}
get_regression_points(poverty_mod) %>% 
  ggplot( aes(x =poverty, y = residual) )+
    geom_point() +
    geom_smooth(method = "lm", se = FALSE)
```

To assess the distribution of residuals, we can use a histogram:

```{r}
get_regression_points(poverty_mod) %>% 
ggplot(aes(x = residual)) +
  geom_histogram(bins = 15 )
```

Since there are relatively few observations (<50), we are careful not to use too many bins.

# Independent Practice

Use the same trump data set for the following questions:

### Exercise 5
Generate a scatterplot with a best-fitting line with non_white as the explanatory variable, and trump_support as the response. Be sure to include an informative title and axis labels to your plot. This will help contextualize it.

***********************************************

***********************************************

### Exercise 6
Do you expect the correlation coefficient (for non_white and trump_support) to be positive or negative? Write code chunk testing your prediction (it is OK if your expectation was wrong!).

***********************************************

***********************************************

### Exercise 7
Run a linear regression using non_white as the explanatory variable, and trump_support as the outcome variable. Interpret the Intercept and slope estimates.

***********************************************

***********************************************

### Exercise 8
Make a numerical prediction for the level of trump support for a region that has 70% of humans that identify as a person of color. In other words, use math not a visual prediction.

***********************************************

***********************************************


### Exercise 9

Create a residual plot with `trump_support` modeled as a function of `non-white`. Do the residuals appear to have relatively constant variability?

***********************************************

***********************************************


### Exercise 10

Create a histogram of the residual values. Does the distribution appear to be bell-shaped, unimodal, symmetric and centered at $0$?

***********************************************

***********************************************


### Exercise 11
Based on the evidence you have so far (scatterplots and correlation coefficients), which of the explanatory variables we have considered (non_white or poverty) seems to be a better explanatory variable of Trump support? Explain.

***********************************************

***********************************************

### Exercise 12
If Representative Ocasio-Cortez saw the regression line and not the actual data:

A. What would her prediction of Trump support be for a region in which 61% of the people identify as non-white?

B. Would her prediction be an overestimate or an underestimate (compared to the observed data), and by how much?

C. In other words, what is the residual for this prediction?

***********************************************

***********************************************

## Exercise 13

A. Shown below are two plots of residuals remaining after fitting a linear model to two different sets of data. Describe important features and determine if a linear model would be appropriate for these data. Explain your reasoning.


```{r echo= FALSE, out.width="50%"}
include_graphics(c("/home/courses/math141s20/Img/resDemoResScatFan.pdf",
                   "/home/courses/math141s20/Img/resDemoResScatExp.pdf" ))
```

************************************************

************************************************

B. For each of the six plots, identify the strength and direction of the relationship (e.g. weak, moderate, or strong) in the data and whether fitting a linear model would be reasonable.

```{r echo= FALSE, out.width="33%"}
include_graphics(c("/home/courses/math141s20/Img/association1.pdf",
                   "/home/courses/math141s20/Img/association2.pdf",
                   "/home/courses/math141s20/Img/association3.pdf",
                   "/home/courses/math141s20/Img/association4.pdf",
                   "/home/courses/math141s20/Img/association5.pdf",
                   "/home/courses/math141s20/Img/association6.pdf") )
```

************************************************

************************************************

C. Sort the following scatterplots in order of increasing correlation (recall negative numbers are always less than positive numbers). Your answer should be a list like: (1), (2), (3), (4). Explain your reasoning.

```{r echo= FALSE, out.width="50%"}
include_graphics(c("/home/courses/math141s20/Img/corrMatch5.pdf",
                     "/home/courses/math141s20/Img/corrMatch6.pdf",
                     "/home/courses/math141s20/Img/corrMatch7.pdf",
                     "/home/courses/math141s20/Img/corrMatch8.pdf" ))
```

************************************************

************************************************


## Exercise 14

A study conducted at the University of Denver investigated whether babies take longer to learn to crawl in cold months, when they are often bundled in clothes that restrict their movement, than in warmer months. Infants born during the study year were split into twelve groups, one for each birth month. We consider the average crawling age of babies in each group against the average temperature when the babies are six months old (that's when babies often begin trying to crawl). Temperature is measured in degrees Fahrenheit and age is measured in weeks. 

A. Load the data from the study by changing `eval=FALSE` to `eval=TRUE` in the following chunk. Then construct a scatterplot with Temperature as the explanatory variable and Crawling Age as the response variable.

```{r  message=FALSE, eval=TRUE}
babies<-read_csv("/home/courses/math141s20/Data/babiesCrawl.csv")
```

************************************************

************************************************

B. Describe the relationship between temperature and crawling age.

************************************************

************************************************

C. Theorize how the relationship would change if temperature was measured in degrees Celsius and age was measured in months. Then test your hypotheses by mutating the data frame and creating a new scatterplot.

************************************************

************************************************

D. Compute the correlation between temperature in Fahrenheit and age in weeks. Predict how the correlation would change if temperature was measured in degrees Celsius and age was measured in months. Test your hypothesis by mutating the data frame and computing the new correlation.

************************************************

************************************************

E. Fit a linear model to the data, with temperature in Fahrenheit as the explanatory variable and age in weeks as the response. Predict how the slope and intercept of the linear model would change if temperature was measured in degrees Celsius and age was measured in months. Test your hypothesis by mutating the data frame and fitting a new linear model.

************************************************

************************************************


## Exercise 15 

The data in `starbucks.csv` contains information about the number of calories and amount of carbohydrates (in grams) Starbucks food menu items contain. Since Starbucks only lists the number of calories on the display items, we are interested in predicting the amount of carbs a menu item has based on its calorie content.

A. Load the data from the study by changing `eval=FALSE` to `eval=TRUE` in the following chunk. Then construct a scatterplot with Calories as the explanatory variable and Carbs as the response variable.

```{r  message=FALSE, eval=TRUE}
starbucks<-read_csv("/home/courses/math141s20/Data/starbucks.csv")
```

************************************************

************************************************


B. Describe the relationship between number of calories and amount of carbohydrates (in grams) that Starbucks food menu items contain.

************************************************

************************************************

C. Explain why we might want to fit a regression line to these data?

************************************************

************************************************


D. Do these data meet the conditions required for fitting a least squares line? Justify your answer using both a residual plot and histogram of residuals.

************************************************

************************************************

E. Regardless of your answer to part D, fit a linear model to the data and create scatterplot with the line of best fit. Use your model to predict the number of carbs in a drink with 225 calories and in a drink with 5 calories. How confidant do you feel in the accuracy of each of these predictions?

************************************************

************************************************


