Announcement

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

  • Calculating correlation and covariance matrix of variables

    Dear All,

    I have 3 variables - age, wage and black; and I am trying to have these variables' correlation and covariance matrix. I know the command "estat vce", but it requires some prior operations that I am not well aware of. I would highly appreciate your support.

    Thank you very much in advance!
    Nick

  • #2
    Like this:
    Code:
    webuse nlswork, clear
    gen black = 2.race
    
    corr age ln_wage black
    matrix correlations = r(C)
    
    corr age ln_wage black, covariance
    matrix  covariance = r(C)
    The command -estat vce- does not give the correlations or covariances of the variables themselves, it gives the covariances of the coefficients of those variables estimated from a regression in which they are predictors.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Like this:
      Code:
      webuse nlswork, clear
      gen black = 2.race
      
      corr age ln_wage black
      matrix correlations = r(C)
      
      corr age ln_wage black, covariance
      matrix covariance = r(C)
      The command -estat vce- does not give the correlations or covariances of the variables themselves, it gives the covariances of the coefficients of those variables estimated from a regression in which they are predictors.
      Thank you very much, Clyde! Can you please explain the meaning of "matrix correlations = r(C)" command?

      Comment


      • #4
        Many Stata commands, in addition to displaying results in the Results window are adding/modifying variables in the data set, leave results behind in r(). -corr- is one of those, and it leaves the matrix of correlations behind in a temporary matrix called r(C). The command -matrix correlations = r(C)- tells Stata to copy that to a permanent matrix called correlations. You can then use any of Stata's -matrix- commands to work with that.

        After any command, to see what is left behind in r() you can run -return list-.

        Note: The contents of r() get erased any time a new command uses it. So it is important to grab whatever you need from r() right after you run the command that creates it, or it may disappear. In other words, -matrix correlations = r(C)- should be run immediately after -corr age ln_wage_black-.

        Comment


        • #5
          Apart from what Clyde showed, the command -pwcorr- can be useful if one needs the significance of the correlations:

          Code:
          . sysuse auto
          (1978 automobile data)
          
          . correlate price mpg head
          (obs=74)
          
                       |    price      mpg headroom
          -------------+---------------------------
                 price |   1.0000
                   mpg |  -0.4686   1.0000
              headroom |   0.1145  -0.4138   1.0000
          
          
          . pwcorr price mpg head
          
                       |    price      mpg headroom
          -------------+---------------------------
                 price |   1.0000 
                   mpg |  -0.4686   1.0000 
              headroom |   0.1145  -0.4138   1.0000 
          
          . pwcorr price mpg head, sig
          
                       |    price      mpg headroom
          -------------+---------------------------
                 price |   1.0000 
                       |
                       |
                   mpg |  -0.4686   1.0000 
                       |   0.0000
                       |
              headroom |   0.1145  -0.4138   1.0000 
                       |   0.3313   0.0002
                       |
          
          .

          Comment

          Working...
          X