Announcement

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

  • How to capture the correlation between two variables and make the correlation become a new variable

    I am conducting a country pair wise correlation between accounting accruals (var1) and changes in operating cash flows (var2).
    The contemporaneous correlation between these two variables is a measure of earnings smoothing. I use the following codes:

    <1>duplicates drop year, force
    <2>xtset gvkey year
    <4>gen var1=accruals
    <3>gen var2=d.onacf
    <4>statsby "corr var1 var2" r(rho), by(loc)

    I wish to use this code to capture the correlation, but it turns out that this code will delete all my observations.

  • #2
    You can now merge back with the original dataset. By the way, there is no reason for var1 var2 as you can use the original variable names.

    For another approach see
    rangestat from SSC.

    Comment


    • #3
      I wish to obtain the correlation between two variables (accruals and d.oancf), why my codes (stats by) do not give favorable results, and will rangestat obtain the correlation for me? Thanks

      Comment


      • #4
        It is not entirely clear what you want to calculate, and how you want to save it.

        But it seems to me what what you want can be done with the user contributed -egenmore-, type
        Code:
        findit egenmore
        and follow instructions to install it, and then

        Code:
        egen thecorrelation = corr(var1 var2), by(loc)

        Comment


        • #5
          Hi Joro Kolev ,

          Is the command corr used to calculate the contemporaneous correlation between the two series?

          Code:
          egen thecorrelation = corr(var1 var2), by(loc)

          Comment


          • #6
            You can look at the code to see what it does. This can be done remotely as well. and the short answer is Yes.

            Code:
            . ssc type _gcorr.ado
            *! NJGW 09jun2005
            *! syntax:  [by varlist:] egen newvar = var1 var2 [if exp] [in exp] 
            *!           [ , covariance spearman taua taub ]
            *! computes correlation (or covariance, or spearman correlation) between var1 and var2, optiona
            > lly by: varlist
            *!    and stores the result in newvar.
            program define _gcorr
                    version 8
            
                    gettoken type 0 : 0
                    gettoken g    0 : 0
                    gettoken eqs  0 : 0
                    syntax varlist(min=2 max=2) [if] [in] [, BY(string) Covariance Spearman taua taub ]
            
                    if "`taua'`taub'`spearman'"!="" & "`covariance'"!="" {
                            di as error "`taua'`taub'`spearman' and covariance are mutually exclusive"
                            exit 198
                    }
            
                    local x : word count `taua' `taub' `spearman'
                    if `x'> 1 {
                            di as error "may only specify one of `taua' `taub' `spearman'"
                            exit 198
                    }
            
                    if `"`by'"'!="" {
                            local by `"by `by':"'
                    }
            
                    quietly { 
                            gen `type' `g' = .
                            `by' GenCorr `varlist' `if' `in', thevar(`g') `covariance' `spearman' `taua' `t
            > aub'
                    }
                    
                    if "`spearman'"!="" {
                            local lab "Spearman Correlation"
                    }
                    else if "`taua'"!="" {
                            local lab "Tau-A Correlation"
                    }
                    else if "`taub'"!="" {
                            local lab "Tau-B Correlation"
                    }
                    else if "`covariance'" != "" {
                            local lab "Covariance"
                    }
                    else {
                            local lab "Correlation"
                    }
                    
                    capture label var `g' "`lab' of `varlist'"
            end
            
            program define GenCorr, byable(recall)
                    syntax varlist [if] [in] , thevar(string) [ covariance spearman taua taub ]
                    marksample touse
                    if "`covariance'"!="" {
                            local stat "r(cov_12)"
                    }
                    else if "`taua'"!="" {
                            local stat "r(tau_a)"
                    }
                    else if "`taub'"!="" {
                            local stat "r(tau_b)"
                    }
                    else {                                  /* correlation and spearman */
                            local stat "r(rho)"
                    }
                    
                    if "`spearman'"!="" {           
                            local cmd spearman
                    }
                    else if "`taua'`taub'"!="" {
                            local cmd ktau
                    }
                    else {
                            local cmd corr                  /* correlation and covariance */
                    }
            
                    cap `cmd' `varlist' if `touse' , `covariance'
                    if !_rc {
                            qui replace `thevar'=``stat'' if `touse'
                    }
            end

            Comment


            • #7
              Okay. Thank you Nick!

              Comment

              Working...
              X