Skip to content Skip to sidebar Skip to footer

How To Get Constant Term In Ar Model With Statsmodels And Python?

I'm trying to model my time series data using the AR model. This is the code that I'm using. # Compute AR-model (data is a python list of number) model = AR(data) result = model

Solution 1:

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

Your code should be

fit = []
for t inrange(result.k_ar, len(data)):
    value = result.params[0]
    for i inrange(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())

Post a Comment for "How To Get Constant Term In Ar Model With Statsmodels And Python?"