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

Issue with setting up time series correctly in R

我一直在尝试对一些时间序列数据做一些基本的分析。但是,无论我尝试做什么,我都会收到此错误

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

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

谢谢!

关于改变频率,你是对的。现在,您说的是每个周期有 365 个观测值,因此您的数据周期少于 1 个。您可以调整它以将频率设置为 5,因为您每周获得 5 次观察。这不是唯一的选择,只是一个应该有效的例子:)

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

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

使用以下数据:

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"

对您的数据集尝试这种通用方法:

# 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

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