Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate autocorrelation in r (zoo object)

Tags:

r

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?

like image 426
sbg Avatar asked Sep 05 '11 13:09

sbg


People also ask

How does R calculate autocorrelation?

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.

How do you calculate autocorrelation?

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.

What does autocorrelation mean in R?

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.

What is a zoo object in R?

zoo is the creator for an S3 class of indexed totally ordered observations which includes irregular time series.


1 Answers

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.

like image 98
Dr G Avatar answered Sep 28 '22 13:09

Dr G