---
title: "lab-3"
format: html
editor: visual
---

# Instructions

-   You will do your work in this `Quarto` 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)
set.seed(1990)
```

## 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: Experiments and counterfactuals

Imagine the following scenario:

> You're a researcher that wants to test whether rebel groups that have strong ideological convictions abuse civilians less or more than rebel groups with weak ideological convictions. You also know that, in civil wars where the state is run by a military dictatorship, civilians are more likely to be abused in general, although the state being a military dictatorship has no effect on the rebel group's ideology.

**Q7: What is the treatment variable here, and why might it pose a challenge for the definition of causality in this class?**

Imagine the following scenario:

> a researcher wants to know to what extent, if at all, a six-week aborition ban (e.g., <https://shorturl.at/fiO03)> would cause a reduction in abortions. They look at all the states that passed abortion bans, collect data on abortions before and after the ban, and test whether there is a meaningful decrease in abortions after the law passes. Let's say Florida passed a six-week ban, but Washington state did not.

**Q8: what is the "potential outcomes" counterfactual for the abortion rates observed in Florida (which passed a six week ban)? Remember, This is the counterfactual that is hypothetical, and cannot be observed.**

**Q9: what is the "control" condition or *observable* counterfactual for Florida in this study?**
