在 R 中开始每日时间序列 [英] starting a daily time series in R
问题描述
我有一个关于网站访问者数量的每日时间序列.我的系列从 01/06/2014
开始到今天 14/10/2015
所以我想预测未来的访客数量.如何使用 R 阅读我的系列?我在想:
I have a daily time series about number of visitors on the web site. my series start from 01/06/2014
until today 14/10/2015
so I wish to predict number of visitor for in the future. How can I read my series with R? I'm thinking:
series <- ts(visitors, frequency=365, start=c(2014, 6))
如果是,并且在运行我的时间序列模型后 arimadata=auto.arima()
我想预测未来 6 天的访客数量,我该怎么做?
if yes,and after runing my time series model arimadata=auto.arima()
I want to predict visitor's number for the next 6o days, how can i do this?
h=..?
forecast(arimadata,h=..),
h
的值应该是什么?预先感谢您的帮助
the value of h
shoud be what ?
thanks in advance for your help
推荐答案
ts
规范有误;如果您将其设置为每日观察,那么您需要指定 2014 年的哪一天是 6 月 1 日,并在 start
中指定:
The ts
specification is wrong; if you are setting this up as daily observations, then you need to specify what day of the year 2014 is June 1st and specify this in start
:
## Create a daily Date object - helps my work on dates
inds <- seq(as.Date("2014-06-01"), as.Date("2015-10-14"), by = "day")
## Create a time series object
set.seed(25)
myts <- ts(rnorm(length(inds)), # random data
start = c(2014, as.numeric(format(inds[1], "%j"))),
frequency = 365)
请注意,我将 start
指定为 c(2014, as.numeric(format(inds[1], "%j")))
.所有复杂的部分都在计算 6 月 1 日是一年中的哪一天:
Note that I specify start
as c(2014, as.numeric(format(inds[1], "%j")))
. All the complicated bit is doing is working out what day of the year June 1st is:
> as.numeric(format(inds[1], "%j"))
[1] 152
一旦你有了这个,你就有效了:
Once you have this, you're effectively there:
## use auto.arima to choose ARIMA terms
fit <- auto.arima(myts)
## forecast for next 60 time points
fore <- forecast(fit, h = 60)
## plot it
plot(fore)
考虑到我提供的随机数据,这似乎很合适...
That seems suitable given the random data I supplied...
您需要根据您的数据为 auto.arima()
选择合适的参数.
You'll need to select appropriate arguments for auto.arima()
as suits your data.
请注意,x 轴标签指的是一年的 0.5(半年).
Note that the x-axis labels refer to 0.5 (half) of a year.
通过使用 zoo 包创建的 zoo
对象可能更容易做到这一点:
This might be easier to do via a zoo
object created using the zoo package:
## create the zoo object as before
set.seed(25)
myzoo <- zoo(rnorm(length(inds)), inds)
注意你现在不需要指定任何start
或frequency
信息;只需使用之前从每日 Date
对象计算的 inds
.
Note you now don't need to specify any start
or frequency
info; just use inds
computed earlier from the daily Date
object.
照原样进行
## use auto.arima to choose ARIMA terms
fit <- auto.arima(myts)
## forecast for next 60 time points
fore <- forecast(fit, h = 60)
绘图虽然会导致问题,因为 x 轴是自纪元 (1970-01-01) 以来的天数,所以我们需要抑制该轴的自动绘图,然后绘制我们自己的.这很简单,因为我们有 inds
The plot though will cause an issue as the x-axis is in days since the epoch (1970-01-01), so we need to suppress the auto plotting of this axis and then draw our own. This is easy as we have inds
## plot it
plot(fore, xaxt = "n") # no x-axis
Axis(inds, side = 1)
这只会产生几个标记的刻度;如果你想要更多的控制,告诉 R 你想要刻度和标签的位置:
This only produces a couple of labeled ticks; if you want more control, tell R where you want the ticks and labels:
## plot it
plot(fore, xaxt = "n") # no x-axis
Axis(inds, side = 1,
at = seq(inds[1], tail(inds, 1) + 60, by = "3 months"),
format = "%b %Y")
这里我们每 3 个月绘制一次.
Here we plot every 3 months.
这篇关于在 R 中开始每日时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!