##
## Welcome to CUNY DATA606 Statistics and Probability for Data Analytics
## This package is designed to support this course. The text book used
## is OpenIntro Statistics, 3rd Edition. You can read this by typing
## vignette('os3') or visit www.OpenIntro.org.
##
## The getLabs() function will return a list of the labs available.
##
## The demo(package='DATA606') will list the demos that are available.
##
## Attaching package: 'DATA606'
## The following object is masked from 'package:utils':
##
## demo
## [1] "C:/Users/stina/Documents/CUNY SPS Data Science/Spring 2018 Classes/DATA 606 - Probability and Statistics/Lab8/Lab8/stina-multiple_regression.Rmd"
## [1] "C:/Users/stina/Documents/CUNY SPS Data Science/Spring 2018 Classes/DATA 606 - Probability and Statistics/Lab8"
This is an observational study.
Given the study design, I don’t think that it is possible to answer this question as it is phrased.
Perhaps it should be phrased as does beauty have an influence in students’ ratings.
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?As you can see the distribution is not symmetrical and is skewed. Most students rate their courses with a high score. I expected a bell shaped distribution because I imagined that most would give their an average rating. But this distribution tells me that most students are pleased with their instructors.
hist(evals$score)
score
, select two other variables and describe their relationship using an appropriate visualization (scatterplot, side-by-side boxplots, or mosaic plot).Based on the side-by-side boxplot below, tenure track professors tend to be younger than teaching or tenured professors.
boxplot(evals$age ~ evals$rank)
The fundamental phenomenon suggested by the study is that better looking teachers are evaluated more favorably. Let’s create a scatterplot to see if this appears to be the case:
plot(evals$score ~ evals$bty_avg)
Before we draw conclusions about the trend, compare the number of observations in the data frame with the approximate number of points on the scatterplot. Is anything awry?
There are 463 observations.
jitter()
on the y or the x-coordinate. (Use ?jitter
to learn more.) What was misleading about the initial scatterplot?I applied the jitter on both the x and y coordinates below; however, I am not sure what the misleading aspect of the original plot above. Perhaps the the plot above makes it look like no two professors have the same ratings since none of the plots touch each other.
plot(evals$score ~ jitter(evals$bty_avg))
plot(jitter(evals$score) ~ evals$bty_avg)
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?score = 3.88034 + bty_avg*0.06664
The slope is the estimated difference in score if bty_avg is one unit larger. Yes, based on the P value below for bty_avg, it is statistically significant.
m_bty <- lm(score ~ bty_avg, data = evals)
m_bty
##
## Call:
## lm(formula = score ~ bty_avg, data = evals)
##
## Coefficients:
## (Intercept) bty_avg
## 3.88034 0.06664
summary(m_bty)
##
## 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
Coefficients: (Intercept) x
3.1128 0.1187
Sum of Squares: 268.531
plot_ss(x = evals$bty_avg, y = evals$score)
## Click two points to make a line.
## Call:
## lm(formula = y ~ x, data = pts)
##
## Coefficients:
## (Intercept) x
## 3.88034 0.06664
##
## Sum of Squares: 131.868
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)
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)
These are the conditions.
Linearity. The data should show a linear trend.
Nearly normal residuals.
Constant variability.
Independent observations
bty_avg
still a significant predictor of score
? Has the addition of gender
to the model changed the parameter estimate for bty_avg
?Yes, the bty_avg
is still a significant predictor of score. Yes, gender
has 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\) for females and a value of \(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.
We can plot this line and the line corresponding to males with the following custom function.
multiLines(m_bty_gen)
score = 3.74734 + bty_avg(0.07416) + gendermal(0.17239)
The male professor tends to have the higher course evaluation for the same beauty rating.
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\). (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.)
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
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.
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.
I’m thinking the size of the class. This should not have much association with the professor’s score.
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)
Based on the results, cls_profssingle has the highest p-value.
The ethnicitynot minority
contributes a difference of .1235 to the score when the professor is not a minority.
Original adjusted R-square: 0.1617 New adjusted R-squre: 0.1634 –> after dropping cls_profs.
m_full <- 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_full)
Remove: rank (above 5%) ethnicity (above 5%) cls_students (above 5%) cls_levelupper (above 5%) pic_outfit (above 5%)
m_full <- lm(score ~ gender + language + age + cls_perc_eval
+ cls_credits + bty_avg
+ pic_color, data = evals)
summary(m_full)
Adjusted R-squared: 0.1502
Verify that the conditions for this model are reasonable using diagnostic plots.
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?
Indepedence. Some rows may represent the same professors. So this isn’t really independent.
Male, speaks english, with high average beauty rating, has a black and white photo young.
No. This is simply an observational study using limited data at this particular university.