```{r setup, include=FALSE}
knitr::opts_chunk$set( echo=TRUE, warning=F, message=F )
```



## Setup 

```{r}
library( dplyr )     # data wrangling
library( pander )    # formatting 
library( stargazer ) # pretty regression tables 
```


```{r}
d <- read.csv( "https://raw.githubusercontent.com/Watts-College/cpp-524-fall-2021/main/labs/data/teach-for-america.csv" )
head( d ) %>% pander()
```

```{r}
d %>% 
  group_by( teacher, school ) %>% 
  summarize( ave.score=mean(score) ) %>% 
  ungroup() %>% 
  pander( digits=0 )
```


-------------



## Question 1

Consider the following models:  

$Model \space (1): \space \space score = b_0 + b_1 \cdot tfa + b_2 \cdot suburban$

$Model \space (2): \space \space score = b_0 + b_1 \cdot tfa + b_2 \cdot suburban + b_3 \cdot tfa \cdot suburban$

*Note that we use a colon instead of multiplication sign to interact coefficients in the R lm() syntax:*


```{r, results="asis"}
m1 <- lm( score ~ tfa.dummy + sub.dummy, data=d )
m2 <- lm( score ~ tfa.dummy + sub.dummy + tfa.dummy:sub.dummy, data=d )

stargazer( m1, m2, 
           type="html", digits=3,
           intercept.bottom = FALSE,
           omit.stat = c("ser","f","rsq","adj.rsq") )
```

--------  

> Calculate the four group means with regression coefficients from **Model (1)**:

* **Q1-a**: regular urban teachers = b0 = **?**  
* **Q1-b**: tfa urban teachers = b0 + b1 = **?**  
* **Q1-c**: regular suburban teachers = b0 + b2 = **?** 
* **Q1-d**: tfa surburban teachers = b0 + b1 + b2 = **?**  

--------

<br>

> Are any of these means correct? 

**ANSWER:**



Here is the table of group means for reference: 

```{r}
tapply( d$score, list(d$teacher,d$school), mean ) %>% round(0) %>% pander()
```



## Question 2

> Now calculate the four group means with coefficients from **Model (2)**:

* **Q2-a**: b0 = regular urban teachers  = **?** 
* **Q2-b**: b0 + b1 = tfa urban teachers   = **?**  
* **Q2-c**: b0 + b2 = regular suburban teachers  = **?**  
* **Q2-d**: b0 + b1 + b2 + b3 = tfa surburban teachers  = **?** 



> Are these means correct? 

**ANSWER:**

----------





## Question 3

> Why do we need to include the interaction term to recover the true group means?  

**ANSWER:**

----------









## Question 4

> Explain the hypothesis associated with each coefficient:

* **Q4-a**: b0 **?**  
* **Q4-b**: b1 **?**  
* **Q4-c**: b2 **?**    
* **Q4-d**: b3 **?**   




## Question 5

Run baseline model comparing performance of Regular to TFA instructors. Do not control for school geography. 

$score = b_0 + b_1 \cdot tfa.dummy$


```{r, results="asis"}
m0 <- lm( score ~ tfa.dummy, data=d )

stargazer( m0, 
           type="html", digits=3,
           intercept.bottom = FALSE,
           omit.stat = c("ser","f","rsq","adj.rsq") )
```


> Do we find differences in performance across teacher types? Report your decision rules. 

**ANSWER:**




## Question 6

Now run a model comparing teacher performance in urban schools. 

$Model \space (3): \space \space score = b_0 + b_1 \cdot tfa + b_2 \cdot urban + b_3 \cdot tfa \cdot urban$

```{r, results="asis"}

m3 <- lm( score ~ tfa.dummy + urb.dummy + tfa.dummy:urb.dummy, data=d )

stargazer( m3, 
           type="html", digits=3,
           intercept.bottom = FALSE,
           omit.stat = c("ser","f","rsq","adj.rsq") )
```



> Q6-a: What does baseline group b0 represent? 

**ANSWER:** 




> Q6-b: which coefficient represents our hypothesis of interest? 

**ANSWER:** 





> Q6-c: What does b3 test? 

**ANSWER:** 








## Question 7

> Q7-a: which model allows us to test whether Regular instructors perform differently in urban versus suburban schools? 

**ANSWER:**


> Q7-b: which model allows us to test whether TFA instructors perform differently in urban versus suburban schools? 

**ANSWER:**






## Challenge Question 

What is the minimum number of regression models you need to run to test all six hypotheses in the TFA vs Regular at Urban vs Suburban Schools example? 

Report which models you would run and the hypotheses associated with each coefficient. 


<br>
<br>
<hr>
<br>
<br>






```{css, echo=F}
body{
     font-family:system-ui,-apple-system,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;
     font-size:calc(1.5em + 0.25vw);
     font-weight:300;line-height:1.65;
     -webkit-font-smoothing:antialiased;
     -moz-osx-font-smoothing:grayscale;
     margin-left:20%;
     margin-right:20%} 
     

h1, h2, h3, h4 { color: #995c00; }

h2 { margin-top:80px; }



.footer {
  background-color:#726e6e;
  height:340px;
  color:white;
  padding: 20px 3px 20px 3px;
  margin:0px;
  line-height: normal;
}

.footer a{ color:orange; text-decoration:bold !important; } 
 
 
 
 table{
   border-spacing:1px;
   margin-top:80px;
   margin-bottom:100px !important;
   margin-left: auto;
   margin-right: auto;
   align:center} 


 
td{ padding: 6px 10px 6px 10px } 

th{ text-align: left; } 

blockquote {
    margin-top:80px;
    border-left: 5px solid #995c00;
    color: #995c00;
}
```



