尝试在R中使用stl和分解函数时出错 [英] Error when trying to use stl and decompose functions in R

查看:619
本文介绍了尝试在R中使用stl和分解函数时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个简单的时间序列,我向sin函数添加了一点噪音,并尝试使用R中的"stl"和"decompose"函数对其进行分解,而我的序列肯定有两个以上的周期,并且定期,R会给我两个函数带来以下错误:

I have made a simple time-series, i added a little noise to a sin function and tried to decompose it using the "stl" and "decompose" function in R, while my series definitely has more than 2 period and is periodic, R gives me the following error for both functions:

x
  [1]  1.4537365796  2.7185844368  2.8394728999  3.8926989923  4.3405508086  5.1959080871
  [7]  5.6602505790  5.4829985648  5.6357660330  4.6084976233  4.6617322922  4.0286486832
 [13]  3.3641752333  1.7408063182  0.8815147612  0.2895139342 -0.5402768515 -1.5612641107
 [19] -2.1584502547 -2.9878043526 -3.5545638149 -4.0530074199 -4.0748538612 -4.7581704662
 [25] -4.6555349052 -4.0726206240 -3.1646413472 -2.6934453823 -2.2364605277 -1.2643569882
 [31] -0.1202011946  1.1136371449  2.2504199271  3.0313528996  3.5384449109  4.5176211013
 [37]  5.4013172839  5.4252837451  5.4768196692  5.8979709077  5.6698285659  4.5133489450
 [43]  4.2702602998  3.5180837069  2.2652913344  1.1975595698  0.5412697849 -0.5966162032
 [49] -1.0827728340 -1.8488242277 -3.4118061838 -3.9009752140 -3.9102671954 -4.3486102172
 [55] -4.7481017993 -4.0097598695 -3.9078554267 -3.8070416888 -2.5968567322 -2.2567568949
 [61] -1.1423907008  0.0002492447  0.4338279080  1.2431986797  2.3216397323  3.3235925116
 [67]  4.1591487169  4.9028481873  5.4535861470  5.0579349546  5.1548777627  4.9707124992
 [73]  5.4496833187  4.4563072487  4.1301372986  2.4594352788  1.7253019929  0.6961453965
 [79]  0.4281167695 -1.3152944759 -1.8645880957 -2.5764132038 -3.7681528112 -4.3731672862
 [85] -3.9940201611 -4.5497596299 -4.9496796983 -4.1233093447 -3.7759837204 -3.3359027749
 [91] -2.3518009102 -1.7488933797 -0.7225148838  0.5395759836  1.0496249652  2.0383715782
 [97]  3.2357449979  3.8028316517  5.0346866280  5.2154265148

fit<- stl(x, t.window=15, s.window="per", robust=TRUE)
Error in stl(x, t.window = 15, s.window = "per", robust = TRUE) :series is not periodic or has less      than two periods

fit<- decompose(x,type="multiplicative")
Error in decompose(x, type = "multiplicative") :time series has no or less than 2 periods

有人可以帮我解决这个问题吗?

Would someone help me with this problem please?

推荐答案

从错误消息中不是很明显:

Isn't it obvious from the error message:

time series has no or less than 2 periods

? R不会告诉您您的数据不是是周期性的,只是您传递的数据没有指示它们是周期性的.

? R's not telling you your data aren't periodic, just that the data you passed it have no indication that they are periodic.

从R的角度来看,您的时间序列x没有周期性.您似乎忘记了说出ts()的周期性,或者说出了错误.由于您没有显示x的创建方式,因此除了告诉您返回并创建x使其具有> = 2个句点之外,我们无能为力.

Your time series x doesn't have an periodicity from the point of view of R. It looks like you forgot to tell, or made an error in telling, ts() what the periodicity is. As you don't show how x was created, there isn't much we can do except tell you to go back and create x so that it does have >=2 periods.

这里的要点是,R本身无法推断出单位时间内的观测频率.您必须告诉ts()该信息.您可以通过多种方式执行此操作:

The point here is that on it's own, R can't deduce what the frequency of observation is per unit time. You have to tell ts() that information. You can do this in a number of ways:

frequency:每单位时间的观察次数.

frequency: the number of observations per unit of time.

deltat:连续两次之间的采样周期的分数 观察;例如,每月数据的1/12.只有一个 应该提供frequencydeltat.

deltat: the fraction of the sampling period between successive observations; e.g., 1/12 for monthly data. Only one of frequency or deltat should be provided.

如果您未提供其中之一,则ts()使用默认值frequency = 1deltat = 1,这将指示每单位时间一个观测值的时间序列(例如,一年一次).

If you don't provide one of these, ts() uses the defaults frequency = 1, deltat = 1 which would indicate a time series of one observation per unit time (one per year for example).

stl()需要一个"ts"分类的对象-如果不提供该对象,它将通过as.ts()将输入数据强制转换为"ts"对象.此功能将使用我上面介绍的默认设置.

stl() requires a "ts" classed object - if you don't provide one, it will coerce the input data to a "ts" object via as.ts(). This function will use the defaults which I describe above.

我认为这里发生的事情是,您没有意识到stl()需要一个"ts"类对象,也没有意识到当您仅提供观测矢量时,它就为您的数据创建了一个不合适的对象.

What I think has happened here is that you didn't realise that stl() requires a "ts" class object nor that it created an inappropriate one for your data when you just supplied the vector of observations.

解决方案是通过ts()显式创建"ts"分类对象,并指定frequencydeltat之一.

The solution would be to explicitly create the "ts" classed object via ts() and specify one of frequency or deltat.

例如

dat <- cumsum(rnorm(12*4))
x <- ts(dat)
stl(x, "periodic")
xx <- ts(dat, frequency = 12)
stl(xx, "periodic")

在这里,我使用frequency = 12表示每单位时间有12个观测值,例如月度数据.

Here I used frequency = 12 to indicate that there are 12 observations per unit time --- such as with monthly data.

以上为我提供了

R> stl(x, "periodic")
Error in stl(x, "periodic") : 
  series is not periodic or has less than two periods

R> stl(xx, "periodic")
 Call:
 stl(x = xx, s.window = "periodic")

Components
       seasonal    trend remainder
Jan 1 -0.103529  0.55245  -0.44301
Feb 1  0.001333  0.56981   0.86135
Mar 1 -0.382075  0.58717   1.11162
Apr 1  0.010552  0.59891  -1.04966
....

鉴于时间序列的长度,我怀疑您想使用frequency = 10;那表示您每年有十个观测值.如果该系列每年有更多观测值,例如月度数据为12,但是您没有最近两个月或前两个月(即没有丢失的数据,NA),则稍后(较早)才开始(结束) ),因此该系列的一端或两端没有整整一年的数据.

For your data I suspect you want frequency = 10 given the length of the time series; that would say that you have ten observations per year. If the series has more observations per year, say 12 for monthly data, but you don;t have the last two or first two months (i.e. there is no missing data, NAs) you just started (ended) later (earlier) in the year and hence don't have a full years worth of data at one or both ends of the series.

这篇关于尝试在R中使用stl和分解函数时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