【发布时间】:2017-08-28 03:25:16
【问题描述】:
我正在尝试使用随机森林进行时间序列分析。 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,我尝试了另一个数据集,我得到了相同的结果。谁能告诉我我在这里做错了什么?
【问题讨论】:
-
什么可以预测销售?目前我只看到日期和两个因素(产品和城市),每个因素都有一个级别。从一些特征工程开始。例如,尝试从 date 中提取星期几,并将其作为 7 级因子。
-
在不知道
slice的来源的情况下,training和testing数据集似乎仅限于 2 个值。可能缺少逗号来选择第一列和第四列training=training[, c(1,4)]。 -
也试试
testing<-slice(SubSales, (train_len+1) :test_len)看看括号的效果试试1+2:5和你的预期结果比较。没有括号,这两个切片本质上是重叠的,从而破坏了分离的目的。强烈推荐caret包中的createDataPartition函数用于此类任务 -
@nya 不,它们不限于选择第一列和第四列的 2 个数据值。
-
@hNu 我的训练和测试集已经创建成功,没有任何问题。我想知道为什么我在使用预测函数时会得到固定值
标签: r random-forest forecasting predict