Multiple Regression Lab

Author

K Kendal

Multiple Regression Lab

Load The Dataset and libraries

library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.4.0      ✔ purrr   1.0.1 
✔ tibble  3.1.8      ✔ dplyr   1.0.10
✔ tidyr   1.3.0      ✔ stringr 1.5.0 
✔ readr   2.1.3      ✔ forcats 0.5.2 
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
download.file("http://www.openintro.org/stat/data/evals.RData", destfile = "evals.RData")
load("evals.RData")

Exercise 1: Is this an observational study or an experiment? The original research question posed in the paper is whether beauty leads directly to the differences in course evaluations. Given the study design, is it possible to answer this question as it is phrased? If not, rephrase the question.

Observational study. Not a randomized, fully controlled study, so not possible to answer the question as phrased.

Exercise 2: Describe the distribution of score. Is the distribution skewed? What does that tell you about how students rate courses? Is this what you expected to see? Why, or why not?

Create a histogram using ggplot

plot1 <- evals %>%
  ggplot(aes(score)) +
  geom_histogram(fill= "turquoise", bins = 20)
plot1

#single quantitative variables

Left skewed.

Exercise 3: Excluding score, select two other variables and describe their relationship using an appropriate visualization (scatterplot, side-by-side boxplots, or mosaic plot).

#age

plot3 <- evals %>%
  ggplot(aes(age)) +
  geom_histogram(fill= "red", bins = 18)
plot3

Somewhat normally distributed, but with a gap around 53-55 years, and another at approximately 66-68 years.

#rank

plot4 <- evals %>%
  ggplot(aes(x= bty_avg, y= rank, color = rank)) +
  geom_point() +
  ggtitle("Scatterplot of Prof Rank by Beauty Scores") +
  xlab("Average Beauty Scores Assessed by Students") +
  ylab("Average Professor Rank")
plot4

All 3 ranks covered just about all the levels of beauty scores; however tenured had the most responses and was also the most normally distributed.

Simple linear regression

plot2 <- evals %>%
  ggplot(aes(x= bty_avg, y= score, color = gender)) +
  geom_point() +
  ggtitle("Scatterplot of Prof Scores by Beauty Scores") +
  xlab("Average Beauty Scores Assessed by Students") +
  ylab("Average Professor Evaluation Score")
plot2

Exercise 4: Replot the scatterplot, but this time use the function jitter() on the y or the x coordinate. (Use ?jitter to learn more.) What was misleading about the initial scatterplot?

plot(jitter(evals$score) ~ evals$bty_avg)

The original scatterplot didn’t contain all of the data points that the jitter plot does. The original took points that are mostly overlapping and merged them.

Exercise 5: Let’s see if the apparent trend in the plot is something more than natural variation. Fit a linear model called m_bty to predict average professor score by average beauty rating and add the line to your plot using abline(m_bty). Write out the equation for the linear model and interpret the slope. Is average beauty score a statistically significant predictor? Does it appear to be a practically significant predictor?

plot2 <- evals %>%
  ggplot(aes(x= bty_avg, y= score, color = gender)) +
  geom_point() +
  geom_smooth(method= "lm") +
  ggtitle("Scatterplot of Prof Scores by Beauty Scores") +
  xlab("Average Beauty Scores Assessed by Students") +
  ylab("Average Professor Evaluation Score")
plot2
`geom_smooth()` using formula = 'y ~ x'

Exercise 6: Use residual plots to evaluate whether the conditions of least squares regression are reasonable. Provide plots and comments for each one (see the Simple Regression Lab for a reminder of how to make these).

plot6 <- lm(bty_avg ~ score, data = evals)
summary(plot6)

Call:
lm(formula = bty_avg ~ score, data = evals)

Residuals:
    Min      1Q  Median      3Q     Max 
-2.7116 -1.2116 -0.2032  0.9328  4.2089 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   2.2237     0.5409   4.111 4.66e-05 ***
score         0.5256     0.1285   4.090 5.08e-05 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.502 on 461 degrees of freedom
Multiple R-squared:  0.03502,   Adjusted R-squared:  0.03293 
F-statistic: 16.73 on 1 and 461 DF,  p-value: 5.083e-05
plot(evals$bty_avg ~ evals$score)
abline(plot6)

Multiple Linear Regression

plot(evals$bty_avg ~ evals$bty_f1lower)

cor(evals$bty_avg, evals$bty_f1lower)
[1] 0.8439112
plot(evals[,13:19])

m_bty_gen <- lm(score ~ bty_avg + gender, data = evals)
summary(m_bty_gen)

Call:
lm(formula = score ~ bty_avg + gender, data = evals)

Residuals:
    Min      1Q  Median      3Q     Max 
-1.8305 -0.3625  0.1055  0.4213  0.9314 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  3.74734    0.08466  44.266  < 2e-16 ***
bty_avg      0.07416    0.01625   4.563 6.48e-06 ***
gendermale   0.17239    0.05022   3.433 0.000652 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.5287 on 460 degrees of freedom
Multiple R-squared:  0.05912,   Adjusted R-squared:  0.05503 
F-statistic: 14.45 on 2 and 460 DF,  p-value: 8.177e-07

#female is reference level #beauty score adds .074 #being male adds .172 #not a good model: Adjusted R-squared is really small (only 5.5% is explained by this model)

Exercise 7: P-values and parameter estimates should only be trusted if the conditions for the regression are reasonable. Verify that the conditions for this model are reasonable using diagnostic plots.

fit3 <- lm(score ~ bty_avg + gender + bty_avg*gender, data = evals)
summary(fit3)

Call:
lm(formula = score ~ bty_avg + gender + bty_avg * gender, data = evals)

Residuals:
    Min      1Q  Median      3Q     Max 
-1.8084 -0.3828  0.0903  0.4037  0.9211 

Coefficients:
                   Estimate Std. Error t value Pr(>|t|)    
(Intercept)         3.95006    0.11800  33.475   <2e-16 ***
bty_avg             0.03064    0.02400   1.277   0.2024    
gendermale         -0.18351    0.15349  -1.196   0.2325    
bty_avg:gendermale  0.07962    0.03247   2.452   0.0146 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.5258 on 459 degrees of freedom
Multiple R-squared:  0.07129,   Adjusted R-squared:  0.06522 
F-statistic: 11.74 on 3 and 459 DF,  p-value: 1.997e-07
plot(fit3)

Based on the diagnostic plots, the conditions for linear regression have been met.

Exercise 8: Is bty_avg still a significant predictor of score? Has the addition of gender to the model changed the parameter estimate for bty_avg?

multiLines(m_bty_gen)

Bty_avg is still a good predictor of score, though adding gender into the mix does have an impact. It appears as though males will have an overall higher score in general.

Exercise 9: What is the equation of the line corresponding to males? (Hint: For males, the parameter estimate is multiplied by 1.) For two professors who received the same beauty rating, which gender tends to have the higher course evaluation score?

y=3.747 +.172x

males have the higher score

Exercise 10: Create a new model called m_bty_rank with gender removed and rank added in. How does R appear to handle categorical variables that have more than two levels? Note that the rank variable has three levels: teaching, tenure track, tenured.

m_bty_rank <- lm(score ~ bty_avg + rank, data = evals)
summary(m_bty_rank)

Call:
lm(formula = score ~ bty_avg + rank, data = evals)

Residuals:
    Min      1Q  Median      3Q     Max 
-1.8713 -0.3642  0.1489  0.4103  0.9525 

Coefficients:
                 Estimate Std. Error t value Pr(>|t|)    
(Intercept)       3.98155    0.09078  43.860  < 2e-16 ***
bty_avg           0.06783    0.01655   4.098 4.92e-05 ***
ranktenure track -0.16070    0.07395  -2.173   0.0303 *  
ranktenured      -0.12623    0.06266  -2.014   0.0445 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.5328 on 459 degrees of freedom
Multiple R-squared:  0.04652,   Adjusted R-squared:  0.04029 
F-statistic: 7.465 on 3 and 459 DF,  p-value: 6.88e-05
multiLines(m_bty_rank)

