Skip to content

Commit 15f3603

Browse files
Add files via upload
1 parent 5311d09 commit 15f3603

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

labs/lab_7.Rmd

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
---
2+
title: "Lab 7"
3+
date: "11/11/2022"
4+
output: pdf_document
5+
---
6+
7+
```{r setup, include=FALSE}
8+
knitr::opts_chunk$set(echo = TRUE)
9+
```
10+
11+
```{r, include=FALSE}
12+
library(tidyverse)
13+
library(estimatr)
14+
```
15+
16+
## Regression with an indicator independent variable
17+
18+
First, let's practice the mechanics of running a regression in R. As you've seen, there are different ways to do this. For most applications, `lm()` is sufficient, but there may be situations in which you want to use the `estimatr` package. We will practice the syntax for both.
19+
20+
First, we need some data. Let's practice with `mtcars`.
21+
22+
```{r}
23+
car_data <- mtcars
24+
```
25+
26+
Let's say that we are interested in exploring the relationship between transmission and fuel economy. Let's regress `mpg` onto `am`.
27+
28+
```{r}
29+
model_1 <- lm(car_data, formula = mpg ~ am)
30+
31+
summary(model_1)
32+
```
33+
34+
We have a slope and intercept, but what do they mean? That depends on what form the independent variable takes and what the specific values represent. Let's check the documentation.
35+
36+
```{r}
37+
?mtcars
38+
```
39+
40+
What values does `am` take in the data? What do those values represent?
41+
42+
*YOUR ANSWER HERE*
43+
44+
Now that we understand what the variable represents, let's interpret our results. One way we can do this, which is especially helpful when we have multiple independent variables is to think about the functional form (or "prediction equation") of our regression. In this case it would be something like:
45+
46+
$$\text{mpg} = b + m D_{i} $$
47+
48+
where $D_i = 0$ if the car has automatic transmission and $D_i = 1$ if it has a manual transmission.
49+
50+
As discussed in lecture, when we have a regression that is structured this way, the intercept is equal to the mean of the "untreated" category and the slope is the difference in means. Can you verify this?
51+
52+
*YOUR CODE HERE*
53+
54+
### Extracting info from a regression
55+
56+
Notice that there is a lot more information stored in the object `model_1` than just the coefficients. For example, the fitted values and residuals. The fitted values are the $Y_i$ generated for each observation by the model and the residuals are the difference between the fitted values and observed values.
57+
58+
If you take Linear Models in the winter quarter, you will talk in great detail about residuals and what they can tell you. For now just know that you can retrieve them from your model:
59+
60+
```{r}
61+
my_resid_1 <- model_1$residuals
62+
```
63+
64+
Since we have the residuals and the fitted values, we should be able to recreate the original `mpg` column. See if you can recreate the mpg column and verify that they are the same.
65+
66+
*YOUR CODE HERE*
67+
68+
## Adding controls
69+
70+
Let's think more critically about the regression we ran above.
71+
+ There's an apparent association between transmission type and miles per gallon.
72+
+ This data is pretty old (1981 I believe), and automatic transmissions were less common and more expensive than they are today. + Taking a quick glance at the data, it appears that some of the cars with the largest engines (measured by displacement) have automatic transmissions and some of cars with the smallest engines have manual transmissions.
73+
+ It is possible that engine size is a confounder, influencing both the choice of transmission (maybe car manufacturers wanted smoother shifting in their more powerful, expensive cars) and the fuel economy (bigger engines consume more fuel).
74+
+ So, we want to account for the association between engine size and fuel economy as well.
75+
76+
To make the interpretation easier, let's create a new indicator variable called `sport` which takes on the value 1 if the displacement is greater than 250 cubic inches.
77+
78+
*YOUR CODE HERE*
79+
80+
Now, let's run the regression again, but include `sport`. Use `lm_robust()` from `estimatr` instead of `lm`.
81+
82+
*YOUR CODE HERE*
83+
84+
What happened to the intercept and coefficient on `am`? What does the coefficient on `sport` mean? What happened to the value of $R^2$?
85+
86+
## Interaction terms
87+
88+
+ The new coefficients suggest that the baseline fuel economy for automatic, non-sport cars is higher than for automatic cars in general, but manual cars are still more fuel efficient on average.
89+
+ Suppose a car industry expert comes to us and points out that there is variation within the cars that have manual transmissions that our model doesn't capture.
90+
+ Manual sports cars tend to be big, beefy American muscle cars (which have terrible gas mileage), but manual non-sports cars tend to be inexpensive, lighter models (which get pretty good gas mileage).
91+
+ In other words, the independent variables in our model *interact* in a way that isn't captured by the coefficients from the previous model.
92+
93+
So, let's add an *interaction term* to the model.
94+
95+
The way to add an interaction term to your model is by including the product of two independent variables. Again, let's use `lm_robust()`.
96+
97+
*YOUR CODE HERE*
98+
99+
Interpret the results. The functional form of this model is now:
100+
101+
$$\text{mpg} = 20.633 + 5.394( \text{am}) - 5.095 (\text{sport}) - 5.532 (\text{am})(\text{sport}) $$
102+
103+
where `am` and `sport` are either 0 or 1. What does our model predict the fuel economy of an automatic sports car is? Automatic non-sports car? Manual sports car? Manual non-sports car?
104+
105+
106+
## Regression with continuous independent variables
107+
108+
So far, we've been working with indicator variables, which have coefficients that are easy to interpret. You're most likely to encounter these variables in experimental contexts. Unfortunately most social science research is not so neat or easy to interpret. Continuous variables are everywhere we look in the real world, and frequently find their way into out models.
109+
110+
Let's look back one of the plots we generated in Lab 1 using the UK coal data. If the code below doesn't work for you, you can save the csv locally (the data is in the course GitHub repo) and read it in.
111+
112+
```{r}
113+
data_path <- "https://raw.github.com/UChicago-pol-methods/IntroQSS-F22/main/data/"
114+
115+
coal_data <- read_csv(str_c(data_path, "uk_coal_tables.csv"),
116+
col_types = "cddddddd",
117+
na = "N/A")
118+
```
119+
120+
Remember this plot that we produced:
121+
122+
```{r}
123+
plot_1 <- ggplot(data = coal_data,
124+
mapping = aes(x = Tons_Produced,
125+
y = Tons_Exported,
126+
color = Country)) +
127+
geom_point() +
128+
geom_smooth(method = "lm") +
129+
labs(x = "Tons Produced",
130+
y = "Tons Exported",
131+
title = "Production vs Exports")
132+
133+
plot_1
134+
```
135+
136+
We colored by country because there seemed to be clusters of points that were behaving differently. Each line represents an OLS regression for a specific country. Use your data wrangling skills and the ggplot code above to find the slope and intercept of the regression for the United Kingdom.
137+
138+
*YOUR CODE HERE*
139+
140+
Interpret the results (remember that the independent variable is continuous). Does the intercept make sense? What does this tell us about this model's ability to generate out of sample predictions? Notice the $R^{2}$ value. Does it tell us anything about whether this is a "good" model?
141+
142+
## Final project brainstorming
143+
144+
Brainstorm a regression you might want to add to your final project. What are the independent and dependent variables? Are they continuous or categorical? How would you interpret your regression coefficient?

0 commit comments

Comments
 (0)