---
title: "Midterm1_Demo_11am"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(moderndive)
```


```{r}
library(palmerpenguins)
data("penguins")
```


Averages don't play nice with NA
```{r}
penguins %>% summarize(avg = mean(body_mass_g, na.rm = T ))
```

```{r}
penguins <- penguins %>% 
  drop_na()
```

`table` takes 2 columns of categorical data and builds a 2-way or contingency table for counts of each pair.

Build a contingency for species and island

```{r}
my_table<- table(penguins$species, penguins$island)
my_table
```

```{r}
prop.table(my_table)
```

What is breakdown of island by species?

```{r}
prop.table(my_table, 1)
prop.table(my_table, 2)
```

## Review Problem


### Problem 1

Nic Marks is a statistician and founder of the Centre for Well-Being at the UK think tank New Economics Foundation.  He and his group created the Happy Planet Index as a measure of the happiness in a country.  One argument of the New Economics Foundation is that we should measure happiness and well-being for a country instead of economic measures, such as, the Gross Domestic Product.  The foundation also tracks the ecological footprint per capita of each country.[^1]

[^1]: http://www.happyplanetindex.org

**We want to determine if you can model the well-being of a country using the ecological footprint.**

```{r}
# Load the data
hpi <- read_csv("https://reed-statistics.github.io/math141s21/data/hpi.csv")
```


#### Codebook for the Variables:

* Region: Region of the world where country is located. a = Latin America, b = Western nations, c = Middle East, d = Sub-Saharan Africa, e = South Asia, f = East Asia, g = former Communist

* LifeExpectancy: Average life expectancy based on the 2011 UNDP Human Development Report

* WellBeing:  In a Gallup 2012 World Poll, respondents were asked what step of the "Ladder of Life" are you currently on where 0 = is the worst possible life and 10 = best possible life.

* Footprint: This is a measure of resource consumption.  It signifies the amount of land required to sustain a country's consumption per capita and is measured in terms of global hectares.[^2]

[^2]: http://www.footprintnetwork.org

* HappyPlanetIndex: a measure created by Nic Mark which takes into account the life expectancy, well-being, and ecological footprint of a country

* Population: Population

* GDP: Gross Domestic Product per capita



 **We want to determine if you can model the well-being of a country using the ecological footprint.**

a. Produce a plot comparing ecological footprint and wellbeing.  Include the least squares regression line. Discuss any trend you observe.
 

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

 



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



b. Recreate the plot but this time include region as well. What new trends are evident?

 

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

 



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



c. Assess whether a linear model is appropriate for this data. Support your answer with appropriate visualizations.

 

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

 



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


d.  Regardless of your previous answer, build the linear regression model for wellbeing using footprint.  Interpret the slope term.
 

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

 



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


e.  For a country with an ecological footprint of 4 global hectares per capita, predict the well-being for that country.



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

 



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


f. Now, build a linear regression model predicting well-being as a function of region. Which region is used as a baseline? What is the mean well-being for this region?


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

 

```{r}
my_mod <- lm(WellBeing ~ Region, data = hpi)
get_regression_table(my_mod)
get_correlation(hpi, formula = LifeExpectancy ~ WellBeing)
get_correlation(hpi, formula =  WellBeing ~ LifeExpectancy)

hpi %>% summarize(correlation = cor(LifeExpectancy, WellBeing))
```

The mean well being for region A is 6.174. 

The mean well-being for region B is 6.174 + .75 = 6.924

```{r}
6.174 + .75 
```


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

g. Based on the coefficients of your linear model in the previous part, which region has the highest well-being? Which has the lowest?


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

 



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


h. What is the predicted well-being China? What is the residual for China?


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

 
A residual is difference between observed value and predicted. 

```{r}
residuals  <- get_regression_points(my_mod)
```



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


