Load Packages

library( pander )     # formatting tables
library( dplyr )      # data wrangling
library( stargazer )  # regression tables

# some custom drawing functions 
source( "https://raw.githubusercontent.com/DS4PS/sourcer-r/master/sourcer.R" )

Load Data

URL <- "https://raw.githubusercontent.com/DS4PS/cpp-523-fall-2019/master/labs/class-size-seed-1234.csv"
dat <- read.csv( URL )
test csize tqual ses
504 38 3.793 0.7947
651.3 23 5.277 2.301
623.6 42 6.084 0.4609
539 21 2.654 2.635
673.1 20 5.429 2.703
584.4 46 5.506 0.09412

Solutions

PART I:

Question 1.

Create a scatterplot between Class Size (x-axis) and Test Score (y-axis). This will serve as a visual representation of our baseline model of the relationship between Class Size and Test Score.

plot( dat$csize, dat$test, 
      xlab="Class Size", ylab="Test Scores",
      main="Relationship Between Class Size and Test Scores" )

abline( lm(test~csize,data=dat), col="firebrick", lwd=2 )

# CHANGE THE PLOT STYLE:
#
# pch=19             # change point style
# cex=1.5            # change point size
# col="firebrick"    # change color
# bty="n"            # remove bounding box

Question 2.

Regress Test Score on Teacher Quality while saving the residuals. Now create a scatterplot of Class Size and the residuals of Test Score. What happened to the strength of the relationship? Why?

\(test = b_0 + b_1 \cdot tqual + e1\)

model.01 <- lm( test ~ tqual, data=dat )
e1.test.score <- model.01$residual
plot( dat$csize, e1.test.score )

Answer: The relationship between class size and test scores remains relatively strong. This is because teacher quality affects test scores, but it is not strongly related to class size. When teacher quality is removed, it mainly removes random variation in test scores. As a result, the estimate becomes more precise, but the relationship between class size and test scores does not change very much.

Question 3.

Regress Test Score on SES and save the residuals. Create a scatterplot of Class Size and the residuals of Test Score. What happened to the strength of the relationship? Why?

\(test = b_0 + b_1 \cdot ses + e2\)

model.02 <- lm( test ~ ses, data=dat )
e2.test.score <- model.02$residual
plot( dat$csize, e2.test.score )

Answer: The relationship between class size and test scores becomes weaker. This is because SES is related to both class size and test scores. Schools with higher SES tend to have smaller classes and higher test scores. When SES is removed, some of the relationship between class size and test scores disappears because SES was influencing both variables.

Question 4.

These graphs demonstrate the effects of adding the control variables Teacher Quality and SES to the baseline model of the relationship between Class Size and Test Score. Conceptually, the control variable will improve a model by removing variance in the DV to make the estimate either more precise (smaller standard errors) or less bias (adjust the slope of the policy variable).

Q 4-1: Which control variable do you think is removing UNEXPLAINED portions of the outcome Test Scores? By removing the unexplained portions it will reduce the residuals in the full model.

Answer: Teacher Quality is the control variable that removes unexplained portions of test scores. This is because it explains differences in test scores but is not strongly related to class size. By removing this variation, the model becomes more precise and the standard errors become smaller.

Q 4-2: Which control variable is removing the EXPLAINED portion of the variance of test scores (the covariance of class size and test scores)? By removing the explained portion it will weaken the relationship between class size and test scores.

Answer: SES is the control variable that removes the explained portion of the variance in test scores. This is because SES is related to both class size and test scores. When SES is removed, the relationship between class size and test scores becomes weaker because part of that relationship was due to SES.



PART II:

Use the following regression table and graphs to answer the question.

Question 5-1

Based upon the correlation structure reported below, which control variable do you expect would change the slope of caffeine if removed from the model?

Explain your reasoning.

Answer: Stress Index would change the slope of caffeine if it were removed. This is because stress is related to both caffeine use and heart rate. Removing it could change the estimated effect of caffeine on heart rate.

Question 5-2

Which would result in a larger standard error associated with caffeine if removed from the model?

Explain your reasoning.

Answer: Gym Time would result in a larger standard error if it were removed. This is because gym time explains a lot of the variation in heart rate. Without it, there would be more unexplained variation in the model, making the estimate less precise.

Dependent variable:
heart.rate
caffeine 0.037
(0.047)
stress.index 0.228
(0.246)
gym.time -1.440***
(0.062)
Constant 116.022***
(2.982)
Observations 100
R2 0.873
Residual Std. Error 11.016 (df = 96)
Note: p<0.1; p<0.05; p<0.01



Submission Instructions

After you have completed your lab, knit your RMD file. Login to Canvas at http://canvas.asu.edu and navigate to the assignments tab in the course repository. Upload your RMD and your HTML files to the appropriate lab submission link.

Remember to: