Announcement

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

  • Difference between variables' foreach loop regression coefficients

    Dear STATA users,

    I am using a foreach loop to regress four models across multiple variables. I need to test whether there are is a statistically significant difference between the intercept and coefficients for each possible pair of variables (e.g. whether the VWESTOT30 coefficients are different from the VWESTOB30 coefficients). I am not interested in the difference between intercepts or coefficients within the models (e.g. the difference between MktRF and SMB coefficients). Please see the example of the code I am using below.

    foreach portfolio of varlist VWESTOT30 VWESTOB30 VWESAFT30 VWESAFB30 VWESAST30 VWESASB30 VWESEAT30 VWESEAB30 VWESMIT30 VWESMIB30 VWESNOT30 VWESNOB30 VWESPAT30 VWESPAB30 VWESSOT30 VWESSOB30 VWESWET30 VWESWEB30 {
    //Regress CAPM
    regress `portfolio' MktRF, robust
    outreg2 `portfolio' MktRF using "I:\Outregs\ESCAPMVWTB30.xls", append
    //Regress FF 3F Model
    regress `portfolio' MktRF SMB HML, robust
    outreg2 `portfolio' MktRF SMB HML using "I:\Outregs\ESFF3FVWTB30.xls", append
    //Regress FF C Model
    regress `portfolio' MktRF SMB HML MOM, robust
    outreg2 `portfolio' MktRF SMB HML MOM using "I:\Outregs\ESCFFVWTB30.xls", append
    //Regress FF 5F Model
    regress `portfolio' MktRF SMB HML RMW CMA, robust
    outreg2 `portfolio' MktRF SMB HML RMW CMA using "I:\Outregs\ESFF5FVWTB30.xls", append
    }


    Thank you very much in advance!

  • #2
    So you need to loop over pairs of portfolios, avoiding doing any portfolio with itself or the same two in opposite orders. I think you need to use a postfile (or in version 16 a new frame) to capture the results. -suest- followed by -test- will do the actual comparisons. Here's some untested code that has the gist of it.

    Code:
    local portfolios VWESTOT30 VWESTOB30 VWESAFT30 VWESAFB30 VWESAST30 ///
        VWESASB30 VWESEAT30 VWESEAB30 VWESMIT30 VWESMIB30 VWESNOT30 ///
        VWESNOB30 VWESPAT30 VWESPAB30 VWESSOT30 VWESSOB30 VWESWET30 VWESWEB30
        
    local n_portfolios: word count `portfolios'
    
    local model1 MktRF
    local model2 `model1' SMB HML
    local model3 `model2' MOM
    local model4 `model2' RMW CMA
    
    capture postutil clear
    tempfile results
    postfile handle str32 (portfolio1 portfolio2) int model_num float(chi2 p) ///
        using `results'
    
    forvalues i = 1/`n_portfolios' {
        forvalues j = `=`i'+1'/`n_portfolios' {
            forvalues m = 1/4 {
                regress `:word `i' of `portfolios'' `model`m', robust
                est sto one
                regress `:word `j' of `portfolios'' `model`m', robust
                est sto two
                suest one two
                test [one = two]:`model`m''
                post handle ("`:word `i' of `portfolios''") ///
                    ("`:word `j' of `portfolios''") ///
                    (`m') (`r(chi2)') (`r(p)')
            }
        }
    }
    postclose handle
    
    use `results', clear
    That said, comparing the coefficients of the same model applied to two different outcome variables is usually a fool's errand. Unless the outcome variables are measured in the same units (which I guess yours are) and have the same frequency distributions (which I imagine yours do not) the results of such comparisons are uninterpretable. You are proposing to do 612 such comparisons.

    Comment


    • #3
      Hi Clyde, thank you very much! For both the code and the tip on comparing coefficients. I will standardize the variables to make them comparable to solve this issue (something I probably wouldn't have thought to do without your tip, so much appreciated!).

      Comment


      • #4
        I tried using the code and with a couple of changes it worked, thank you! However, currently it jointly tests whether all coefficients are equal. I only need to know whether the _cons coefficient is equal to the respective _cons coefficient and the same for the MktRF variable. This is what i have so far but I cannot figure out how to only test the differences between these two coefficients (separately not jointly). The differences between the other coefficients and the joint differences are not important. I have included the code that I know works to jointly test all coefficients, as well as what i think is my best guess at the code I am aiming for commented out.

        Code:
        local portfolios ZVWESTOT30 ZVWESTOB30 ZVWESAFT30 ZVWESAFB30 ZVWESAST30 ///
            ZVWESASB30 ZVWESEAT30 ZVWESEAB30 ZVWESMIT30 ZVWESMIB30 ZVWESNOT30 ///
            ZVWESNOB30 ZVWESPAT30 ZVWESPAB30 ZVWESSOT30 ZVWESSOB30 ZVWESWET30 ZVWESWEB30
            
        local n_portfolios: word count `portfolios'
        
        local model1 MktRF
        local model2 `model1' SMB HML
        local model3 `model2' MOM
        local model4 `model2' RMW CMA
        
        capture postutil clear
        tempfile results
        postfile handle str32 (portfolio1 portfolio2) int model_num float(chi2 p) ///
            using `results'
        
        forvalues i = 1/`n_portfolios' {
            forvalues j = `=`i'+1'/`n_portfolios' {
                forvalues m = 1/4 {
                    regress `:word `i' of `portfolios'' `model`m''
                    est sto one
                    regress `:word `j' of `portfolios'' `model`m''
                    est sto two
                    suest one two
                    test[one_mean = two_mean]:`model`m''
                    //test _b[one_mean:_cons]:`model`m'' = _b[two_mean:_cons]:`model`m''
                    post handle ("`:word `i' of `portfolios''") ///
                        ("`:word `j' of `portfolios''") ///
                        (`m') (`r(chi2)') (`r(p)')
                }
            }
        }
        set more off
        postclose handle
        use `results', clear
        Again, thank you in advance!

        Comment


        • #5
          It is unclear to me whether you wish to do a joint test of the _cons and the MktRF coefficients, or separate tests of each.

          If the former:
          Code:
          test [one_mean = two_mean]:_cons MktRF
          If the latter, then somewhat more extensive changes to the code are needed:
          Code:
          local portfolios ZVWESTOT30 ZVWESTOB30 ZVWESAFT30 ZVWESAFB30 ZVWESAST30 ///
              ZVWESASB30 ZVWESEAT30 ZVWESEAB30 ZVWESMIT30 ZVWESMIB30 ZVWESNOT30 ///
              ZVWESNOB30 ZVWESPAT30 ZVWESPAB30 ZVWESSOT30 ZVWESSOB30 ZVWESWET30 ZVWESWEB30
              
          local n_portfolios: word count `portfolios'
          
          local model1 MktRF
          local model2 `model1' SMB HML
          local model3 `model2' MOM
          local model4 `model2' RMW CMA
          
          capture postutil clear
          tempfile results
          postfile handle str32 (portfolio1 portfolio2) int model_num float(chi2_cons p_cons) ///
              float(chi2_MktRF p_Mkrtf) using `results'
          
          forvalues i = 1/`n_portfolios' {
              forvalues j = `=`i'+1'/`n_portfolios' {
                  forvalues m = 1/4 {
                      regress `:word `i' of `portfolios'' `model`m''
                      est sto one
                      regress `:word `j' of `portfolios'' `model`m''
                      est sto two
                      suest one two
                      test [one_mean = two_mean]:_cons
                      local chi2_cons = r(chi2)
                      local p_cons = r(p)
                      test [one_mean = two_mean]: MktRF
                      local chi2_MktRF = r(chi2)
                      local p_MktRF = r(p)
                      post handle ("`:word `i' of `portfolios''") ///
                          ("`:word `j' of `portfolios''") ///
                          (`m') (`chi2_cons)') (`p_cons') (`chi2_MktRF') (`p_MktRF')
                  }
              }
          }
          set more off
          postclose handle
          use `results', clear
          Last edited by Clyde Schechter; 26 Jul 2019, 10:39.

          Comment


          • #6
            Thanks Clyde, it worked well. Greatly appreciated! I meant the latter, so thank you for the more extensive edits to the code.

            Comment

            Working...
            X