Take the 2-minute tour ×

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:


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

share|improve this question
add comment

migrated from stats.stackexchange.com Sep 5 '11 at 14:24


4 Answers

up vote 7 down vote accepted

Just use


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.

share|improve this answer
thanks! It now works if I do it just for one column but not for the matrix, it gives me an error: > acf(coredata(feb)) Error in acf(coredata(feb)) : 'lag.max' must be at least 0 –  sbg Sep 7 '11 at 15:26
I don't think you can use acf on multivariate series. What do you want to obtain the acf of each column? Or are you interested in the cross correlation between the columns (that would be the ccf not the acf)? –  Dr G Sep 7 '11 at 16:09
add comment

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)

share|improve this answer
thanks but my data doesn't have NAs –  sbg Sep 5 '11 at 16:14
@sbg, acf works on regularly spaced data so acf first expands the time series to a regularly spaced one inserting NAs as needed to make it regularly spaced. –  G. Grothendieck Sep 5 '11 at 17:01
@Grothendieck, thanks but it is monthly data so it is regularly spaced –  sbg Sep 6 '11 at 12:38
add comment

Or self made

autocorrplot <- function(x)
  n <- length(x)
  barplot(sapply(1:10,function(i) cor(x[-i:-1],x[(-n-1+i):-n])))
share|improve this answer
add comment

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!

share|improve this answer
add comment

Your Answer


