R 从数据帧创建ts并使用STL进行分解

R 从数据帧创建ts并使用STL进行分解,r,stl,time-series,R,Stl,Time Series,我是R新手,正在创建我的第一个时间序列分析。 我的第一步是将数据(数据帧)转换为时间序列,并: 这是转换前的数据: Date Value Season 1576 2017-01-30 45330.34 0 1604 2017-02-27 43757.68 0 1693 2017-03-30 50092.90 1 1723 2017-04-29 39405.65 0 1812 2017-05-30 42031.80 0

我是R新手,正在创建我的第一个时间序列分析。 我的第一步是将数据(数据帧)转换为时间序列,并:

这是转换前的数据:

           Date    Value Season
1576 2017-01-30 45330.34      0
1604 2017-02-27 43757.68      0
1693 2017-03-30 50092.90      1
1723 2017-04-29 39405.65      0
1812 2017-05-30 42031.80      0
1842 2017-06-29 40008.67      0
格式如下:

> str(mth3)
'data.frame':   66 obs. of  3 variables:
 $ Date   : Date, format: "2012-01-30" "2012-02-28" ...
 $ Value  : num  40222 43437 46047 33813 35757 ...
 $ Season : int  0 0 1 1 0 0 0 0 0 2 ...
当我使用此代码转换数据时:

mth4 <- ts(mth3, frequency=12, start=c(2012,1), end=c(2017,6)) 
mth4
中的数据格式为:

-Series [1:66, 1:3] from 2012 to 2017: 15369 15398 15429 15459 15490 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:3] "Date" "Value" "Season"
将数据分解为趋势、季节性和随机行为时

mth_stl <- stl(mth4, s.window="periodic")

我猜我的格式有问题,但我不知道为什么,并且花了大量时间搜索此论坛。

尽管有评论和我对该软件包的喜爱,但您不需要xts。:)

问题出现在错误消息中,“只允许使用单变量序列”。您的
mth4
对象是一个多变量时间序列
stl()
只接受单变量序列,因此您需要提取
列以传递到
stl()


您可能需要
xts
库(xts);mth4@akrun-感谢您的评论。我已经更改了结构,日期变量现在被排除,但错误仍然是一样的。不幸的是。>str(mth4)2012-01-30/2017-06-29上的“xts”对象包含:数据:num[1:66,1:2]4022433746047338133757…-attr(*,“dimnames”)=2..$:NULL..$:chr[1:2]“值”“季节”的列表,由类的对象索引:[Date]TZ:UTC xts属性:NULL
mth_stl <- stl(mth4, s.window="periodic")
only univariate series are allowed
R> mth_stl <- stl(mth4[,"Value"], s.window="periodic")
R> str(mth_stl)
List of 8
 $ time.series: Time-Series [1:66, 1:3] from 2012 to 2017: 621 175 425 -104 2528 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:3] "seasonal" "trend" "remainder"
 $ weights    : num [1:66] 1 1 1 1 1 1 1 1 1 1 ...
 $ call       : language stl(x = mth4[, "Value"], s.window = "periodic")
 $ win        : Named num [1:3] 661 19 13
  ..- attr(*, "names")= chr [1:3] "s" "t" "l"
 $ deg        : Named int [1:3] 0 1 1
  ..- attr(*, "names")= chr [1:3] "s" "t" "l"
 $ jump       : Named num [1:3] 67 2 2
  ..- attr(*, "names")= chr [1:3] "s" "t" "l"
 $ inner      : int 2
 $ outer      : int 0
 - attr(*, "class")= chr "stl"
mth4 <- ts(mth3[, c("Value", "Season")], frequency=12, start=c(2012,1), end=c(2017,6))