Announcement

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

  • min, max, mean of correlaiton matrix

    I apologize if I've overlooked something, but I cannot find an answer. For simplicity, I'll use an example of 8 continuous variables. I compute their Pearson correlation matrix with corr v1-v8. I wish to know the minimum coefficient in this matrix, the maximum coefficient in this matrix, and the mean coefficient. Are there commands to do this? Thanks for your help.

  • #2
    Well, the maximum coefficient is always 1, on the main diagonal. I assume you only want to consider off-diagonal coefficients. Here's an illustration of the approach using the auto.dta:

    Code:
    sysuse auto, clear
    
    corr price mpg headroom weight
    
    matrix C = r(C)
    local nvar = rowsof(C)
    
    local min = 1
    local max = -1
    
    forvalues i = 1/`nvar' {
        forvalues j = `=`i'+1'/`nvar' {
            if C[`i', `j'] < `min' {
                local min = C[`i', `j']
            }
            if C[`i', `j'] > `max' {
                local max = C[`i', `j']
            }
        }
    }
    //  CALCULATE THE MEAN OFF-DIAGONAL ELEMENT
    matrix J = J(`nvar', 1, 1)
    matrix M = (J'*(C-I(`nvar'))*J)/(`nvar'*`=`nvar'-1')
    
    display "Minimum correlation = " %05.3f =`min'
    display "Maximum correlation = " %05.3f =`max'
    display "Mean correlation = " %05.3f =M[1,1]

    Comment


    • #3
      Here's another approach, using corrci from the Stata Journal.


      Code:
      . sysuse auto, clear
      (1978 Automobile Data)
      
      . 
      . corrci price mpg headroom weight, saving(results)
      
      (obs=74)
      
                         correlations and 95% limits
      price    mpg          -0.469   -0.630   -0.269
      price    headroom      0.115   -0.117    0.334
      price    weight        0.539    0.354    0.683
      mpg      headroom     -0.414   -0.587   -0.205
      mpg      weight       -0.807   -0.874   -0.710
      headroom weight        0.483    0.287    0.641
      
      . use results, clear . 
      . 
      . su r
      
          Variable |        Obs        Mean    Std. Dev.       Min        Max
      -------------+---------------------------------------------------------
                 r |          6   -.0921669    .5528692  -.8071749   .5386115

      Comment


      • #4
        Yet another way how this can be done goes like this:

        Code:
        . sysuse auto, clear
        (1978 Automobile Data)
        
        . corr price mpg headroom weight
        (obs=74)
        
                     |    price      mpg headroom   weight
        -------------+------------------------------------
               price |   1.0000
                 mpg |  -0.4686   1.0000
            headroom |   0.1145  -0.4138   1.0000
              weight |   0.5386  -0.8072   0.4835   1.0000
        
        
        . mat C = vec(r(C))
        
        . svmat C
        
        . summ C if C<1
        
            Variable |        Obs        Mean    Std. Dev.       Min        Max
        -------------+---------------------------------------------------------
                  C1 |         12   -.0921669    .5271401  -.8071749   .5386115

        Comment


        • #5
          Thanks. I'll take a look at these and give them a try. Appreciate your help.

          Comment

          Working...
          X