【问题标题】:Plotting the overlay using an exponential smoothing of a time-series using `lines()`使用“lines()”对时间序列进行指数平滑来绘制叠加层
【发布时间】:2014-04-21 22:16:21
【问题描述】:

/* 我不想在 R 中使用 HoltWinters 函数,我已经看到 R code 用于 holtwinters 函数,但我真的不想实现或复制整个 R 代码。主要是我想写一个代码来得到和holtwinters得到的答案一样的结果*/

到目前为止,我有以下代码,但我不确定应该使用哪个函数来绘制新罕布什尔州从 1912 年到 1971 年的年平均温度测量值,并使用 lines() 对其进行指数平滑处理

require(datasets)
data(nhtemp)
plot.ts(nhtemp)
lines(nhtemp, col="red") # gives the same plot as plot.ts(nhtemp)!!

我不知道如何使用lines() 覆盖nhtemp 的指数平滑 P.S.:我不想尝试任何外部软件包,例如 forecaststats,但我只是在尝试以下一些实验: 我尝试按照下图中提到的说明进行操作,但收到此错误:

> lines(nhtemp$fitted)
#Error in lines(nhtemp$fitted) : 
#  error in evaluating the argument 'x' in selecting a method for function 'lines': #Error in nhtemp$fitted : $ operator is invalid for atomic vectors
> HoltWinters(nhtemp)
#Error in decompose(ts(x[1L:wind], start = start(x), frequency = f), seasonal) : 
#  time series has no or less than 2 periods

【问题讨论】:

  • 这篇文章不是和交叉验证更相关吗?我不知道为什么它被迁移或关闭了?!
  • 这应该转到 CrossValidated 并且您需要重新表述整个问题。正如您尝试过的代码暗示您正在尝试在某些数据集上运行 HoltWinters() 函数并遇到错误,而实际上您要做的是自己编写指数平滑函数

标签: r time-series data-visualization


【解决方案1】:

由于您的数据集不是季节性的 (Frequency = 1),您应该通过指定 gamma = F 来拟合非季节性 Holt-Winters。

另外,lines(nhtemp$fitted)(在您的代码中)没有任何意义,因为您在原始数据中没有拟合值。

因此,为了重现书中的示例,您必须执行以下操作(基本上只是指数平滑)

plot(nhtemp)
lines(HoltWinters(nhtemp, gamma=F)$fitted[,2], col = "red")

【讨论】:

  • 谢谢大卫,但我不想使用任何外部包,想使用 RStudio 中已经安装的包。有什么解决办法吗?
  • 什么外部包?我刚刚通过添加gamma = F 运行您的代码。我没有添加任何包
  • 我知道。我的意思是我不想使用HoltWinters。对不起,如果我含糊其辞。
  • 我不熟悉 base R 中的任何其他指数平滑函数。你也可以从 forecast 包中运行它,不过如果你喜欢 library(forecast); plot(nhtemp); lines(ses(nhtemp)$fitted, col = "red")
  • 你能看看更新的问题吗?正如我所说,我应该编写自己的函数。如果我不应该使用像statsforecast 这样的任何外部软件包,我会被困在哪里开始!任何提示都非常感谢! :)
猜你喜欢
  • 1970-01-01
  • 2020-07-12
  • 2021-12-08
  • 1970-01-01
  • 2020-08-10
  • 2014-06-03
  • 1970-01-01
  • 2019-02-04
  • 2013-06-13
相关资源
最近更新 更多