R makes one of them the standard that the others are compared to

The search for the best model

Exercise 11: Which variable would you expect to have the highest p-value in this model? Why? Hint: Think about which variable would you expect to not have any association with the professor score.

I would think level of class taught would have the highest p-value. That seems to be the least relevant toward the beauty score.

Full model

m_full <- lm(score ~ rank + ethnicity + gender + language + age + cls_perc_eval 
             + cls_students + cls_level + cls_profs + cls_credits + bty_avg 
             + pic_outfit + pic_color, data = evals)
summary(m_full)

Call:
lm(formula = score ~ rank + ethnicity + gender + language + age + 
    cls_perc_eval + cls_students + cls_level + cls_profs + cls_credits + 
    bty_avg + pic_outfit + pic_color, data = evals)

Residuals:
     Min       1Q   Median       3Q      Max 
-1.77397 -0.32432  0.09067  0.35183  0.95036 

Coefficients:
                        Estimate Std. Error t value Pr(>|t|)    
(Intercept)            4.0952141  0.2905277  14.096  < 2e-16 ***
ranktenure track      -0.1475932  0.0820671  -1.798  0.07278 .  
ranktenured           -0.0973378  0.0663296  -1.467  0.14295    
ethnicitynot minority  0.1234929  0.0786273   1.571  0.11698    
gendermale             0.2109481  0.0518230   4.071 5.54e-05 ***
languagenon-english   -0.2298112  0.1113754  -2.063  0.03965 *  
age                   -0.0090072  0.0031359  -2.872  0.00427 ** 
cls_perc_eval          0.0053272  0.0015393   3.461  0.00059 ***
cls_students           0.0004546  0.0003774   1.205  0.22896    
cls_levelupper         0.0605140  0.0575617   1.051  0.29369    
cls_profssingle       -0.0146619  0.0519885  -0.282  0.77806    
cls_creditsone credit  0.5020432  0.1159388   4.330 1.84e-05 ***
bty_avg                0.0400333  0.0175064   2.287  0.02267 *  
pic_outfitnot formal  -0.1126817  0.0738800  -1.525  0.12792    
pic_colorcolor        -0.2172630  0.0715021  -3.039  0.00252 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.498 on 448 degrees of freedom
Multiple R-squared:  0.1871,    Adjusted R-squared:  0.1617 
F-statistic: 7.366 on 14 and 448 DF,  p-value: 6.552e-14

Exercise 12: Check your suspicions from the previous exercise. Include the model output in your response.

Cls_profssingle had the highest p-value, with an output of 0.77806

Exercise 13: Interpret the coefficient associated with the ethnicity variable.

The ethnicity variable has a 0.1235 greater slope than the intercept, which is the not-tenured professor.

Exercise 14: Drop the variable with the highest p-value and re-fit the model. Did the coefficients and significance of the other explanatory variables change? If not, what does this say about whether or not the dropped variable was collinear with the other explanatory variables?

m_full2 <- lm(score ~ rank + ethnicity + gender + language + age + cls_perc_eval 
             + cls_students + cls_level + cls_credits + bty_avg 
             + pic_outfit + pic_color, data = evals)
summary(m_full2)

Call:
lm(formula = score ~ rank + ethnicity + gender + language + age + 
    cls_perc_eval + cls_students + cls_level + cls_credits + 
    bty_avg + pic_outfit + pic_color, data = evals)

Residuals:
    Min      1Q  Median      3Q     Max 
-1.7836 -0.3257  0.0859  0.3513  0.9551 

Coefficients:
                        Estimate Std. Error t value Pr(>|t|)    
