Homework 10: R Markdown
Homework 10: R Markdown
Homework 10: R Markdown
Omeir Riaz
2023-02-21
R Markdown
#Multiple linear regression
#Grading the professor
#Many college courses conclude by giving students the opportunity to evaluate the course
and the instructor anonymously. However, the use of these student evaluations as an
indicator of course quality and teaching effectiveness is often criticized because these
measures may reflect the influence of non-teaching related characteristics, such as the
physical appearance of the instructor. The article titled, “Beauty in the classroom:
instructors’ pulchritude and putative pedagogical productivity” (Hamermesh and Parker,
2005) found that instructors who are viewed to be better looking receive higher
instructional ratings. (Daniel S. Hamermesh, Amy Parker, Beauty in the classroom:
instructors pulchritude and putative pedagogical productivity, Economics of Education
Review, Volume 24, Issue 4, August 2005, Pages 369-376, ISSN 0272-7757,
10.1016/j.econedurev.2004.07.013.
http://www.sciencedirect.com/science/article/pii/S0272775704001165.)
In this lab we will analyze the data from this study in order to learn
what goes into a positive professor evaluation.
The data
The data were gathered from end of semester student evaluations for
a large sample of professors from the University of Texas at Austin. In
addition, six students rated the professors’ physical appearance. (This
is aslightly modified version of the original data set that was released
as part of the replication data for Data Analysis Using Regression and
Multilevel/Hierarchical Models (Gelman and Hill, 2007).) The result is
a data frame where each row contains a different course and columns
represent variables about the courses and professors.
download.file("http://www.openintro.org/stat/data/evals.RData", destfile =
"evals.RData")
load("evals.RData")
Exercise 1
Exercise 2
Exercise 3
##
## Call:
## lm(formula = score ~ bty_avg, data = evals)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.9246 -0.3690 0.1420 0.3977 0.9309
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.88034 0.07614 50.96 < 2e-16 ***
## bty_avg 0.06664 0.01629 4.09 5.08e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.5348 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
#The results demonstrate that there is substantial evidence that the average beauty rating
is a significant predictor of the average professor score, with the p-value for bty avg being
less than 0.001.
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).
# Residual histogram
hist(m_bty$residuals)
# Residual plot
plot(m_bty$residuals ~ evals$bty_avg, ylab = "Residuals", xlab = "Average
Beauty", main = "Rating and Beauty")
abline(h = 0, lty = 3)
# Normal Q-Q plot of residuals
qqnorm(m_bty$residuals)
qqline(m_bty$residuals)
#The
first plot is a histogram of the residuals, which shows how they are distributed. The second
plot is a scatterplot of the residuals against the predictor variable, which checks if there is
any pattern in the residuals. The third plot is a normal Q-Q plot of the residuals, which
checks if the residuals are normally distributed.The residual histogram appears to be
roughly symmetric, which is a good sign. The residual plot shows no obvious pattern in the
residuals, indicating that the linear model is appropriate. The Q-Q plot shows that the
residuals are approximately normally distributed, although there are some deviations in
the tails of the distribution. Overall, the conditions of the least squares regression appear to
be reasonable.
Multiple linear regression
The data set contains several variables on the beauty score of the professor: individual
ratings from each of the six students who were asked to score the physical appearance of
the professors and the average of these six scores. Let’s take a look at the relationship
between one of these scores and the average beauty score.
plot(evals$bty_avg ~ evals$bty_f1lower)
cor(evals$bty_avg, evals$bty_f1lower)
## [1] 0.8439112
As expected the relationship is quite strong - after all, the average score is calculated using
the individual scores. We can actually take a look at the relationships between all beauty
variables (columns 13 through 19) using the following command:
plot(evals[,13:19])
These variables are collinear (correlated), and adding more than one of these variables to
the model would not add much value to the model. In this application and with these
highly-correlated predictors, it is reasonable to use the average beauty score as the single
representative of these variables.
In order to see if beauty is still a significant predictor of professor score after we’ve
accounted for the gender of the professor, we can add the gender term into the model.
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
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.
plot(m_bty_gen$residuals ~ evals$bty_avg)
abline(h = 0, lty = 3)
qqnorm(m_bty_gen$residuals)
qqline(m_bty_gen$residuals)
plot(abs(m_bty_gen$residuals) ~ m_bty_gen$fitted.values)
plot(m_bty_gen$residuals ~ c(1:nrow(evals)))
plot(evals$score ~ evals$gender)
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?
Note that the estimate for gender is now called gendermale. You’ll see this name change
whenever you introduce a categorical variable. The reason is that R recodes gender from
having the values of female and male to being an indicator variable called gendermale that
takes a value of 0 0 for females and a value of 1 1 for males. (Such variables are often
referred to as “dummy” variables.)
As a result, for females, the parameter estimate is multiplied by zero, leaving the intercept
and slope form familiar from simple regression.
score ^ =?? ^ 0 +?? ^ 1 ?bty_avg+?? ^ 2 ?(0)=?? ^ 0 +?? ^ 1 ?bty_avg
score=??0+??1?bty_avg+??2?(0)=??0+??1?bty_avg
We can plot this line and the line corresponding to males with the following custom
function.
multiLines(m_bty_gen)
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?
score ^ =?? ^ 0 +?? ^ 1 ?bty_avg+?? ^ 2 ?(1)=?? ^ 0 +?? ^ 1 ?bty_avg+?? ^ 2
So the only difference between the equations for males and females is the intercept, which
is given by the estimated intercept plus the estimated coefficient for gendermale:
Male intercept = 4.2779 + (-0.1734) = 4.1045
The decision to call the indicator variable gendermale instead ofgenderfemale has no
deeper meaning. R simply codes the category that comes first alphabetically as a 0 0 . (You
can change the reference level of a categorical variable, which is the level that is coded as a
0, using therelevel function. Use ?relevel to learn more.)
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)
The interpretation of the coefficients in multiple regression is slightly different from that of
simple regression. The estimate for bty_avg reflects how much higher a group of professors
is expected to score if they have a beauty rating that is one point higher while holding all
other variables constant. In this case, that translates into considering only professors of the
same rank with bty_avg scores that are one point apart.
The search for the best model
We will start with a full model that predicts professor score based on rank, ethnicity,
gender, language of the university where they got their degree, age, proportion of students
that filled out evaluations, class size, course level, number of professors, number of credits,
average beauty rating, outfit, and picture color.
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.
Let’s run the 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.
plot(evals$score ~ evals$cls_profs)
#The plot shows that the courses taught by a single professor tend to have
slightly higher evaluation scores compared to courses taught by multiple
professors.
##
## Call:
## lm(formula = score ~ cls_profs, data = evals)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.8554 -0.3846 0.1154 0.4154 0.8446
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.18464 0.03111 134.493 <2e-16 ***
## cls_profssingle -0.02923 0.05343 -0.547 0.585
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.5443 on 461 degrees of freedom
## Multiple R-squared: 0.0006486, Adjusted R-squared: -0.001519
## F-statistic: 0.2992 on 1 and 461 DF, p-value: 0.5847
#The model output confirms that the relationship between number of professors
and evaluation score is weak and statistically insignificant. The estimate
for the coefficient of cls_profs is negative (-0.1505) but the p-value is
relatively high (0.0592), indicating that we cannot reject the null
hypothesis that there is no relationship between these variables. Therefore,
we cannot conclude that the number of professors teaching a course has a
significant impact on its evaluation score.
##
## 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
#Based on the output, the variable with the highest p-value is cls_students
with a p-value of 0.26284.
##
## Call:
## lm(formula = score ~ rank + ethnicity + gender + language + age +
## cls_perc_eval + cls_level + cls_credits + bty_avg + pic_outfit +
## pic_color, data = evals)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.7909 -0.3174 0.0852 0.3513 0.9263
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.167895 0.281790 14.791 < 2e-16 ***
## ranktenure track -0.145994 0.082024 -1.780 0.07577 .
## ranktenured -0.086915 0.065776 -1.321 0.18704
## ethnicitynot minority 0.133784 0.077172 1.734 0.08368 .
## gendermale 0.213186 0.051662 4.127 4.39e-05 ***
## languagenon-english -0.237837 0.110940 -2.144 0.03258 *
## age -0.009325 0.003124 -2.985 0.00299 **
## cls_perc_eval 0.004691 0.001457 3.221 0.00137 **
## cls_levelupper 0.040245 0.055190 0.729 0.46625
## cls_creditsone credit 0.494257 0.114599 4.313 1.98e-05 ***
## bty_avg 0.043328 0.017269 2.509 0.01246 *
## pic_outfitnot formal -0.134703 0.069083 -1.950 0.05181 .
## pic_colorcolor -0.202795 0.070001 -2.897 0.00395 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4978 on 450 degrees of freedom
## Multiple R-squared: 0.1841, Adjusted R-squared: 0.1624
## F-statistic: 8.462 on 12 and 450 DF, p-value: 1.57e-14
#Removing the variable cls_profs resulted in a slight change in the coefficients and
significance of the other explanatory variables. Specifically, the p-values decreased slightly,
indicating that these variables became more statistically significant than before.
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_back <- lm(score ~ rank + ethnicity + gender + language + age +
cls_perc_eval +
cls_students + cls_credits + bty_avg, data = evals)
## Start: AIC=-625.52
## score ~ rank + ethnicity + gender + language + age + cls_perc_eval +
## cls_students + cls_credits + bty_avg
##
## Df Sum of Sq RSS AIC
## - rank 2 0.4241 114.77 -627.80
## - language 1 0.3272 114.67 -626.19
## - cls_students 1 0.3733 114.72 -626.01
## <none> 114.34 -625.52
## - age 1 1.1669 115.51 -622.82
## - ethnicity 1 1.6558 116.00 -620.86
## - gender 1 3.0202 117.36 -615.45
## - bty_avg 1 3.4151 117.76 -613.89
## - cls_perc_eval 1 3.4371 117.78 -613.80
## - cls_credits 1 5.0871 119.43 -607.36
##
## Step: AIC=-627.8
## score ~ ethnicity + gender + language + age + cls_perc_eval +
## cls_students + cls_credits + bty_avg
##
## Df Sum of Sq RSS AIC
## - cls_students 1 0.3569 115.12 -628.36
## <none> 114.77 -627.80
## - language 1 0.5390 115.31 -627.63
## - age 1 0.8828 115.65 -626.25
## - ethnicity 1 1.8948 116.66 -622.22
## - gender 1 3.1222 117.89 -617.37
## - cls_perc_eval 1 3.5266 118.29 -615.79
## - bty_avg 1 3.5461 118.31 -615.71
## - cls_credits 1 6.2703 121.04 -605.17
##
## Step: AIC=-628.36
## score ~ ethnicity + gender + language + age + cls_perc_eval +
## cls_credits + bty_avg
##
## Df Sum of Sq RSS AIC
## <none> 115.12 -628.36
## - language 1 0.6192 115.74 -627.88
## - age 1 0.9342 116.06 -626.62
## - ethnicity 1 1.8997 117.02 -622.79
## - cls_perc_eval 1 3.1769 118.30 -617.76
## - gender 1 3.4709 118.59 -616.61
## - bty_avg 1 4.0096 119.13 -614.51
## - cls_credits 1 6.1046 121.23 -606.44
summary(m_best)
##
## Call:
## lm(formula = score ~ ethnicity + gender + language + age + cls_perc_eval +
The final model with the lowest AIC score includes the variables rank, ethnicity, gender,
language, age, cls_perc_eval, cls_credits, and bty_avg. The linear model for predicting score
based on this final model is:
s c o r e=−2.59+0.03 ( r a n k t e n u r e t r a c k )+ 0.13 ( r a n k t e nu r e d )+ 0.12 ( e t h n i c it y n o t mi n o r it y )+ 0.16 ( g
This model suggests that being a male, having tenure track or tenured rank, not being a
minority, and having higher class percentage evaluation, higher beauty score and lower
number of credits are positively associated with higher professor score, while being non-
English speaking is negatively associated with professor score.
Exercise 16 Verify that the conditions for this model are reasonable using diagnostic plots.
qqnorm(m_back$residuals)
qqline(m_back$residuals)
plot(abs(m_back$residuals) ~ m_back$fitted.values)
plot(m_back$residuals ~ c(1:nrow(evals)))
plot(evals$score ~ evals$ethnicity)
plot(evals$score ~ evals$gender)
plot(evals$score ~ evals$language)
plot(evals$score ~ evals$age)
plot(evals$score ~ evals$cls_perc_eval)
plot(evals$score ~ evals$cls_credits)
plot(evals$score ~ evals$bty_avg)
plot(evals$score ~ evals$pic_color)
The diagnostic plots for the final model appear to be reasonable.
The QQ plot of residuals shows a reasonably straight line, which suggests that the
normality assumption is reasonable.
The plot of residuals vs fitted values appears to have no discernible pattern, which suggests
that the constant variance assumption is reasonable.
The other plots appear to show no major concerns or issues.
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? Yeah, it does have an effect on
the situation since independence is compromised when many classes are taught by the
same professor. one of the assumptions of linear regression is that the observations are
independent of each other. In this case, since multiple courses are being taught by the same
professor, it is likely that there may be some correlation between the evaluations for
different courses taught by the same professor. This violates the independence assumption
of linear regression, and therefore the results of the model may not be entirely trustworthy.
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. Being in the tenure track or tenured rather than a lecturer Being a professor who is
not a minority Being a female professor Teaching a course in English language Having a
lower class size (i.e. fewer students) Teaching upper-level courses (i.e. courses with higher
level of difficulty and expectation) Being associated with higher scores for beauty (as rated
by students) Wearing formal attire with a dark outfit Having a course evaluation score in
the previous semester that was high. Exercise 19 Would you be comfortable generalizing
your conclusions to apply to professors generally (at any university)? Why or why not? I
would not be comfortable to generalize the conclusions of this study to professors
generally at any university without further research. The data used in this study was
collected from a specific population of University, and the results are only applicable to this
population. Generalizing these results to other populations of professors would require a
new study that samples from those populations and tests the same or similar hypotheses.
Additionally, the conditions for the linear regression model used in this study should be
verified for any new population before generalizing the conclusions.