开发者

How to calculate autocorrelation in r (zoo object)

I am trying to check for auto-correlation in a zoo object (monthly data with several columns) using:

acf(jan, plot=F)$acf[2]

but I get the following error:

Error in na.fail.default(as.ts(x)) : missing values in object

To simplify, I extracted just one of the columns which I called "a" (so now I have a simple zoo object with index and data), and used:

开发者_如何学运维acf(a)

but still get the same error. Can't acf be used in zoo objects?


Just use

acf(coredata(jan))

That should work fine. Keep in mind that you have to provide a regularly spaced time series for that to give you a meaningful answer.


The default behaviour for acf is na.action = na.fail. Try setting it to na.omit or na.pass in your call acf(..., na.action = na.omit)


I had the same problem as you when trying to use the ACF function on monthly S&P returns. Turns out the coredata function solved the problem as it stripped date information from returns in my data set from yahoo finance.

you might want to give it a shot!


Or self made

autocorrplot <- function(x)
{
  n <- length(x)
  barplot(sapply(1:10,function(i) cor(x[-i:-1],x[(-n-1+i):-n])))
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