\(~\)
This lab covers a few topics in multivariable regression modeling, specifically:
During the first part of the lab you will use the professor salary data. Recall that these data were collected by a large, public university as part of an investigation into possible sex-based differences in faculty pay.
fac_sal = read.csv("https://remiller1450.github.io/data/Salaries.csv")
\(~\)
The \(F\)-test for comparing the fit of nested regression models is fundamentally the same test we learned about when studying one-way ANOVA. So, similarly, the first step in performing the test is to fit the two models of interest. For example:
smaller_model = lm(salary ~ sex, data = fac_sal)
larger_model = lm(salary ~ sex + rank, data = fac_sal)
Next we use the anova() function to perform the test on
these fitted models:
anova(smaller_model, larger_model)
## Analysis of Variance Table
##
## Model 1: salary ~ sex
## Model 2: salary ~ sex + rank
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 395 3.5632e+11
## 2 393 2.1923e+11 2 1.3709e+11 122.88 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Each model’s sum of squared residuals is provided in the “RSS”
column, and the degrees of freedom indicate the number of additional
parameters used by the larger model. In this example, those parameters
are the slope coefficients on the two dummy variables used to encode
categories of rank.
The small \(p\)-value indicates the
residuals are significantly smaller for the larger model, thus providing
strong evidence that the additional predictors (the two dummy variables
used to encode rank) improve model fit.
Because the \(F\)-test relies on the assumptions of the larger model, we should use residual diagnostic plots to assess whether those assumptions appear reasonable:
par(mfrow = c(1,2))
plot(larger_model, which = c(1,2))
In this plot we see some indication of increasing spread in the residuals for larger fitted values (more variability on the right side of the Residuals vs. Fitted plot), as well as some moderate skew in the residuals (departure from the 45-degree line in the upper right of the QQ-plot).
These violations might lead us to question a borderline \(p\)-value, but probably aren’t enough to invalidate our overall conclusion. Nevertheless, we’ll try to find a model that better aligns with the assumptions of linear regression as we progress through the lab.
Question #1: In Lab
9 we saw how to fit regression models using a quadratic
effect. In this question you will consider linear vs. quadratic
effects of the variable yrs.service.
yrs.service is nested within the model using a quadratic
effect. Which model parameters (coefficients) do they share? Which are
only present in the larger model?salary ~ sex + yrs.service and
salary ~ sex + yrs.service + I(yrs.service^2). Provide a
brief conclusion using the \(p\)-value
of the test.rank is an important
predictor of salary. For this question, use an \(F\)-test to compare the models:
salary ~ sex + rank + yrs.service and
salary ~ sex + rank + yrs.service + I(yrs.service^2).
Provide a brief conclusion using the \(p\)-value of the test.yrs.service is
warranted in a model that already includes rank by comparing the models:
salary ~ sex + rank and
salary ~ sex + rank + yrs.service + I(yrs.service^2) using
an F-test. What set of coefficients (\(\beta\) parameters) are being tested? What
do you conclude from the test?yrs.service doesn’t seem to
improve the model fit after adjusting for the effects of
rank.\(~\)
Unlike the \(F\)-test, which formally tests whether a predictor improves model fit, adjusted \(R^2\) provides a descriptive measure of model quality that penalizes unnecessary complexity.
Recall from our lecture slides that adjusted \(R^2\) is defined: \[R^2_{\text{adj}} = 1 - \frac{(1 - R^2)(n - 1)}{n - k - 1}\]
The purpose of adjusted \(R^2\) is to provide a measure of model fit that doesn’t always improve when model complexity is increased; however, the downside is that the metric can no longer be interpreted as the proportion of variation in the outcome variable that is explained by the model.
Question #2: For this question you will consider the
baseline model: salary ~ sex + rank
summary() function to
find the \(R^2\) and adjusted \(R^2\) of the baseline model.discipline to the baseline
model and find the new \(R^2\) and
adjusted \(R^2\). Did this additional
predictor seem to improve the model fit?summary() function in the previous part on the model
coefficient of the dummy variable disciplineB.yrs.since.phd to the
baseline model and find the new \(R^2\)
and adjusted \(R^2\). Did this
additional predictor seem to improve the model fit? Note: do
not include discipline for this question.summary() function in the previous part on the model
coefficient of the variable yrs.since.phd.\(~\)
In the first section of this lab we saw a QQ-plot showing that the right-tail of the residual distribution is longer than would be expected in a Normal distribution, calling into question whether the data align with the assumptions of the model. Log-transformations are commonly used in this situation because they often reduce right-skewness and lessen the influence of unusually large observations.
As we’ve seen previously, the log-transformation can be done directly
in the model formula that we give the lm() function:
log_model = lm(log(salary) ~ sex + rank, data = fac_sal)
summary(log_model)
##
## Call:
## lm(formula = log(salary) ~ sex + rank, data = fac_sal)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.76503 -0.13371 0.00085 0.12828 0.62276
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 11.40054 0.03668 310.803 < 2e-16 ***
## sexMale 0.04520 0.03281 1.378 0.169
## rankAsstProf -0.14396 0.03365 -4.279 2.37e-05 ***
## rankProf 0.28403 0.02696 10.536 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.1925 on 393 degrees of freedom
## Multiple R-squared: 0.4568, Adjusted R-squared: 0.4526
## F-statistic: 110.1 on 3 and 393 DF, p-value: < 2.2e-16
Now let’s reassess our model’s assumptions after the log-transformation:
par(mfrow = c(1,2))
plot(log_model, which = c(1,2))
We still see some unequal spread of errors in the residuals vs. fitted plot, but the QQ-plot shows the residuals almost perfectly follow a normal distribution. Overall, these diagnostic plots suggest that the log-transformed model better satisfies the assumptions required for reliable inference.
Question #3:
log(salary) ~ sex + rank + discipline, which is arguably
the best model based upon what we’ve seen so far throughout this lab.
Print a summary() of this model and report its \(R^2\) and adjusted \(R^2\). How do these compare with the values
you had seen in Question #2?sexMale in the model from Part A. Does there appear to be
convincing statistical evidence of a difference in male versus female
salaries after adjusting for differences in rank and discipline?disciplineB. That is, describe how
the expected salary in discipline “B” differs from the expected salary
in discipline “A” after adjusting for differences in rank and sex.plot() function to
create residual diagnostic plots for the model from Part A. Briefly
describe how you feel about the constant variance and normality of
residuals assumptions.