Calculate within correlation in panel data long form

Dim*_*ima 2 panel correlation stata cross-correlation panel-data

We have a simple panel data set in long form, which has the following structure:

i   t            x
1   Aug-2011     282
2   Aug-2011    -220
1   Sep-2011     334
2   Sep-2011     126
1   Sep-2012    -573
2   Sep-2012     305
1   Nov-2013     335
2   Nov-2013     205
3   Nov-2013     485
Run Code Online (Sandbox Code Playgroud)

I would like to get the cross-correlation between each i within the time-variable t.

This would be possible by converting the data in wide format. Unfortunately, this approach is not feasible due to the big number of i and t values in the real data set.

是否可以在此虚拟命令中执行类似的操作:

by (tabulate t): corr x 
Run Code Online (Sandbox Code Playgroud)

Pea*_*cer 5

您可以x使用community-contributed命令的reshape选项轻松地计算单个变量的相关性,例如跨面板组:pwcorrf

ssc install pwcorrf
Run Code Online (Sandbox Code Playgroud)

为了说明起见,请考虑您的玩具示例(略有简化):

clear

input i t x 
1 2011 282
2 2011 -220
1 2012 334
2 2012 126
1 2013 -573
2 2013 305
1 2014 335
2 2014 205
3 2014 485
end

xtset i t
       panel variable:  i (unbalanced)
        time variable:  t, 2011 to 2014
                delta:  1 unit

pwcorrf x, reshape
Variable(s): x
Panel var: i

corrMatrix[3,3]
            1           2           3
1           1           0           0
2  -.54223207           1           0
3           .           .           1
Run Code Online (Sandbox Code Playgroud)