先知在R中提前一步预测

发布于 2025-01-29 03:18:33 字数 1584 浏览 3 评论 0原文

我已经构建了一个自定义功能,可以使用R中的先知进行一步预测,但是我的代码不起作用,不知道该问题是什么。我将滚动窗口尺寸为100,并用60天训练了数据,以预测数据中的下一个1个条目。但是,我的模型无法正常工作,请帮助我。

# loading the data:

s <- read.csv(url('https://ondemand.websol.barchart.com/getHistory.csv?apikey=c3122f072488a29c5279680b9a2cf88e&symbol=zs*1&type=dailyNearest&backAdjust=false&startDate=20100201'))

data <- s[c(3, 7)]

data <- as_tibble(data) %>% 
  mutate(tradingDay = as_date(tradingDay))

Here is my code:

# One-step ahead forecasting with Prophet

Prophet_prediction <- function(k) {
  
  range_validation <- 100
  n_ahead <- 1
  
  train_tbl <- data %>% slice((1 + k):(60 + k))
  valid_tbl <- data %>% slice((60 + 1 + k):(60 + k + range_validation)) 
  test_tbl  <- data %>% slice((60 + k + range_validation + 1):(60 + k + range_validation + n_ahead))
  
  train_df <- bind_rows(train_tbl, valid_tbl) %>% select(1:2)
  test_df <- test_tbl %>% select(1:2)
  
  # Prophet model: 
  my_prophet <- prophet(train_df[,2] %>% ts(start = 1))
  # Use the model for forecasting: 
  predict.prophet <- forecast(my_prophet, h = 1)$yhat %>% as.vector()
  
  actual_predicted_df_test <- test_df %>% 
    mutate(predicted = predict.prophet) 
  
  return(actual_predicted_df_test)

}

lapply(0:50, Prophet_prediction) ->> Prophet_prediction
Prophet_prediction <- do.call("bind_rows", Prophet_prediction)
view(Prophet_prediction)

但这给了我这个错误:

Error in attr(data, "tsp") <- c(start, end, frequency) : 
object is not a matrix

I have built a custom function to do one-step-ahead forecasting using Prophet in R. But my code is not working don't know what is the problem. I have taken the rolling window size of 100 and trained the data with 60 days to forecast the next 1 entry in the data. But, my model is not working as it supposes to, please help me.

# loading the data:

s <- read.csv(url('https://ondemand.websol.barchart.com/getHistory.csv?apikey=c3122f072488a29c5279680b9a2cf88e&symbol=zs*1&type=dailyNearest&backAdjust=false&startDate=20100201'))

data <- s[c(3, 7)]

data <- as_tibble(data) %>% 
  mutate(tradingDay = as_date(tradingDay))

Here is my code:

# One-step ahead forecasting with Prophet

Prophet_prediction <- function(k) {
  
  range_validation <- 100
  n_ahead <- 1
  
  train_tbl <- data %>% slice((1 + k):(60 + k))
  valid_tbl <- data %>% slice((60 + 1 + k):(60 + k + range_validation)) 
  test_tbl  <- data %>% slice((60 + k + range_validation + 1):(60 + k + range_validation + n_ahead))
  
  train_df <- bind_rows(train_tbl, valid_tbl) %>% select(1:2)
  test_df <- test_tbl %>% select(1:2)
  
  # Prophet model: 
  my_prophet <- prophet(train_df[,2] %>% ts(start = 1))
  # Use the model for forecasting: 
  predict.prophet <- forecast(my_prophet, h = 1)$yhat %>% as.vector()
  
  actual_predicted_df_test <- test_df %>% 
    mutate(predicted = predict.prophet) 
  
  return(actual_predicted_df_test)

}

lapply(0:50, Prophet_prediction) ->> Prophet_prediction
Prophet_prediction <- do.call("bind_rows", Prophet_prediction)
view(Prophet_prediction)

But it gives me this error:

Error in attr(data, "tsp") <- c(start, end, frequency) : 
object is not a matrix

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

够钟 2025-02-05 03:18:33

我至少可以看到一些错误。首先,您缺少功能的关闭括号。其次,您没有指定什么k是。此外,该功能还有一些问题。

首先,这是错误的:

my_prophet <- prophet(train_df[,2] %>% ts(start = 1))

根据?Prophet,您需要一个

包含历史记录的数据框。必须具有DS(日期类型)和
是的,时间序列。

这也是错误的

  predicted_prophet <- forecast(my_prophet, h = 1)$mean %>% as.vector()

使用函数precade.prophet用于使用先知模型进行预测。 predive.prophet返回的对象没有列均值。您可能正在寻找$ YHAT

I can see at least a few errors. First, you are missing the closing bracket of your function. Second, you did not specify what k is. Additionally, the function has some issues.

First, this is wrong:

my_prophet <- prophet(train_df[,2] %>% ts(start = 1))

According to ?prophet, you need a

Dataframe containing the history. Must have columns ds (date type) and
y, the time series.

This is also wrong

  predicted_prophet <- forecast(my_prophet, h = 1)$mean %>% as.vector()

Use function predict.prophet to predict using a prophet model. The object returned by predict.prophet does not have column mean. You might be looking for $yhat instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文