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

Prophet模型拟合报错求助:变量t不存在

Prophet模型拟合报错解决

报错信息

model <- prophet(train)
Error : Exception: variable does not exist; processing stage=data initialization; variable name=t; base type=double (in 'string', line 75, column 2 to column 14)
Error in if (m$stan.fit$return_code != 0) { : argument is of length zero

问题代码

library(prophet)
library(tidyverse)

# Data preprocessing
# Assuming 'median_value' is your target variable
# Make sure to adjust the column names based on your data
REIT_1997$ZipCode <- as.factor(REIT_1997$ZipCode)

# Extract relevant columns for time series regression
# Assuming columns from '1997-01-31' to '2023-09-30' are your monthly median values
ts_data <- REIT_1997 %>%
  select(ZipCode, starts_with("1997"), starts_with("1998"), starts_with("1999"), starts_with("2000"), starts_with("2001"),
         starts_with("2002"), starts_with("2003"), starts_with("2004"), starts_with("2005"), starts_with("2006"),
         starts_with("2007"), starts_with("2008"), starts_with("2009"), starts_with("2010"), starts_with("2011"),
         starts_with("2012"), starts_with("2013"), starts_with("2014"), starts_with("2015"), starts_with("2016"),
         starts_with("2017"), starts_with("2018"), starts_with("2019"), starts_with("2020"), starts_with("2021"),
         starts_with("2022"), starts_with("2023")) %>%
  pivot_longer(cols = -ZipCode, names_to = "Date", values_to = "MedianValue") 
# mutate(Date= as.Date(Date_, "%Y.%m.%d"))


# Check for NAs in ts_data
if (any(is.na(ts_data))) {
  print("ts_data contains NAs.")
} else {
  print("ts_data does not contain NAs.")
}

# Remove NAs
ts_data <- ts_data %>%
  drop_na()

# Check for NAs in ts_data
if (any(is.na(ts_data))) {
  print("ts_data contains NAs.")
} else {
  print("ts_data does not contain NAs.")
}

# Time series split
set.seed(123)


ts_data$Date <- as.Date(ts_data$Date)

str(ts_data)
head(ts_data)




prophet_data <- ts_data %>%
  select(ds = Date, y = MedianValue)  # Adjust column names based on your data



train <- prophet_data %>% filter(ds<ymd("2020-01-01"))
test <- prophet_data %>% filter(ds>=ymd("2020-01-01"))


# Fitting the Prophet Model
model <- prophet(train)



# Making Future Dataframe
future <- make_future_dataframe(model, periods = 365)  # Adjust the number of periods as needed

# Generating Forecast
forecast <- predict(model, future)

问题原因

  1. 多分组时间序列未拆分:你的数据是按ZipCode分组的长格式,同一个日期ds对应多个不同邮编的y值,但Prophet是单变量时间序列模型,要求每个时间点只能有一个观测值,混合数据导致Stan拟合时无法生成正确的时间索引变量t,触发报错。
  2. 日期转换可能存在问题:你注释掉了指定格式的日期转换代码,直接用as.Date(ts_data$Date),如果原始日期字符串格式不符合默认识别规则,会生成NA值,进一步干扰模型拟合。

解决方案

1. 确保日期转换正确

先检查并修正日期列的转换:

# 查看原始日期格式
head(ts_data$Date)
# 根据实际格式转换,例如如果是"YYYY-MM-DD"格式:
ts_data$Date <- as.Date(ts_data$Date, format = "%Y-%m-%d")
# 检查是否存在NA值
sum(is.na(ts_data$Date))
# 如果有NA,需排查原始数据格式并调整format参数

2. 按ZipCode分组拟合模型

为每个邮编的独立时间序列单独拟合Prophet模型:

library(purrr)

# 定义单个分组的模型拟合函数
fit_single_zip <- function(zip_data) {
  # 整理Prophet要求的列名
  prophet_df <- zip_data %>% select(ds = Date, y = MedianValue)
  # 划分训练集和测试集
  train_df <- prophet_df %>% filter(ds < ymd("2020-01-01"))
  test_df <- prophet_df %>% filter(ds >= ymd("2020-01-01"))
  
  # 拟合模型
  model <- prophet(train_df)
  # 生成未来数据框(月度数据,设置freq="month",periods按需调整)
  future_df <- make_future_dataframe(model, periods = 46, freq = "month")
  # 生成预测
  forecast_df <- predict(model, future_df)
  
  # 返回该邮编的测试集和预测结果
  list(zip_code = unique(zip_data$ZipCode), test = test_df, forecast = forecast_df, model = model)
}

# 按ZipCode拆分数据并批量拟合模型
zip_forecast_results <- ts_data %>%
  group_split(ZipCode) %>%
  map(fit_single_zip)

# 为结果命名对应的邮编,方便后续调用
names(zip_forecast_results) <- unique(ts_data$ZipCode)

3. 验证结果

可以查看单个邮编的预测结果:

# 查看第一个邮编的预测
print(zip_forecast_results[[1]]$zip_code)
plot(zip_forecast_results[[1]]$model, zip_forecast_results[[1]]$forecast)

内容的提问来源于stack exchange,提问作者Nader Mehri

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 07:44:56