import pandas as pd
uc = pd.read_excel('uschange.xlsx')
uc.head()
| 1970-01-01 |
0.615986 |
0.972261 |
-2.452700 |
4.810312 |
0.9 |
| 1970-04-01 |
0.460376 |
1.169085 |
-0.551525 |
7.287992 |
0.5 |
| 1970-07-01 |
0.876791 |
1.553271 |
-0.358708 |
7.289013 |
0.5 |
| 1970-10-01 |
-0.274245 |
-0.255272 |
-2.185455 |
0.985230 |
0.7 |
| 1971-01-01 |
1.897371 |
1.987154 |
1.909734 |
3.657771 |
-0.1 |
from statsmodels.formula.api import ols
ols('Consumption ~ Income', uc).fit().summary()
OLS Regression Results
| Consumption | 0.159 |
| OLS | 0.154 |
| Least Squares | 34.98 |
| Sat, 01 Jul 2023 | 1.58e-08 |
| 19:29:57 | -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.
from patsy import dmatrices
y, x = dmatrices('Consumption ~ Income', uc)
from statsmodels.regression.linear_model import OLS
OLS(y, x).fit().summary()
OLS Regression Results
| Consumption | 0.159 |
| OLS | 0.154 |
| Least Squares | 34.98 |
| Sat, 01 Jul 2023 | 1.58e-08 |
| 19:30:13 | -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.
from statsmodels.tsa.api import SARIMAX
m = SARIMAX(y, order=(1, 0, 0)).fit()
m.summary()
SARIMAX Results
| Consumption | 187 |
| SARIMAX(1, 0, 0) | -196.683 |
| Sat, 01 Jul 2023 | 397.367 |
| 19:39:49 | 403.829 |
| 0 | 399.985 |
| - 187 | |
| opg | |
|
| 0.7152 | 0.051 | 14.133 | 0.000 | 0.616 | 0.814 |
| 0.4780 | 0.040 | 11.973 | 0.000 | 0.400 | 0.556 |
| 31.48 | 24.93 |
| 0.00 | 0.00 |
| 0.22 | 0.15 |
| 0.00 | 4.76 |
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-step).
import matplotlib.pyplot as plt
uc.Consumption.plot()
plt.plot(m.predict(start=0, end=186))
[<matplotlib.lines.Line2D at 0x2d376654ca0>]
m = SARIMAX(y, order=(0, 0, 1), trend='c').fit()
m.summary()
SARIMAX Results
| Consumption | 187 |
| SARIMAX(0, 0, 1) | -177.185 |
| Sat, 01 Jul 2023 | 360.371 |
| 19:39:50 | 370.064 |
| 0 | 364.298 |
| - 187 | |
| opg | |
|
| 0.7463 | 0.063 | 11.844 | 0.000 | 0.623 | 0.870 |
| 0.2666 | 0.069 | 3.878 | 0.000 | 0.132 | 0.401 |
| 0.3894 | 0.029 | 13.466 | 0.000 | 0.333 | 0.446 |
| 0.45 | 52.67 |
| 0.50 | 0.00 |
| 0.31 | -0.59 |
| 0.00 | 5.31 |
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-step).
uc.Consumption.plot()
plt.plot(m.predict(start=0, end=186))
[<matplotlib.lines.Line2D at 0x2d3766d3e50>]
m = SARIMAX(y, x, order=(1, 0, 0)).fit()
m.summary()
SARIMAX Results
| y | 187 |
| SARIMAX(1, 0, 0) | -166.064 |
| Sat, 01 Jul 2023 | 340.129 |
| 19:39:51 | 353.053 |
| 0 | 345.366 |
| - 187 | |
| opg | |
|
| 0.6093 | 0.062 | 9.898 | 0.000 | 0.489 | 0.730 |
| 0.1907 | 0.034 | 5.633 | 0.000 | 0.124 | 0.257 |
| 0.2353 | 0.067 | 3.514 | 0.000 | 0.104 | 0.367 |
| 0.3457 | 0.028 | 12.308 | 0.000 | 0.291 | 0.401 |
| 0.72 | 25.86 |
| 0.40 | 0.00 |
| 0.42 | -0.42 |
| 0.00 | 4.62 |
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-step).
uc.Consumption.plot()
plt.plot(m.predict(start=0, end=186, exog=x))
[<matplotlib.lines.Line2D at 0x2d3777a0220>]
<statsmodels.tsa.statespace.sarimax.SARIMAXResultsWrapper at 0x2d373963fd0>