- Oct 23, 2024
Model Coefficient and Residual Plots
- DevTechie
In this article, we will understand and calculate model coefficient and correlate with the residual plots in earlier articles
What is Model Coefficient?
Let us understand this with the same Ads dataset that we have been using. As we have been trying to predict sales based on ads spend data for TV, radio and newspaper, if we recall, when we fit linear regression model to this dataset our resulting linear regression equation might look like below:
Sales = β0+β1*TV+β2*radio+β3*newspaper
where β0 is the baseline value, i.e. even if there is no ad spend we assume our sales to be this value. β1 represents that sales are expected to increase for each additional unit spend, same goes for radio and newspaper for β2 and β3 respectively.
Let’s look at the Python code to understand further. This will look familiar to what we have already done in previous articles.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
file_path = r'/Users/Downloads/advertising.csv'
df_ad_data = pd.read_csv(file_path)
df_ad_data.head()
# X matrix to represent all my feature
X = df_ad_data[["TV","Radio","Newspaper"]]
X.head()
# Y vector column
y = df_ad_data['Sales']Above we are creating X matrix and Y vector and below we will be using sklearn to do train test split and calculate coefficient.
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101)
model = LinearRegression()
model.fit(X_train, y_train)
model.coef_model.coef_ returns me the values as array([ 0.05493018, 0.1095576 , -0.00619422]), these values are for TV, Radio and Newspaper respectively. As we discussed earlier, these coefficient provide the strength and direction of the relationship between predictors (TV, Radio and Newspaper spend in this case) and outcome (Sales in this case).
If we look, Newspaper coefficient is a small number so it is telling us that spend towards newspaper is not resulting in a sales or there is no real relationship. This was also clear with residual plot from last article.
For TV coefficient 0.05 we can interpret that the spend has a positive impact on sales. Residual plot for TV also looked randomized indicating model is a good fit for predicting sales based on TV spend.
For Radio, coefficient 0.11 suggest a strong relationship between Radio spend data and sales. However, if recall from previous article we know that the residual plot for radio was a straight line and the data was not distributed normally.
When we identify such difference in results for Coefficients vs Residual we can interpret below:
a. If a significant coefficient indicates a relationship, but if residual plots reveal non-linearity, the model’s assumption of linearity is violated, suggesting that the model might not be capturing the true nature of the relationship.
b. Even if a model coefficient is significant, residual plots might show issues like outliers, non-linearity, or heteroscedasticity, indicating that the model may not be the best fit or that transformations or alternative models might be needed.
c. Examining residuals against each feature independently helps identify specific issues related to that feature. For example, if residuals plotted against a particular feature show a non-random pattern, it suggests that the relationship between that feature and the outcome is not well captured by the model, possibly indicating a need for a non-linear transformation or a different model structure.
In conclusion, both model coefficients and residual plots are crucial for a comprehensive understanding and evaluation of a regression model. Coefficients provide information about the relationships between predictors and the outcome, while residual plots diagnose model fit and validate assumptions. Analyzing residual plots independently for each feature can uncover specific issues that coefficients alone may not reveal, helping refine the model and improve its accuracy and validity. With this we have come to the end of our article. See you in the next one.