How to get constant term in AR Model with statsmodels and Python?
Asked Answered
C

1

3

I'm trying to model my time series data using the AR model.

enter image description here

This is the code that I'm using.

# Compute AR-model (data is a python list of number)

model = AR(data)
result = model.fit()

plt.plot(data, 'b-', label='data')
plt.plot(range(result.k_ar, len(data)), result.fittedvalues, 'r-')
plt.show()

I've successfully get the p value using result.k_ar, parameter with result.params, epsilon term with result.sigma2. The problem is that I can't find a way to get the c (constant) term. Here is the code I write to compare the result.

# Plot

fit = []
for t in range(result.k_ar, len(data)):
    value = 0
    for i in range(1, result.k_ar+1):
        value += result.params[i-1] * data[t - i]
    fit.append(value)

plt.plot(data, 'b-', label='data')
plt.plot(range(result.k_ar, len(data)), fit, 'r-', label='fit')
plt.plot(range(result.k_ar, len(data)), result.fittedvalues, 'r-')
plt.show()

My result and the result from result.fittedvalues confirm my evident that there is some constant term added to the model. Thanks.

enter image description here

Cusco answered 11/6, 2014 at 20:57 Comment(0)
M
5

The constant is the zero-th element in params. E.g., params[0].

Your code should be

fit = []
for t in range(result.k_ar, len(data)):
    value = result.params[0]
    for i in range(2, result.k_ar + 2):
        value += result.params[i - 1] * data[t - i + 1]
    fit.append(value)

Or even easier, since we've made the lag matrix for you (this is what fittedvalues does)

np.dot(result.model.X, result.params)

As an aside, note that for AR this is actually the constant and not the mean. The mean is reported by the ARMA model, which is a bit more full-featured than the plain AR model. (It has a summary method that reports the constant. AR should too but doesn't.) The connection is

constant = mean(1 - arparams.sum())
Maxon answered 11/6, 2014 at 22:52 Comment(3)
According to the equation should I add the epsilon to the result (fit.append(value + result.sigma2)?Cusco
No, but you can use sigma2 to get estimates for forecast uncertainty. There's a PR to put this into AR but for now it's only available in ARMA/ARIMA.Maxon
I have tried using ARMA but still got some problems. Could you please help me looking at this question please? https://mcmap.net/q/1375400/-correct-way-to-use-armaresult-predict-function/1241606Cusco

© 2022 - 2024 — McMap. All rights reserved.