Announcement

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

  • Pairwise linear regression

    Dear Statalisters,

    pwcorr is an excellent command for getting pairwise pearsons correlation coefficients across a number of different variables in an 'all against all' manner. It has the added advantage that the matrix it produces can easily then produce a heatplot, if you are a believer in this method of data presentation.
    Is there an equivalent bit of code that I have been unable to find that will calculate simple univariate linear regressions between all the different variables (again in an all against all manner). I note, that unlike correlation coefficients, regressions are directional a ~ b is not the same as b ~a, and therefore one can envisage the top right of the output matrix being b ~a and the bottom left of the matrix a ~ b.

    Many thanks for your thoughts
    Robert Shaw

  • #2
    I am not aware of any program that would do this. But it is not particularly hard to code. I illustrate the approach with the auto.dta:
    Code:
    clear*
    sysuse auto
    
    local vbles price mpg headroom trunk
    
    frame create results str16(depvar indvar) float coef
    
    foreach v of local vbles {
        foreach w of local vbles {
            regress `v' `w'
            frame post results ("`v'") ("`w'") (_b["`w'"])
        }
    }
    
    frame change results
    format coef %3.2f
    reshape wide coef, i(depvar) j(indvar) string
    rename coef* *
    mkmat headroom-trunk, matrix(M) rownames(depvar)
    matrix list M

    Comment


    • #3
      Dear Clyde,
      Thank you so much for this. I think a mixture of laziness and lack of confidence in creating and manipulating matrices led me to not having tried this yet and hoping that someone had taken the time to make an off the shelf solution. Nevertheless your answer is both very helpful and instructive. It has introduced the concept of frames to me, which is very helpful. Thank you
      Kind regards
      Robert Shaw
      Last edited by Robert Shaw; 28 Feb 2024, 14:45.

      Comment

      Working...
      X