gpt4 book ai didi

r - 在 R 中使用随机森林进行时间序列预测

转载 作者:行者123 更新时间:2023-12-02 02:32:57 26 4
gpt4 key购买 nike

我正在尝试使用随机森林进行时间序列分析。 PFB我的代码

Subsales<-read.csv('Sales.csv')
head(Subsales)

示例数据:

Date               SKU                            City   Sales
<date> <chr> <chr> <dbl>
1 2014-08-11 Vaseline Petroleum Jelly Pure 60 ml Jeddah1 378
2 2014-08-18 Vaseline Petroleum Jelly Pure 60 ml Jeddah1 348
3 2014-08-25 Vaseline Petroleum Jelly Pure 60 ml Jeddah1 314
4 2014-09-01 Vaseline Petroleum Jelly Pure 60 ml Jeddah1 324
5 2014-09-08 Vaseline Petroleum Jelly Pure 60 ml Jeddah1 352
6 2014-09-15 Vaseline Petroleum Jelly Pure 60 ml Jeddah1 453


####Length of training & testing set Splitting it 80-20####

train_len=round(nrow(SubSales)*0.8)
test_len=nrow(SubSales)



######Splitting dataset into training and testing#####

#### Training Set
training<-slice(SubSales,1:train_len)
#### Testing Set
testing<-slice(SubSales,train_len+1:test_len)

training=training[c(1,4)]
testing=testing[c(1,4)]

library(randomForest)
set.seed(1234)
regressor = randomForest(formula=Sales~.,
data=training,
ntree=100)

y_pred = predict(regressor,newdata = testing)

当我在测试数据集上使用预测函数时,我得到了一个稳定的结果。所有预测值都是 369,我尝试过另一个数据集,但得到了相同的结果。谁能告诉我我在这里做错了什么?

最佳答案

让我尝试重新表述您的问题,以确保我准确理解您想要做什么。

您有某种产品每天的销售额,并且您希望根据 future 日期的函数来预测销售额。您没有任何预测变量,例如客户数量、广告支出或其他任何内容。您的输入数据如下所示:

Date        Sales
2014-08-11 378
2014-08-18 348
2014-08-25 314
2014-09-01 324
2014-09-08 352
2014-09-15 453
...

我认为您的 RandomForest 的行为符合预期。随机森林是一种监督机器学习算法,尝试在给定输入变量 x(预测变量)的情况下预测 y(响应,此处:销售)。在这里,您提供的唯一 x 是日期。然而,每个日期对于随机森林来说都是全新的,因此算法只能猜测您的产品当天的销售额是平均的。

您有两个选择:

选项 1) 坚持仅使用日期作为预测变量的方法。您将需要一种不同的方法,也许是自回归方法,例如 ARIMA。这种方法试图检测数据的趋势。销售额是或多或少保持不变、增长还是下降?有周趋势、月趋势、年度趋势吗?可以找到一个入门示例 here

选项 2) 使用数据收集和特征工程来创建特征,帮助您的 RandomForest 预测新日期的值。例如,尝试获取有关任意一天有多少顾客来到商店的数据,或者提取一周中的某一天(星期一、星期二……)并将其保留为单独的变量。 R 软件包 lubridate 将帮助您做到这一点。下面是一个简短的例子:

library(lubridate)
Subsales <- mutate(Subsales, Weekday = wday(Date, label = TRUE))

希望这有帮助!

关于r - 在 R 中使用随机森林进行时间序列预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43181629/

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