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

R Prophet报错:数据框需含'ds'和'y'列的原因及解决方法

Prophet建模报错:Dataframe must have columns 'ds' and 'y'的原因及解决方法

问题场景

用户生成了包含分组的时间序列数据框,尝试对单个分组使用Prophet建模时触发报错:

数据生成代码

library(prophet)
Date  <- rep(seq(as.Date("2010-01-01"), as.Date("2014-01-01"), "years"), 3)
Group <- rep(LETTERS[1:3], each = 5)
Value <- runif(15, 0, 100)
Expln <- runif(15, 0, 100)

df <- data.frame(Date, Group, Value, Expln)

建模尝试及报错

dfx = df[df$Group == "A",]
dfx = dfx[c(1, 3, 4)]
dfx
#         Date     Value    Expln
# 1 2010-01-01 93.019346 39.44623
# 2 2011-01-01  3.389022 38.37196
# 3 2012-01-01  3.719541 93.92216
# 4 2013-01-01 11.267414 64.71667
# 5 2014-01-01 84.978091 18.80874

m = prophet()
m = add_regressor(m, 'Expln')
m = fit.prophet(m, dfx)

报错信息:

Error in fit.prophet(m, dfx) : 
  Dataframe must have columns 'ds' and 'y' with the dates and values respectively.

报错原因

Prophet有强制的列名规范:必须用ds存储日期时间列,用y存储要预测的目标变量列。当前dfx的日期列名为Date,目标变量列名为Value,不符合Prophet的要求,因此触发报错。

解决方法

将数据框的列名修改为Prophet要求的ds和y即可,同时保留额外的回归变量列Expln:

修改列名的代码示例

# 重命名列名
names(dfx) <- c("ds", "y", "Expln")

# 重新建模
m = prophet()
m = add_regressor(m, 'Expln')
m = fit.prophet(m, dfx)

也可以在筛选数据时直接指定新列名:

dfx <- df[df$Group == "A", c("Date", "Value", "Expln")]
colnames(dfx) <- c("ds", "y", "Expln")

修改后Prophet就能识别正确的输入列,正常完成模型拟合。


内容的提问来源于stack exchange,提问作者Saïd Maanan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 08:45:37