Announcement

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

  • How to treat models as coefficient and coefficients as models with coefplot?

    I am estimating several models, storing the results with estout (from ssc) and using coefplot (from ssc, by Ben Jann ) to plot the results. I would like to invert the way that coefplot treats models and coefficients. That is I would like each y-axis point to be a different model, with coefficients for different variables shown for each model, and a legend that shows which marker corresponds to which coefficient (the variables are common across models). How can I do this? I don't have to use coefplot, but I have many models and different plots I want to produce for different subsets of coefficients, so I am after solutions that don't involve manually relabeling everything. Suggestions welcome. Here is an illustrative example:

    Code:
    ssc install coefplot
    ssc install eststo
    sysuse auto
    estimates clear
    foreach var in price mpg {
        eststo: reg `var' trunk weight foreign
    }
    coefplot *, keep(trunk weight)  // incorrect, y-axis points are models, different markers for different coefficients
    coefplot *, keep(trunk weight) swapnames asequation // also incorrect, stacks both models & variables, instead of multiple models for each variable

  • #2
    Thanks for a reproducible example. Versions of this question pop up from time to time. Here is some technique:

    Code:
    sysuse auto
    estimates clear
    foreach var in price mpg {
        eststo `var': reg `var' trunk weight foreign
    }
    
    
    coefplot (price\ mpg, keep(trunk) label(`:var lab trunk'))  ///
    (price\ mpg, keep(weight) label(`:var lab weight')), ///
    aseq swapnames
    Res.:

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	27.5 KB
ID:	1784833

    Comment


    • #3
      You need to create a separate "plot" for each predictor, about as follows:

      Code:
      sysuse auto
      estimates clear
      foreach var in price weight {
          eststo `var': reg `var' trunk mpg foreign
      }
      coefplot (*, keep(trunk) label(Effect of trunk)) ///
               (*, keep(mpg) label(Effect of mpg)) ///
               , swap aseq
      (Note that I use swaped the roles of weight and mpg compared to your example; I did this so that all displayed coefficients are on a similar scale and it is easier to spot if anything went wrong.)

      Same example with a liitle more automation:

      Code:
      sysuse auto
      estimates clear
      local xvars trunk mpg
      foreach var in price weight {
          eststo `var': reg `var' `xvars' foreign
      }
      local plots
      foreach var of local xvars {
          local plots `plots' (*, keep(`var') label(Effect of `var'))
      }
      coefplot `plots', swap aseq

      Comment


      • #4
        Sorry, didn't see that Andrew already answered.

        Comment


        • #5
          Thanks both for your answers, very helpful. Here's hoping this version of the question/solution will be the definitive one!

          Comment

          Working...
          X