Residual = observed − fitted. Plot resid vs fitted; a flat band is the hope.
Goal
Print residuals and show the scatter.
import pandas as pd
import statsmodels.api as sm
df = pd.DataFrame({'units': [12, 9, 3, 7, 11], 'shillings': [126.0, 94.5, 40.0, 73.5, 115.5]})
fit = sm.OLS(df['shillings'], sm.add_constant(df['units'])).fit()
print(fit.resid.round(2).tolist())
plt.scatter(fit.fittedvalues, fit.resid)
plt.axhline(0, color='gray')
plt.show()
print('ok')import pandas as pd
import statsmodels.api as sm
fit = sm.OLS([1, 2, 2, 4], sm.add_constant([1, 2, 3, 4])).fit()
print(round(fit.mse_resid, 4))