---
title: "Problem set 4: Models"
output: html_document
---


# Instructions


- You will do your work in this `Rmarkdown` document but will submit all your answers on Canvas
- *very important*: As you move through the document, make sure to run all code chunks (grey bits) that already have code in them
- Write your own code in the empty code chunks
- Useful shortcuts: 
    - to run all the code in a specific code chunk, press the green right-facing triangle at the top right of the code chunk
    - to run all *prior* code chunks, press the downward-facing gray triangle at the top right of the code chunk




```{r load-libraries, warning=FALSE, message=FALSE}
library(tidyverse)
library(socviz)
library(juanr)
library(broom)
library(moderndive)
```


## Task 1


Look at the following example of a data analysis task: 


> Amazon using your past browsing history to recommend new purchases. 


**Q1: Identify the treatment and outcome variable in this example. One sentence.**




Run the code below to get the correlation coefficient for each pair of variables in `movies`:

```{r}
movies |> 
  select(where(is.double)) |> 
  cor(use = "complete.obs") |> 
  round(2)
```


**Q2: using specific language, describe the correlation between the year in which a movie came out and its IMDB score. One sentence.**



## Task 2


Look at the `bonica` data:


```{r}
bonica
?bonica
```



Fit a model that uses `gender` to explain how much a candidate contributed to their own campaign (`contribs_from_candidate`). Look at the output. Note: some candidates have a "U" for gender because the candidate's gender is unknown.

```{r}
lm(contribs_from_candidate ~ gender, data = bonica)
```


**Q3 What is the estimated amount that men candidates contributed to their own campaign, according to the model?**



Fit a model that explains how much money a candidate spent (`total_disbursements`) using their DW-NOMINATE score, their gender, and how many distinct donors they have. 

```{r}
lm(total_disbursements ~ gender + dwnom1 + num_distinct_donors, data = bonica) |> broom::tidy()
```

**Q4. Based on the model, who spends more: conservatives, liberals, or centrists?**





Let's look at elections:

```{r}
elections
?elections
```


Make a scatterplot that visualizes the effect of household income on democratic vote share in the 2016 election, and color the plot by census_region. Add an OLS smoothing line to the plot. You should end up with three lines on the graph when you do this. 

```{r}
ggplot(elections, aes(x = hh_income, y = per_dem_2016, color = census_region)) + 
  geom_point() + 
  geom_smooth(method = "lm")
```

**Q5. right click the graph --> "save image as" --> and save it somewhere on your computer. You will need to submit the plot!**

**Q6. Which census region has the weakest relationship between the two variables( flattest line)?**



## Task 3 Prediction


Let's look at Zillow data on housing prices from `moderndive`:

```{r}
house_prices
?house_prices
```



A seller wants to know: how much should I expect to sell my house for? Fit a model that predicts a house's price based on its: number of bedrooms, number of bathrooms, the square footage of the living area, square footage of the lot, number of floors, whether it is on the waterfront, and the year it was built. 

```{r}
model = lm(price ~ bedrooms + bathrooms + sqft_living + sqft_lot + floors + waterfront + yr_built, data = house_prices)
tidy(model)
```


Now, using the model, answer the following questions. 

What is the estimated price for the seller's house, which has 1 beds, 1 baths, 1,100 sq ft, a 2,500 sq ft lot, 1 floor, no waterfront view, and which was built in 1980? 

```{r}
scen = crossing(bedrooms = 1, bathrooms = 1, sqft_living = 1100, sqft_lot = 2500, floors = 1, waterfront = FALSE, yr_built = 1980)

augment(model, newdata = scen)
```

**Q7: What is the estimated price?**



How good of a job is this model doing at predicting house prices? Let's compare what our model says the house with ID number = 7202290240 *should* cost, and what it *actually* costs. Plug in the house's characteristics into your scenario object, and generate the estimated price. 

```{r}
one_house = house_prices |> filter(id == 7202290240)
augment(model, newdata = one_house)
```

**Q8: how much more is the house actually worth than our estimate ("real price" - "estimated price")?**




That's an in-sample prediction. How well does the model do *out of sample* (a house that wasn't included in our data)? Look at this house: https://www.redfin.com/WA/Kirkland/11131-NE-129th-St-98034/home/459350. Using the house characteristics, generate an estimated price from our model. From the pictures, let's assume that it's a 2 story house with no waterfront view. Let's also assume that the listed price (1.2 million) is what it's worth.  

```{r}
scen = data.frame(bedrooms = 3, bathrooms = 2.5, sqft_living = 1390, 
                  sqft_lot = 7303, floors = 2, waterfront = FALSE, 
                  yr_built = 1972)
augment(model, newdata = scen)
```


**Q9: how much more is the house actually worth than our estimate ("real price" - "estimated price")?**
