You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

adf.test报错“x不是向量或单变量时间序列”的问题排查

问题解决:ADF检验报错“x is not a vector or univariate time series”

错误原因

你创建的inf_ph是多变量时间序列对象(包含日期、通胀率两列),但adf.test()仅支持输入单变量时间序列或向量,因此触发报错。

修正步骤及代码

方法1:创建ts对象时直接提取通胀率列

在生成时间序列时,只选取原数据框的通胀率列(第2列),生成单变量时间序列:

inf <- read.csv("inf_ph.csv")
# 仅提取通胀率列创建单变量ts对象
inf_ph <- ts(inf[,2], frequency=12, start=c(2018,1))
autoplot(inf_ph,
    main = "Monthly Headline Inflation",
    xlab = "Year",
    ylab = "In Percent (%)")
12*(65/100)^(1/4)
# 正常运行ADF检验
adf.test(inf_ph, nlag=10)
#H0: not stationary
#H1: stationary

方法2:调用ADF检验时提取单变量序列

若需保留原多变量ts对象,在adf.test()中指定通胀率列即可:

inf <- read.csv("inf_ph.csv")
inf_ph <- ts(inf,frequency=12,start=c(2018,1))
autoplot(inf_ph[,2],
    main = "Monthly Headline Inflation",
    xlab = "Year",
    ylab = "In Percent (%)")
12*(65/100)^(1/4)
# 提取第2列(通胀率)作为单变量输入
adf.test(inf_ph[,2], nlag=10)
#H0: not stationary
#H1: stationary

额外说明

原代码中绘图时已经正确提取了通胀率列,所以绘图能正常运行,但ADF检验时未做同样操作,导致报错。如果通胀率列读取后为字符型,需先通过as.numeric()转换为数值型。

内容的提问来源于stack exchange,提问作者Kirsten Sigua

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 17:00:09