Announcement

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

  • Correlation matrix: display the correlations between the same variables

    Hi everyone, I used the code from the post shown below and tried to get the correlation matrix. It works very well, thanks to the person who solved the question. The correlations between the same variables are not displayed as they are 1, because this is the requirement from the original post. However, I would like to get the correlations between the same variables to show that IT IS 1. From the code, I could not figure it out which line is supposed to be modified to displace the correlation 1. Any suggestions are apprieciated. Thank you.

    Original post: https://www.statalist.org/forums/for...relation-table

    Code:


    Code:
    sysuse auto, clear
    matrix drop _all  
    
    ** Set variables used in Summary and Correlation
    local variables length weight displacement price
    local labels `" "Length (in)" "Weight (lbs)" "Disp (ci)" "Price (USD)" "'  
    
    ** Descriptive statistics
    estpost summarize `variables'
    matrix table = ( e(count) \ e(mean) \ e(sd) )
    matrix rownames table = count mean sd
    matrix list table  
    
    ** Correlation matrix
    correlate `variables'
    matrix C = r(C)
    local corr : rownames C
    matrix table = ( table \ C )
    matrix list table  
    
    estadd matrix table = table  
    
    local cells table[count](fmt(0) label(Count)) table[mean](fmt(2) label(Mean)) table[sd](fmt(2) label(Standard Deviation))
    local collab
    local drop
    local i 0
    foreach row of local corr {    
    local drop `drop' `row'    
    local cells `cells' table[`row']( fmt(4) drop(`drop') label((`++i')) )    
    local lbl : word `i' of `labels'    
    local collab `" `collab' `row' "(`i') `lbl'" "'
    }
    display "`cells'"
    display `"`collab'"'  
    
    esttab using Report.rtf, ///        
    replace ///        
    noobs ///        
    nonumbers ///        
    compress ///        
    cells("`cells'") ///          
    coeflabels(`collab')
    Click image for larger version

Name:	image_14423.png
Views:	1
Size:	218.8 KB
ID:	1653668



    Last edited by mws macekk; 09 Mar 2022, 10:45.

  • #2
    I provided the original code, but it took me a while just now to figure out what I was doing. :-)

    Change this loop
    Code:
    foreach row of local corr {    
    local drop `drop' `row'    
    local cells `cells' table[`row']( fmt(4) drop(`drop') label((`++i')) )    
    local lbl : word `i' of `labels'    
    local collab `" `collab' `row' "(`i') `lbl'" "'
    }
    to this loop
    Code:
    foreach row of local corr {    
    local cells `cells' table[`row']( fmt(4) drop(`drop') label((`++i')) )    
    local lbl : word `i' of `labels'    
    local collab `" `collab' `row' "(`i') `lbl'" "'
    local drop `drop' `row'    
    }
    and you will get what you want, I believe.

    Comment


    • #3
      Originally posted by William Lisowski View Post
      I provided the original code, but it took me a while just now to figure out what I was doing. :-)

      Change this loop
      Code:
      foreach row of local corr {
      local drop `drop' `row' 
      local cells `cells' table[`row']( fmt(4) drop(`drop') label((`++i')) )
      local lbl : word `i' of `labels'
      local collab `" `collab' `row' "(`i') `lbl'" "'
      }
      to this loop
      Code:
      foreach row of local corr {
      local cells `cells' table[`row']( fmt(4) drop(`drop') label((`++i')) )
      local lbl : word `i' of `labels'
      local collab `" `collab' `row' "(`i') `lbl'" "'
      local drop `drop' `row' 
      }
      and you will get what you want, I believe.
      Thank you very much. It works perfectly.

      Comment


      • #4
        Originally posted by William Lisowski View Post
        I provided the original code, but it took me a while just now to figure out what I was doing. :-)

        Change this loop
        Code:
        foreach row of local corr {
        local drop `drop' `row' 
        local cells `cells' table[`row']( fmt(4) drop(`drop') label((`++i')) )
        local lbl : word `i' of `labels'
        local collab `" `collab' `row' "(`i') `lbl'" "'
        }
        to this loop
        Code:
        foreach row of local corr {
        local cells `cells' table[`row']( fmt(4) drop(`drop') label((`++i')) )
        local lbl : word `i' of `labels'
        local collab `" `collab' `row' "(`i') `lbl'" "'
        local drop `drop' `row' 
        }
        and you will get what you want, I believe.
        Hi, William. Sorry I also have a follow-up question referring to the original post. "How would you modify the above code so that it includes significance stars for the correlation part of the table? For instance *** for significant at p < .01, ** for significant at p < .05, and * for p < .1. " Thank you so much.

        Comment


        • #5
          I am sorry to say that I have no idea. It requires expertise in esttab that I do not have - I do not make regular use of it.

          Comment

          Working...
          X