Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate autocorrelation in panel data?

Tags:

r

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

like image 324
J. Alexander Sanden Avatar asked Oct 19 '25 17:10

J. Alexander Sanden


1 Answers

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.

like image 83
ekstroem Avatar answered Oct 21 '25 09:10

ekstroem