Problem Set 7
Transformations and Multiple Regression
The files mlb_payrolls_train.csv
and mlb_payrolls_test.csv
contain observations for MLB teams and seasons with complete payroll and
winning percentages for the current and previous seasons. The payroll
source does not cover every team and season. The training file contains
1999–2018, and the testing file contains 2019 and 2021–2023. The
shortened 2020 season is excluded as an outcome, but its winning
percentages provide previous_wp for the 2021 rows.
The variables are:
team: team nameseason: season of the outcome and payrollwinning_percentage: wins divided by games playedrelative_payroll: team payroll divided by the MLB average in that seasonprevious_wp: the team’s winning percentage in the previous season
Relative payroll makes payroll comparable across seasons with different spending levels across MLB. Winning percentage from the previous season adds a measure of recent team performance that current payroll does not contain.
Fit all six candidate models with mlb_train. Load
mlb_test only after all six models are fixed. Select the
final model using testing RMSE, without refitting after viewing the
scores.
Candidate Transformations
- Load
mlb_payrolls_train.csvasmlb_train. Report the ranges ofseasonandrelative_payroll, then plotrelative_payrollagainstwinning_percentage.
Fit three models of
winning_percentageusingrelative_payroll. Save them with the names below.lin_payroll_model:relative_payrolllog_payroll_model:log(relative_payroll)quad_payroll_model:relative_payrollandrelative_payroll^2
Use
I()around the squared term.
lin_payroll_model <- lm(winning_percentage ~ relative_payroll, data = mlb_train)
log_payroll_model <- lm(winning_percentage ~ log(relative_payroll), data = mlb_train)
quad_payroll_model <- lm(winning_percentage ~ ..., data = mlb_train)Create
payroll_grid, a tibble containing 150 evenly spacedrelative_payrollvalues from the observed minimum to the observed maximum. Use each model to add a column of fitted winning percentages. Reshape the three columns of fitted values into a long table calledpayroll_grid_long.Plot the observed training data and add all three curves from
payroll_grid_long. Mapmodelto color so that each curve has a different color and the plot includes a legend.
Training Fit
Add fitted values from the three models to
mlb_train, using the same fitted value column names as inpayroll_grid.Define
rmse(), a function withpredictedandactualarguments that returns RMSE. Use it to calculate the training RMSE for all three models and put the results in a long table calledpayroll_train_scores.Run the supplied code to add a column of random values, include it in the regression with linear payroll, and add the fitted values as
lin_payroll_random. Usermse()andreframe()to createrandom_training_scores, a long table comparing the training RMSE oflin_payrollandlin_payroll_random.
set.seed(2026)
mlb_train <- mlb_train %>%
mutate(random_value = rnorm(n()))
lin_payroll_random_model <- lm(
winning_percentage ~ relative_payroll + random_value,
data = mlb_train
)
mlb_train <- mlb_train %>%
mutate(lin_payroll_random = predict(lin_payroll_random_model))random_value contains no baseball information. Its model
still has slightly lower training RMSE because lm() has
another coefficient to fit. A lower training RMSE alone does not
identify the better model. The random predictor model is not a candidate
for testing.
Adding Previous Season Winning Percentage
Calculate the residuals from
lin_payroll_modelas observed winning percentage minuslin_payroll. Plot the residuals againstprevious_wp, add a horizontal line at zero, and add a fitted straight line withgeom_smooth(). The fitted line should slope upward.Fit three multiple regression models by adding the untransformed
previous_wppredictor to each payroll specification. Save them aslin_payroll_prev_wp_model,log_payroll_prev_wp_model, andquad_payroll_prev_wp_model. Add their fitted values tomlb_trainaslin_payroll_prev_wp,log_payroll_prev_wp, andquad_payroll_prev_wp.Display the coefficients of
log_payroll_prev_wp_model. Calculate the fitted difference in current winning percentage associated with a 0.100 increase inprevious_wp, holding relative payroll constant.Calculate the training RMSE for the three multiple regression models. Combine these results with
payroll_train_scoresin a table calledtraining_scores. Do not select a model or load the testing data.
Testing on Later Seasons
The later seasons measure whether relationships fitted through 2018 carry forward in time.
Load
mlb_payrolls_test.csvasmlb_test. Do not refit any model. Add predictions from all six fitted models.Calculate testing RMSE for all six models and combine those scores with
training_scores. Make a bar chart with the training and testing bars next to each other for each model, and label every bar with its RMSE. Createbest_payroll_modelcontaining the model using only payroll with the lowest testing RMSE andbest_overall_modelcontaining the model with the lowest testing RMSE overall.