Announcement

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

  • Add mean and sd column in correlation matrix in Stata

    I'm trying to create correlation matrix that also includes means and sd's of each variable.
    ```
    ** Set variables used in Summary and Correlation
    local variables relationship commission anxiety enjoyment negotiation_efficacy similarity_values similarity_behaviors SPT_confidence own_SPT_effort


    ** Descriptive statistics
    estpost summarize `variables'
    matrix table = ( e(mean) \ e(sd) )
    matrix rownames table = mean sd
    matrix list table

    ** Correlation matrix
    correlate `variables'
    matrix C = r(C)
    local k = colsof(C)
    matrix C = C[1..`=`k'-1',.]
    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 drop
    foreach row of local corr {
    local drop `drop' `row'
    local cells `cells' table[`row'](fmt(4) drop(`drop'))
    }
    display "`cells'"

    esttab using Report.rtf,
    replace
    noobs
    nonumbers
    compress
    cells("`cells'")
    ```


    If it helps, this is what the correlation code looks like:

    ```
    asdoc corr relationship commission anxiety enjoyment negotiation_efficacy similarity_values similarity_behaviors SPT_confidence own_SPT_effort ranger_SPT_effort cooperative_motivation competitive_motivation, nonum
    ```

    This correlation matrix looks exactly how it should, but I'm essentially hoping to add means and sd's to the beginning.

    *This is cross-posted here: https://stackoverflow.com/questions/...44775_61471719

  • #2
    *Cross-post has been deleted.

    Comment


    • #3
      I might do something like this...
      Code:
      sysuse auto, clear
      
      local vars headroom trunk weight length
      
      estpost summarize `vars'
      mat t = e(mean)',e(sd)'
      mat colnames t = mean sd
      
      corr `vars'
      
      mat t = t,r(C)
      mat l t

      Comment


      • #4
        Thanks Lance!! Just so I make suer I am translating what you have correctly, would you write something like this,

        ```
        local vars corr Relationship Commission Anxiety Enjoyment Negotiation_efficacy Similarity_values Similarity_behaviors SPT_confidence Own_SPT_effort Rangers_SPT_effort Cooperative_motivation Competitive_motivation

        estpost summarize 'vars'
        mat Table_1 = e(mean)', e(sd)'
        mat colnames Table_1 = mean sd

        corr 'vars'

        mat Table_1 = t, r(C)
        mat l Table_1
        ```

        I'm sure I'm translating it not how you expected, but I'm getting the following error code:

        ```
        estpost summarize 'vars'
        ' invalid name
        r(198);

        . mat Table_1 = e(mean)', e(sd)'
        conformability error
        r(503);

        . mat colnames Table_1 = mean sd
        mean not found
        r(111);

        .
        . corr 'vars'
        ' invalid name
        r(198);

        .
        . mat Table_1 = t, r(C)
        t not found
        r(111);

        . mat l Table_1
        matrix operation not found
        r(501);

        .
        ```

        Comment


        • #5
          Claire,

          You have
          Code:
          'vars'
          but you should have
          Code:
          `vars'
          You had the opening of the local macro correct in your original. It's the key left of the numeral 1.

          Comment


          • #6
            Great, thanks Lance!

            Comment


            • #7
              Is there an easy way to modify this so that one gets one decimal places each for the mean and sd and two decimal places for the correlations, and gets stars for statistical significance?

              Comment

              Working...
              X