Announcement

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

  • Generate correlation as variable

    Hi everyone,

    I am running a cross-sectional random effects regression. And I want to generate a new variable that consists of the correlation between two variables (say x and y) for each country. For each country this value will be a constant for all observations, because the correlation of x and y doesn't change and is unique for each country.

    Using the following command Stata gave me a long list of, essentially, correlation of x and y for each country.
    Code:
    bysort country: corr y x
    However, I failed to find a code that could generate what I need, which is a new variable that contains all these values.

    Appreciate if someone can help with this issue.

  • #2
    Here is one possible way, using statsby:

    Code:
    clear
    sysuse auto
    
    preserve
    tempfile the_rho
    statsby, by(foreign): pwcorr mpg weight
    save `the_rho'
    restore
    
    merge m:1 foreign using `the_rho', nogen

    Comment


    • #3
      See the corr() function for egen in egenmore from SSC. OR the functionality in rangestat from SSC. Here is an example of the second.


      Code:
      .  sysuse auto, clear
      (1978 automobile data)
      
      . rangestat (corr) mpg weight, int(foreign 0 0)
      
      . tabdisp foreign, c(corr*)
      
      ------------------------------------------------------
      Car       |
      origin    |            corr_nobs                corr_x
      ----------+-------------------------------------------
       Domestic |                   52            -.87594266
        Foreign |                   22            -.68285403
      ------------------------------------------------------

      Comment


      • #4
        Thank you so much Ken! It's solved!

        Comment

        Working...
        X