gpt4 book ai didi

R Prophet add_regressor 给出奇怪的结果

转载 作者:行者123 更新时间:2023-12-04 04:21:42 26 4
gpt4 key购买 nike

我正在尝试(第一次)使用 add_regressor 函数向 prophet 添加一个外部变量,但我得到的结果看起来很奇怪。我使用的数据集可在 kaggle(众所周知的洗发水销售)上免费获得 here .我正在尝试使用 R 的 quantmod 包作为我的外部变量,为 SPY 股票指数使用免费可用的数据。

下面是我如何开始代码:

library(prophet)
library(quantmod)
library(dplyr)


df <- read.csv("~/shampoo.csv")


#now get the min and max dates in the column
min_date <- min(df$Date, na.rm = TRUE)
max_date <- max(df$Date, na.rm = TRUE)


#download the SPY stock data
getSymbols("SPY", from = min_date, to = max_date)

#SPY closes stored into a df and massage a bit
Close <- data.frame(Cl(SPY))
Close <- cbind(ds = rownames(Close), Close)
rownames(Close) <- NULL
Close_no_rename <- Close
colnames(Close)[colnames(Close) == 'SPY.Close'] <- 'y'
colnames(Close_no_rename)[colnames(Close_no_rename) == 'SPY.Close'] <- 'SPY_CLOSE'


#now put this into prophet and make a forecast for the forecast_period for SPY
stock_model <- prophet(Close)


#make a forecast dataframe
future_stocks <- make_future_dataframe(stock_model, periods = 30, freq = "month", include_history = FALSE)

#the below df will have predicted stock prices of the SPY. want to extract the future y values as point forecast along with dates
forecast <- predict(stock_model, future_stocks) %>% select(ds, yhat)
colnames(forecast)[colnames(forecast) == 'yhat'] <- 'SPY_CLOSE'


#rename the columns of the actual df
colnames(df)[colnames(df) == 'Date'] <- 'ds'
colnames(df)[colnames(df) == 'Value'] <- 'y'

#now want to merge the Close df y historic values onto the training df, merge by date ds column
df_historic_with_SPY_close <- merge(df, Close_no_rename, by = "ds")


#now actually forecast using prophet
model <- prophet()

#add the SPY regressor
model <- add_regressor(model, 'SPY_CLOSE', prior.scale = 0.0000001, standardize = FALSE)
model <- fit.prophet(model, df_historic_with_SPY_close)


forecast_final <- predict(model, forecast)

plot(model, forecast_final)

这不会引发任何错误,但预测图看起来......错误。看起来好像秤不对或什么的。我尝试摆弄先前的设置并标准化设置,但没有成功。谢谢你的帮助! enter image description here

这里是用作主要变量的洗发水数据集:

Date    Value
2017-01-01 266
2017-02-01 145.9
2017-03-01 183.1
2017-04-01 119.3
2017-05-01 180.3
2017-06-01 168.5
2017-07-01 231.8
2017-08-01 224.5
2017-09-01 192.8
2017-10-01 122.9
2017-11-01 336.5
2017-12-01 185.9
2018-01-01 194.3
2018-02-01 149.5
2018-03-01 210.1
2018-04-01 273.3
2018-05-01 191.4
2018-06-01 287
2018-07-01 226
2018-08-01 303.6
2018-09-01 289.9
2018-10-01 421.6
2018-11-01 264.5
2018-12-01 342
2019-01-01 339.7
2019-02-01 440.4
2019-03-01 315.9
2019-04-01 439.3
2019-05-01 401.3
2019-06-01 437.4
2019-07-01 575.5
2019-08-01 407.6
2019-09-01 682
2019-10-01 475.3
2019-11-01 581.3
2019-12-01 646.9

最佳答案

我想我已经修复了一些问题,但我唯一不同的是将日期从因子格式更改为日期格式,并告诉 R 使用 dplyr 中的选择函数。我也在没有加载任何其他包的情况下运行 R。因此,这为何有效仍然是个谜。

当行时,我遇到了这个问题

df_historic_with_SPY_close <- merge(df, Close_no_rename, by = "ds")

没有正常工作。我发现这是因为我已将 df 日期格式化为日期以使它们与 getSymbols 一起使用,但它们与 Close_no_rename 不同。

首先是我使用的数据:

df<-dput(df)
structure(list(ds = structure(c(17167, 17198, 17226, 17257, 17287,
17318, 17348, 17379, 17410, 17440, 17471, 17501, 17532, 17563,
17591, 17622, 17652, 17683, 17713, 17744, 17775, 17805, 17836,
17866, 17897, 17928, 17956, 17987, 18017, 18048, 18078, 18109,
18140, 18170, 18201, 18231), class = "Date"), y = c(266, 145.9,
183.1, 119.3, 180.3, 168.5, 231.8, 224.5, 192.8, 122.9, 336.5,
185.9, 194.3, 149.5, 210.1, 273.3, 191.4, 287, 226, 303.6, 289.9,
421.6, 264.5, 342.3, 339.7, 440.4, 315.9, 439.3, 401.3, 437.4,
575.5, 407.6, 682, 475.3, 581.3, 646.9)), row.names = c(NA, -36L
), class = "data.frame")


