简体   繁体   English

使用 Holt 的线性模型在 R 中进行预测

[英]Forecasting in R using Holt's Linear model

I am trying to forecast data with a downward trend.我正在尝试预测呈下降趋势的数据。 I understand that Holt's linear model might be the better way to do it, but am unsure how I can implement it in R.我知道 Holt 的线性模型可能是更好的方法,但我不确定如何在 R 中实现它。

The data is as follows:数据如下:

   day  saleRep
1    1 1001.104
2   11 1000.944
3   21 1000.734
4   31 1000.642
5   41 1000.517
6   51 1000.468
7   61 1000.425
8   71 1000.377
9   81 1000.286
10  91 1000.306
11 101 1000.285
12 111 1000.170

Plotting it gives:绘制它给出: 在此处输入图片说明

I am trying to achieve a few things:我正在努力实现以下目标:

  1. Conduct a train test split to create a forecast model that I can evaluate on the test set进行列车测试拆分以创建我可以在测试集上评估的预测模型
  2. Using the model, obtain the predicted sale values for the 250th and 500th day.使用该模型,获取第 250天和第 500天的预测销售额。

How can I implement it in R?我如何在 R 中实现它?

Use this to reproduce the code:使用它来重现代码:

day <- seq(1,111, by = 10)
saleRep <- c(1001.104, 1000.944, 1000.734, 1000.642, 1000.517, 1000.468, 1000.425, 1000.377, 1000.286, 1000.306, 1000.285, 1000.170)
df <- data.frame(day, saleRep)

Thank you.谢谢你。

I will use the forecast package and go step-by-step.我将使用预测包并逐步进行。 Load the forecast package and generate an example daily time-series data加载预测包并生成示例每日时间序列数据

require(forecast)

The toy data玩具数据

x <- c(120:1 + rnorm(120, 12, 3)) 

Convert data to a ts object将数据转换为 ts 对象

x <- ts(x, frequency = 7, #daily data with no yearly seasonalty
          start = 1)
  
  autoplot(x)

Split train and test拆分训练和测试

train <- window(x, end = c(17,1))
  test <- window(x, start = c(17,2))

fit and forecast an hw model拟合和预测硬件模型

fc <- forecast::hw(x, h = 7) # h = 7 the lenght of test set
  

Plot the forecast绘制预测

  autoplot(fc)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM