J. Alexander Sanden
J. Alexander Sanden

Reputation: 41

Calculate autocorrelation in panel data?

I have a large panel data set in the form:

ID | Time| X-VALUE
---| ----|-----
1  | 1   |x
1  | 2   |x
1  | 3   |x
2  | 1   |x
2  | 2   |x
2  | 3   |x
3  | 1   |x
3  | 2   |x
3  | 3   |x
.  | .   |.
.  | .   |.

More specifically, I have dataset of a large set of individual stock returns over a period of 30 years. I would like to calculate the "stock-specific" first (lag 1) autocorrelation in returns for all stocks individually.

I suspect that by applying the code: acf(pdata$return, lag.max = 1, plot = FALSE) I'll only get som kind of "average" autocorrelation value, is that correct?

Thank you

Upvotes: 4

Views: 2182

Answers (2)

Sebastian
Sebastian

Reputation: 1369

This is not exactly what was requested, but a real autocorrelation function for panel data in R is collapse::psacf, it works by first standardizing data in each group, and then computing the autocovariance on the group-standardized panel-series using proper panel-lagging. Implementation is in C++ and very fast.

Upvotes: 1

ekstroem
ekstroem

Reputation: 6171

You can split the data frame and do the acf on each subset. There are tons of ways to do this in R. For example

by(pdata$return, pdata$ID, function(i) { acf(i, lag.max = 1, plot = FALSE) })

You may need to change variable and data frame names to match your own data.

Upvotes: 3

Related Questions