我试图预测一天剩下的订单数量。我有一个从时间序列开始到结束都处于分钟级别的数据框。
我正在使用每日,每周和每年的季节性时段。
我想弄清楚如何在较低的预测间隔内不出现负数的订单。
我尝试将lambda设置为0,但这会引发错误:
stl(ts(deseas,frequency = msts [i]),s.window = s.window [i], :外部函数调用中的NA / NaN / Inf(arg 1)
这是我写的脚本:
# Load packages
library(RMySQL)
library(dplyr)
library(lubridate)
library(forecast)
# Load the SQL Driver
mydb = dbConnect(MySQL(max.con = 16, fetch.default.rec = 500),
user=,
password=,
dbname=,
host=)
# Find the length of the table
recordCountQuery = dbSendQuery(mydb, "SELECT count(*) FROM orders")
recordCount = fetch(recordCountQuery, n=-1)[1,1]
# Get the last 1M records
rs = dbSendQuery(mydb, paste0("SELECT orders_id, date_purchased, orders_status
FROM orders
LIMIT 1000000
OFFSET ", recordCount-1000000))
data = fetch(rs, n=-1) %>%
mutate(date_purchased = ymd_hms(date_purchased)) %>%
filter(orders_status != 2099,
orders_status != 2105)
# Close connections
lapply(dbListConnections(dbDriver(drv = "MySQL")), dbDisconnect)
# Make minute level aggrigation for forecast
ordersByMinute <- data %>%
select(orders_id, date_purchased) %>%
mutate(time = round_date(date_purchased, unit = "minute")) %>%
group_by(time) %>%
summarize(orderCount = n()) %>%
ungroup() %>%
slice(1:n()-1) #drop last row
# Find the min and max times
timeMin <- min(ordersByMinute$time)
timeMax <- max(ordersByMinute$time)
# Create a sequence of times from min to max by minute
minuteFrame <- as.data.frame(list(time = seq(timeMin, timeMax, by="1 min"))) %>%
merge(ordersByMinute, all = TRUE)
# Set times with no orders to 0 instead of NA
minuteFrame$orderCount[which(is.na(minuteFrame$orderCount))] <- 0
# Create multiple time series
minuteFrame <- minuteFrame %>%
mutate(mts = msts(orderCount, seasonal.periods = c(60*24, 60*24*7, 60*24*365.25)))
# Calculate how many minutes are left in the day
minutesLeftToday <- 1440 - hour(timeMax)*60 - minute(timeMax)
# Make a forecast for the remaining minutes in the day
fcSTL <- stlf(minuteFrame$mts, h = minutesLeftToday, etsmodel = "ANN")
# Number of orders so far today
ordersSoFarToday <- data %>%
nrow()
# Mean forecast of additional orders expected
meanFcst <- round(sum(fcSTL$mean),0)
lo80 <- round(sum(fcSTL$lower[,1]),0)
lo95 <- round(sum(fcSTL$lower[,2]),0)
hi80 <- round(sum(fcSTL$upper[,1]),0)
hi95 <- round(sum(fcSTL$upper[,2]),0)
如何使预测间隔不为负?今天不可能有比我们已经收到的订单少的订单。