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?
The first way to check for autocorrelation in R is by using the ACF() function. This function is part of the stats package and computes and plots estimates of the autocorrelation. The ACF() function requires just one argument, namely a numeric vector with the residuals of the regression model.
The number of autocorrelations calculated is equal to the effective length of the time series divided by 2, where the effective length of a time series is the number of data points in the series without the pre-data gaps. The number of autocorrelations calculated ranges between a minimum of 2 and a maximum of 400.
Autocorrelation measures the degree of similarity between a time series and a lagged version of itself over successive time intervals. It's also sometimes referred to as “serial correlation” or “lagged correlation” since it measures the relationship between a variable's current values and its historical values.
zoo is the creator for an S3 class of indexed totally ordered observations which includes irregular time series.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With