Story points | 13 |
Tags | linear-regression statistics data-analysis |
Hard Prerequisites | |
IMPORTANT: Please review these prerequisites, they include important information that will help you with this content. | |
|
We will predict employee salaries from different employee characteristics (or features).
We are going to use a simple supervised learning technique: linear regression. We want to build a simple model to determine how well Years Worked predicts an employee’s salary.
Import the data salary.csv to a Jupyter Notebook. A description of the variables is given in [Salary Metadata](Salary metadata.csv). You will need the packages matplotlib
, pandas
and statsmodels
.
Split your data into a training and test set. Leave the test set for now. Examine the training data for missing and extreme values. Create different histograms to show the distribution of each variable in your dataset. Create a scatterplot showing the relationship between Years Worked and Salary. Are the data appropriate for linear regression? Is there anything that needs to be transformed or edited first?
Using the statsmodels
package and the training data, run a simple linear regression for Salary with one predictor variable: Years Worked.
What does the unstandardised coefficient (B or ‘coef’ in statsmodels
) tell you about the relationship between Years Worked and Salary?
What do the 95% confidence intervals [0.025, 0.975] mean?
Calculate the expected salary for someone with 12 years’ work experience.
Calculate the expected salary for someone with 80 years’ work experience. Are there any problems with this prediction? If so, what are they?
Now fit your model to your test set. DO NOT BUILD A NEW MODEL ON THE TEST SET! Simply use your existing, model, to predict salaries in the test set.
How does your model compare when running it on the test set - what is the difference in the Root Mean Square Error (RMSE) between the training and test sets? Is there any evidence of overfitting?
We have only looked at the number of years an employee has worked. What other employee characteristics might influence their salary?
Data is made up and inspired by Cohen, Cohen, West & Aiken. Applied Multiple Regression/Correlation Analysis for the Behavioral Sciences, 3rd Edition.
Make sure the amount of variance explained and its statistical significance is correctly interpreted. Simply computing these values is not sufficient, the student needs to demonstrate understanding.
Note that statsmodel
suppresses the constant term unless it’s added or (you use the formula notation). If the model is built without a constant term, this should be justified. If a constant term is added, it should also be explained why this is necessary.
When fitting the model to the test data, make sure that a new model isn’t being built. There should only be one model in the notebook.
A common mistake is misinterpreting the condition for overfitting using the RMSE criterion. Pay attention to which is higher/lower and weather this has been interrupted correctly.