Announcement

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

  • Pairwise correlations

    I have a set of variables named for countries Algeria to Zimbabwe ordered alphabetically. There are other variables Algeria_cor to Zimbabwe_cor interspersed:

    Algeria float %9.0g
    Algeria_cor double %9.2f Algeria v2
    Angola float %9.0g
    Angola_cor double %9.2f Angola v2
    Argentina float %9.0g
    Argentina1 float %9.0g Argentina education lagged
    Argentina_cor double %9.2f Argentina v2
    Argentinachange float %9.0g Argentina change education
    Armenia_cor double %9.2f Armenia v2
    Australia float %9.0g
    Australia_cor double %9.2f Australia v2
    Austria float %9.0g
    Austria_cor double %9.2f Austria v2
    Bangladesh float %9.0g
    Bangladesh_cor double %9.2f Bangladesh v2
    Barbados_cor double %9.2f Barbados v2
    Belarus_cor double %9.2f Belarus v2
    Belgium float %9.0g
    Belgium_cor double %9.2f Belgium v2

    etc.

    I want to estimate the correlations between each pair : Algeria Algeria_cor, etc. The other correlations are not relevant. I can;t figure out how to do this so any help will be appreciated.

    Thanks,

    Ric


  • #2
    I am sure that you know a way. You could just type out all the variable names two by two. But you are understandably reluctant to do that, believing there should be a shorter way.

    If we put all the *_cor names in a bag, then we are halfway there. We go through the bag, pick each variable name in turn and find its sibling by using the name without the _cor suffix.

    Not tested. Change 25 as needed. See help macro

    Code:
    unab corvars : *_cor 
    
    foreach second of local corvars { 
          local first : subinstr local second "_cor" "" 
          qui corr `first' `second' 
          di "`first'{col 25}" %4.3 r(rho) 
    }
    See also SJ-10-3 dm0051 . . . Tip 91: Putting unabbreviated varlists into local macros
    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
    Q3/10 SJ 10(3):503--504 (no commands)
    tip on interactively using unab to expand a varlist and
    place the resulting unabbreviated varlist in a local macro

    http://www.stata-journal.com/sjpdf.h...iclenum=dm0051

    Comment


    • #3
      Thanks Nick but this is what I got:

      foreach second of local corvars {
      2. local first : subinstr local second "_cor" ""
      3. qui corr `first' `second'
      4. di "`first'{col 25}" %4.3 r(rho)
      5. }
      Afghanistan invalid %format
      r(120);

      Any suggestions?

      Comment


      • #4

        Sorry, my typo, at least.

        Code:
        unab corvars : *_cor  
        foreach second of local corvars {        
              local first : subinstr local second "_cor" ""        
              qui corr `first' `second'        
              di "`first'{col 25}" %5.3f r(rho)  
        }

        Comment


        • #5
          Thanks so much, Nick. One more question please. How would I also list the N for the correlations? I tried adding N and it didn't work.

          Comment


          • #6
            That is soluble by reading the help for correlate. After running the command, the number of paired values used is stored in r(N). You therefore can extend the command

            Code:
             
             di "`first'{col 25}" %5.3f r(rho)
            to something like

            Code:
             
             di "`first'{col 25}" %5.3f r(rho) %8.0f r(N)
            where the format %8.0f is generous to ensure some spaces between the numerical results.

            Comment


            • #7
              Thanks much Nick.

              Comment

              Working...
              X