import pandas as pd
df = pd.read_excel('uschange.xlsx' )
df.Consumption.plot(legend=True )
df.Income.plot(legend=True )
from statsmodels.formula.api import ols
ols('Consumption ~ Income' , df).fit().summary()
OLS Regression Results
Consumption 0.159
OLS 0.154
Least Squares 34.98
Sun, 28 May 2023 1.58e-08
11:10:33 -169.62
187 343.2
185 349.7
1
nonrobust
0.5451 0.056 9.789 0.000 0.435 0.655
0.2806 0.047 5.915 0.000 0.187 0.374
16.528 1.696
0.000 29.145
-0.454 4.69e-07
4.707 2.08
Notes: [1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
m = ols('Consumption ~ Income + Production + Unemployment + Savings' , df).fit()
m.summary()
OLS Regression Results
Consumption 0.754
OLS 0.749
Least Squares 139.5
Sun, 28 May 2023 2.62e-54
11:10:34 -54.692
187 119.4
182 135.5
4
nonrobust
0.2673 0.037 7.184 0.000 0.194 0.341
0.7145 0.042 16.934 0.000 0.631 0.798
0.0459 0.026 1.773 0.078 -0.005 0.097
-0.2048 0.106 -1.941 0.054 -0.413 0.003
-0.0453 0.003 -16.287 0.000 -0.051 -0.040
18.566 2.169
0.000 28.796
0.564 5.58e-07
4.556 61.9
Notes: [1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
y_pred = m.predict(df)
df.Consumption.plot(legend=True )
y_pred.plot(legend=True , label='prediction' )
import matplotlib.pyplot as plt
import numpy as np
plt.plot(df.Consumption, y_pred, '.' )
x = np.array(plt.gca().get_xlim())
plt.plot(x, x, '--' )
[<matplotlib.lines.Line2D at 0x158d851a290>]
0 0.168638
1 -0.184591
2 -0.051472
3 -0.070916
4 0.267760
...
182 -0.109993
183 -0.037906
184 -0.113017
185 0.128985
186 -0.046047
Length: 187, dtype: float64
0 0.168638
1 -0.184591
2 -0.051472
3 -0.070916
4 0.267760
...
182 -0.109993
183 -0.037906
184 -0.113017
185 0.128985
186 -0.046047
Length: 187, dtype: float64
from statsmodels.graphics.tsaplots import plot_acf
plot_acf(m.resid);
import pingouin as pg
pg.corr(df.Income, m.resid)
187
-1.283695e-15
[-0.14, 0.14]
1.0
0.092
0.049914
187
3.885781e-16
[-0.14, 0.14]
1.0
0.092
0.049914
n = len (df)
df['t' ] = np.arange(n)
ols('Consumption ~ t' , df).fit().summary()
OLS Regression Results
Consumption 0.025
OLS 0.020
Least Squares 4.726
Sun, 28 May 2023 0.0310
11:12:40 -183.46
187 370.9
185 377.4
1
nonrobust
0.9242 0.095 9.777 0.000 0.738 1.111
-0.0019 0.001 -2.174 0.031 -0.004 -0.000
46.181 1.333
0.000 115.917
-1.058 6.74e-26
6.225 214.
Notes: [1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
r = (n + 1 ) // 4
df['season' ] = np.tile([1 , 2 , 3 , 4 ], r)[:187 ]
ols('Consumption ~ C(season)' , df).fit().summary()
OLS Regression Results
Consumption 0.009
OLS -0.007
Least Squares 0.5513
Sun, 28 May 2023 0.648
11:13:05 -184.97
187 377.9
183 390.9
3
nonrobust
0.7356 0.096 7.667 0.000 0.546 0.925
-0.0296 0.136 -0.218 0.828 -0.297 0.238
0.1143 0.136 0.842 0.401 -0.153 0.382
-0.0424 0.136 -0.311 0.756 -0.312 0.227
29.475 1.283
0.000 63.812
-0.718 1.39e-14
5.476 4.78
Notes: [1] Standard Errors assume that the covariance matrix of the errors is correctly specified.