先知在R中提前一步预测
我已经构建了一个自定义功能,可以使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我至少可以看到一些错误。首先,您缺少功能的关闭括号。其次,您没有指定什么
k
是。此外,该功能还有一些问题。首先,这是错误的:
根据
?Prophet
,您需要一个这也是错误的
使用函数
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:
According to
?prophet
, you need aThis is also wrong
Use function
predict.prophet
to predict using a prophet model. The object returned bypredict.prophet
does not have columnmean
. You might be looking for$yhat
instead.