How to determine lag in time series?
I am working on a time series problem and want to decompose to get some basic info on lag. Goal is to evaluate lag of the output variable based on changes in the change variable as part of the example data.frame below. Full data.frame has more data but it is all by week and follows开发者_Python百科 the same structure as this example.
year <- c(2010,2010,2010,2010)
week <- c("P7W1","P7W2","P7W3","P7W4")
output <- c(3295,4379,4284,4832)
change <- c(1912,2177,1587,2708)
timeTest <- data.frame(year,week,output,change)
I created a time series object with the following.
timeObject <- ts(timeTest, start=c(2010,7), frequency=52)
However, when I ran decompose(timeObject) I got an error message stating that I had no or less than 2 periods. I'm clearly missing something here, any advice is appreciated.
In order to run decompose() or its cousin stl(), you need to have at least two complete periods of data. Period is defined as 1/frequency. So if you are working with weekly data, where frequency=52, your period is one year and you need two years of data.
For example, running decompose() with data set of 103 weeks will fail:
decompose(ts(runif(103), frequency=52))
Error in decompose(ts(runif(103), frequency = 52)) :
time series has no or less than 2 periods
But running decompose() with 104 data points works:
decompose(ts(runif(104), frequency=52))
$seasonal
Time Series:
Start = c(1, 1)
End = c(2, 52)
Frequency = 52
[1] -0.015447737 0.392006955 0.185528936 0.372505618 -0.079588619
[6] -0.351928149 -0.472617951 -0.306461367 -0.475596801 0.266197693
[11] 0.167468113 -0.332837411 -0.427845149 -0.001199151 0.276361737
...
$type
[1] "additive"
attr(,"class")
[1] "decomposed.ts"
PS. You may also want to have a look at acf() which will compute autocorrelation. This will work even if you have less than two complete years of data. For example:
acf(ts(runif(100), frequency=52))
精彩评论