Announcement

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

  • egen corr() with weights

    Dear Statalisters,

    I have panel data and want to compute a time series of cross-sectional correlations of two variables, x1 and x2.

    So far, my approach has been:
    Code:
    bysort year: egen correlation = corr(x1 x2)
    However, the corr() command from egenmore does not allow weights. I am struggling to find an efficient work around...

    Any suggestions are highly appreciated!

    Best,
    John
    Last edited by John Hanser; 06 Jul 2022, 18:15.

  • #2
    You doing mention the type of weights but I would look at -corr- and use those returned results.

    Comment


    • #3
      Originally posted by Leonardo Guizzetti View Post
      You doing mention the type of weights but I would look at -corr- and use those returned results.

      Thanks for the response, Leonardo. I did exactly that now... I was just hoping there is an easier way.

      Comment


      • #4
        Try this:

        Code:
        . sysuse auto, clear
        (1978 automobile data)
        
        . levelsof rep, local(repslevs)
        1 2 3 4 5
        
        . gen corrmpgheadroom = .
        (74 missing values generated)
        
        . foreach l of local repslevs {
          2. correlate mpg headroom [w=weight] if rep == `l'
          3. replace corrmpgheadroom = r(rho) if rep == `l'
          4. }

        Comment

        Working...
        X