R中IRF脉冲响应函数无法生成图像:对象缺失与方法不适用问题
VAR模型IRF分析报错解决方法
问题背景
用vars和varEst包对3变量时间序列数据构建了varest类模型Model1,执行IRF绘图时遇到两个错误:
- 运行
plot(varp.irfs, plot.type="multiple")报错:
Error in h(simpleError(msg, call)) : error in evaluating the argument 'x' in selecting a method for function 'plot': object 'varp.irfs' not found
- 运行
plot(irf(Model1,n.ahead=10))报错:
Error in h(simpleError(msg, call)) : error in evaluating the argument 'x' in selecting a method for function 'plot': no applicable method for 'irf' applied to an object of class "varest"
解决步骤
1. 正确生成IRF对象
报错核心是未正确生成IRF结果对象,且存在包函数冲突。对于vars包的varest模型,必须指定包名调用irf()函数生成IRF对象:
# 生成IRF结果,n.ahead指定预测期数,boot可选用于生成置信区间 var_irfs <- vars::irf(Model1, n.ahead = 10, boot = TRUE)
2. 绘制IRF图像
生成var_irfs对象后,直接调用plot()即可,vars包自带针对irf类对象的绘图方法:
# 绘制多面板IRF图 plot(var_irfs, plot.type = "multiple")
额外注意事项
- 用
class(Model1)确认Model1是有效的varest类对象。 - 若需使用
varEst包的IRF功能,查阅该包官方文档确认专属IRF函数的调用方式(该包可能不使用通用irf()函数)。 - 调用函数时尽量指定包名(如
vars::irf()),避免不同包的函数冲突。
内容的提问来源于stack exchange,提问作者Säge Zwiebel
相关产品推荐
相关产品推荐

