如何使用 statsmodels 和 Python 在 AR 模型中获得常数项? [英] How to get constant term in AR Model with statsmodels and Python?

查看:107
本文介绍了如何使用 statsmodels 和 Python 在 AR 模型中获得常数项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 AR 模型为我的时间序列数据建模.

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.fit()

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

我已经使用 result.k_ar 成功获得了 p 值,参数带有 result.params,epsilon 术语带有 result.sigma2.问题是我找不到获得 c(常数)项的方法.这是我编写的用于比较结果的代码.

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()

我的结果和来自 result.fittedvalues 的结果证实了我在模型中添加了一些常数项的证据.谢谢.

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

推荐答案

常量是 params 中的第零个元素.例如,params[0].

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

你的代码应该是

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)

甚至更容易,因为我们已经为您制作了滞后矩阵(这就是 fittvalues 所做的)

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

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

顺便说一句,请注意,对于 AR,这实际上是常数而不是平均值.平均值由 ARMA 模型报告,它比普通 AR 模型功能更全面.(它有一个报告常量的汇总方法.AR 也应该但没有.)连接是

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())

这篇关于如何使用 statsmodels 和 Python 在 AR 模型中获得常数项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