Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Pairwise Correlation between Groups within a Variable

    Dear all,

    Suppose I have the following dataset:

    Code:
    clear
    input str7 yq   int firmid float  retn
    2000q1     1    0.2564
    2000q2     1    0.2700
    2000q3     1    0.2160
    2000q4     1    0.1005
    2000q1     2    0.2493
    2000q2     2    0.3820
    2000q3     2    0.2179
    2000q4     2    0.3778
    2000q1     3    0.9692
    2000q2     3    0.4204
    2000q3     3    0.9001
    2000q4     3    0.2245
    2000q1     4    0.7196
    2000q2     4    0.3100
    2000q3     4    0.5980
    2000q4     4    0.4791
    2000q1     5    0.1168
    2000q2     5    0.2005
    2000q3     5    0.1802
    2000q4     5    0.2783
    end
    I'd like to calculate the pairwise correlation between retn for each pair of firms (e.g., correlation between firmid 1's retn and firmid 2's retn ) and store correlation coefficients in separate variables.

    Could anyone please help me with this?

    Thank you very much for your help,

    Vinh

  • #2
    Each pairwise correlation is a single number, so it makes little sense to waste a whole variable on each. The natural way to represent these pariwise correlations would be as a matrix.

    To get there, this is one of the few situations where a wide data layout works better.
    Code:
    reshape wide retn, i(yq) j(firmid)
    pwcorr retn*
    matrix wanted = r(C)

    Comment


    • #3
      Thank you so much for your help, Clyde. Your code works perfectly.

      Vinh

      Comment

      Working...
      X