library(prophet)
library(quantmod)
library(dplyr)

# can use your df, rather than above
df<-read.csv("~/shampoo.csv")
# either way, should run this
df$Date<-as.Date.factor(df$Date,tryFormats = c("%d-%m-%y"))
str(df) #check

#now get the min and max dates in the column
min_date <- min(df$Date, na.rm = TRUE)
max_date <- max(df$Date, na.rm = TRUE)


#download the SPY stock data
getSymbols("SPY", from = min_date, to = max_date)

#SPY closes stored into a df and massage a bit
Close <- data.frame(Cl(SPY))
Close <- cbind(ds = rownames(Close), Close)
rownames(Close) <- NULL
Close_no_rename <- Close
colnames(Close)[colnames(Close) == 'SPY.Close'] <- 'y'
colnames(Close_no_rename)[colnames(Close_no_rename) == 'SPY.Close'] <- 'SPY_CLOSE'

# make dates in date format

Close_no_rename$ds<-as.Date(Close_no_rename$ds)
str(Close_no_rename)

#now put this into prophet and make a forecast for the forecast_period for SPY
stock_model <- prophet(Close)


#make a forecast dataframe
future_stocks <- make_future_dataframe(stock_model, periods = 30, freq = "month", include_history = FALSE)

#the below df will have predicted stock prices of the SPY. want to extract the future y values as point forecast along with dates
# specify dplyr:::select
forecast <- predict(stock_model, future_stocks) %>% dplyr:::select(ds, yhat)
colnames(forecast)[colnames(forecast) == 'yhat'] <- 'SPY_CLOSE'


#rename the columns of the actual df
colnames(df)[colnames(df) == 'Date'] <- 'ds'
colnames(df)[colnames(df) == 'Value'] <- 'y'

#now want to merge the Close df y historic values onto the training df, merge by date ds column
df_historic_with_SPY_close <- merge(df, Close_no_rename, by = "ds")
df_historic_with_SPY_close

#now actually forecast using prophet
model <- prophet()

#add the SPY regressor
model <- add_regressor(model, 'SPY_CLOSE', prior.scale = 0.0000001, standardize = FALSE)
model <- fit.prophet(model, df_historic_with_SPY_close)


forecast_final <- predict(model, forecast)

plot(model, forecast_final)

结果:

enter image description here

编辑

使用以下数据集,日期更改为 SPY 数据集中接近的日期:

df<-dput(df)
structure(list(ds = structure(c(17169, 17198, 17226, 17259, 17287,
17318, 17350, 17379, 17410, 17442, 17471, 17501, 17534, 17563,
17591, 17624, 17652, 17683, 17715, 17744, 17778, 17805, 17836,
17868, 17898, 17928, 17956, 17987, 18017, 18050, 18078, 18109,
18142, 18170, 18201, 18232), class = "Date"), y = c(266, 145.9,
183.1, 119.3, 180.3, 168.5, 231.8, 224.5, 192.8, 122.9, 336.5,
185.9, 194.3, 149.5, 210.1, 273.3, 191.4, 287, 226, 303.6, 289.9,
421.6, 264.5, 342.3, 339.7, 440.4, 315.9, 439.3, 401.3, 437.4,
575.5, 407.6, 682, 475.3, 581.3, 646.9)), class = "data.frame", row.names = c(NA,
-36L))

我们得到这个,看起来好多了:

enter image description here

编辑 2

问题与缺失数据有关。洗发水数据集中的某些日期不在 SPY 数据集中。下面将在SPY数据集中选取最近日期的数据来克服数据缺失的问题。但是,它生成的图表看起来仍然很奇怪,并且稍微更改日期似乎是问题的原因。

替换行:

df_historic_with_SPY_close <- merge(df, Close_no_rename, by = "ds")

与(归功于 https://stackoverflow.com/a/28073823/7967291 ):

library(data.table)
setDT(Close_no_rename)
setDT(df)

setkey(Close_no_rename, ds)[, dateMatch:=ds]
df_historic_with_SPY_close<-Close_no_rename[df, roll='nearest']
df_historic_with_SPY_close<-setDT(df_historic_with_SPY_close)[,-1]
names(df_historic_with_SPY_close)[names(df_historic_with_SPY_close) == "dateMatch"] <- "ds"
df_historic_with_SPY_close
df_historic_with_SPY_close <- mutate ( df_historic_with_SPY_close, ds = ymd(ds) )
str(df_historic_with_SPY_close)

关于R Prophet add_regressor 给出奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59062540/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com