(Intercept)            4.0872523  0.2888562  14.150  < 2e-16 ***
ranktenure track      -0.1476746  0.0819824  -1.801 0.072327 .  
ranktenured           -0.0973829  0.0662614  -1.470 0.142349    
ethnicitynot minority  0.1274458  0.0772887   1.649 0.099856 .  
gendermale             0.2101231  0.0516873   4.065 5.66e-05 ***
languagenon-english   -0.2282894  0.1111305  -2.054 0.040530 *  
age                   -0.0089992  0.0031326  -2.873 0.004262 ** 
cls_perc_eval          0.0052888  0.0015317   3.453 0.000607 ***
cls_students           0.0004687  0.0003737   1.254 0.210384    
cls_levelupper         0.0606374  0.0575010   1.055 0.292200    
cls_creditsone credit  0.5061196  0.1149163   4.404 1.33e-05 ***
bty_avg                0.0398629  0.0174780   2.281 0.023032 *  
pic_outfitnot formal  -0.1083227  0.0721711  -1.501 0.134080    
pic_colorcolor        -0.2190527  0.0711469  -3.079 0.002205 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.4974 on 449 degrees of freedom
Multiple R-squared:  0.187, Adjusted R-squared:  0.1634 
F-statistic: 7.943 on 13 and 449 DF,  p-value: 2.336e-14

Generally speaking, the variables that came after the removed variable were impacted more than the ones that came before. For example, age as a variable had little change in it’s coefficient, whereas the color/BW variable for the picture did have some change in the coefficient. Nothing changed drastically, but there is more impact after the removed variable.

Exercise 15: Using backward-selection and p-value as the selection criterion, determine the best model. You do not need to show all steps in your answer, just the output for the final model. Also, write out the linear model for predicting score based on the final model you settle on.

m_full3 <- lm(score ~ ethnicity + gender + age + pic_outfit*pic_color + age*gender
              + cls_credits + bty_avg 
             + pic_color, data = evals)
summary(m_full3)

Call:
lm(formula = score ~ ethnicity + gender + age + pic_outfit * 
    pic_color + age * gender + cls_credits + bty_avg + pic_color, 
    data = evals)

Residuals:
    Min      1Q  Median      3Q     Max 
-1.8498 -0.3338  0.1019  0.3786  1.0712 

Coefficients:
                                     Estimate Std. Error t value Pr(>|t|)    
(Intercept)                          4.622986   0.273650  16.894  < 2e-16 ***
ethnicitynot minority                0.200090   0.071152   2.812  0.00513 ** 
gendermale                          -0.608015   0.255517  -2.380  0.01775 *  
age                                 -0.018500   0.004350  -4.253 2.56e-05 ***
pic_outfitnot formal                 0.047172   0.130203   0.362  0.71730    
pic_colorcolor                      -0.092984   0.135271  -0.687  0.49219    
cls_creditsone credit                0.600414   0.104171   5.764 1.52e-08 ***
bty_avg                              0.053780   0.017344   3.101  0.00205 ** 
pic_outfitnot formal:pic_colorcolor -0.146881   0.156664  -0.938  0.34897    
gendermale:age                       0.016889   0.005363   3.149  0.00174 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.5005 on 453 degrees of freedom
Multiple R-squared:  0.1694,    Adjusted R-squared:  0.1529 
F-statistic: 10.27 on 9 and 453 DF,  p-value: 1.837e-14

Exercise 16: Verify that the conditions for this model are reasonable using diagnostic plots.

plot(m_full3)

Constant variability and normal distribution are still mostly intact.

Exercise 17: The original paper describes how these data were gathered by taking a sample of professors from the University of Texas at Austin and including all courses that they have taught. Considering that each row represents a course, could this new information have an impact on any of the conditions of linear regression?

If each row represents a course rather than a professor, than there could be impact to the conditions of linear regression. If a professor teaches multiple courses, then data will be skewed toward that professor, which would potentially violate the distribution of the data.

Exercise 18: Based on your final model, describe the characteristics of a professor and course at University of Texas at Austin that would be associated with a high evaluation score.

Casual picture in color, male, younger, white, one credit courses.

Exercise 19: Would you be comfortable generalizing your conclusions to apply to professors generally (at any university)? Why or why not?

No. Different areas have different physical standards. Younger, white, male professors aren’t the most appealing to everybody everywhere.