在R中正确设置时间序列的问题

时间:2015-06-16 21:28:54

标签: r time-series

我一直试图对一些时间序列数据做一些基本的分析。但是,我一直在尝试做的任何事情上都会出现此错误

Error in decompose(data_ts, type = c("additive")) : 
 time series has no or less than 2 periods

我认为问题在于我没有为时间序列分析正确设置数据。我正在处理运行M-F大约一年的数据。下面是我用来将系列转换为时间序列的代码

train=xts(data$x,as.Date(data$Date,format='%m/%d/%Y'),frequency=365)
data_ts=as.ts(train)
attributes(data_ts)
$tsp
[1]   1 277   1
$class
[1] "ts" 

但是当我尝试对时间序列数据进行任何类型的分析时,我会收到:

dcomp=decompose(data_ts,type=c('additive'))
Error in decompose(data_ts, type = c("additive")) : 
time series has no or less than 2 periods

我是否错误地设置了时间序列?是否有一个更好的时期我应该选择频率,因为从技术上讲,我没有一整年的数据?

谢谢!

3 个答案:

答案 0 :(得分:3)

我没有看到xts频率参数与ts频率参数做同样的事情。

因此,我假设您需要在使用ts之前将数据转换为decompose对象。我让它工作的方式如下:

使用以下数据:

data(sample_matrix)
df <- as.data.frame(sample_matrix )
df$date <- as.Date(row.names(df))

如果我执行以下操作:

dfxts <- xts(df[1:4], order.by=df$date, frequency=12)
decompose(dfts)
Error in decompose(dfts) : time series has no or less than 2 periods

我得到了和你一样的错误。

但是,如果我将其转换为ts对象并使用该频率参数:

#use as.ts to convert into ts
#make sure your data frame consists of numeric columns otherwise it will fail
#drop all the others
#in my case df[1:4] has numeric values. I use the date as a separate vector.
dfts <- as.ts(xts(df[1:4], order.by=df$date))
#I guess splitting by month would make sense in your case.
#I think ts works with frequency 4 (quarterly) or 12 (monthly)
#If you see your dfts now you ll see the difference
dfts <- ts(dfts, frequency=12)

然后它起作用:

dcomp <- decompose(dfts) 

输出:

> str(dcomp)
List of 6
 $ x       : mts [1:180, 1:4] 50 50.2 50.4 50.4 50.2 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:4] "Open" "High" "Low" "Close"
  ..- attr(*, "tsp")= num [1:3] 1 15.9 12
  ..- attr(*, "class")= chr [1:3] "mts" "ts" "matrix"
 $ seasonal: Time-Series [1:720] from 1 to 60.9: -0.00961 0.02539 0.06149 0.01773 -0.00958 ...
 $ trend   : mts [1:180, 1:4] NA NA NA NA NA ...
  ..- attr(*, "tsp")= num [1:3] 1 15.9 12
  ..- attr(*, "class")= chr [1:2] "mts" "ts"
 $ random  : mts [1:180, 1:4] NA NA NA NA NA ...
  ..- attr(*, "tsp")= num [1:3] 1 15.9 12
  ..- attr(*, "class")= chr [1:3] "mts" "ts" "matrix"
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:4] "x - seasonal.x.Open" "x - seasonal.x.High" "x - seasonal.x.Low" "x - seasonal.x.Close"
 $ figure  : num [1:12] -0.00961 0.02539 0.06149 0.01773 -0.00958 ...
 $ type    : chr "additive"
 - attr(*, "class")= chr "decomposed.ts"

答案 1 :(得分:1)

尝试使用这种通用方法处理您的数据集:

# Pre-loading the data
library(TSA)
library(mgcv)
#Choose the 'Temp Monthly.csv' dataset, wherever it is located on your computer
#Additionally, just skip this step and replace ‘fname’ with the files direct location
fname <- file.choose()
#Load Data
data <- read.csv(fname)
data <- data[,2]
#Convert to TS data in proper frame
temp <- ts(data,start=c(1950,1),freq=12)
# Plotting Time Plots
ts.plot(temp,ylab="Temperature")
abline(a=mean(temp),b=0,col='red')

这可能会有所帮助。有关R中时间序列的更多详细信息,本文中的内容可能会有所帮助:https://medium.com/analytics-vidhya/time-series-analysis-101-in-r-and-python-1e1a7f7c3e51

如果您需要任何帮助,请随时发表评论。 :)

答案 2 :(得分:0)

你改变频率是正确的。现在,你说每个时期会有365个观测值,所以你的数据周期不到1个。您可以调整它以将频率设置为5,因为您每周获得5次观察。这不是唯一的选择,只是一个应该工作的例子:)