Announcement

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

  • Coefplot, combine multiple plots and rename models issue

    Dear Statalist,

    I am using coefplot to plot regression coefficients from four different models. On the Y axis, I want to reference a value below Y-axis value (for example the mean of a given variable).

    Here is a reproducible example:
    HTML Code:
    #delimit;
    sysuse auto, clear;
    reg price mpg;
    est store Y1;
    summ price;
    loc estm1 = string(round(r(mean),.01));
    reg weight mpg;
    est store Y2;
    summ weight;
    loc estm2 = string(round(r(mean),.01));
    
    coefplot
    (Y1, rename(mpg = "price {sub:[`estm1']}") mcolor(navy) ciopts(color(navy)))
    (Y2, rename(mpg = "weight {sub:[`estm1']}") mcolor(navy) ciopts(color(navy)))
    || 
    (Y1, rename(mpg = "price {sub:[`estm1']}") mcolor(navy) ciopts(color(navy)))
    (Y2, rename(mpg = "weight {sub:[`estm1']}") mcolor(navy) ciopts(color(navy)))
    || 
    (Y1, rename(mpg = "price {sub:[`estm2']}") mcolor(navy) ciopts(color(navy)))
    (Y2, rename(mpg = "weight {sub:[`estm2']}") mcolor(navy) ciopts(color(navy)))
    || 
    (Y1, rename(mpg = "price {sub:[`estm2']}") mcolor(navy) ciopts(color(navy)))
    (Y2, rename(mpg = "weight {sub:[`estm2']}") mcolor(navy) ciopts(color(navy)))
    , keep(mpg)
    coeflabel(
        price  = "Price"
        weight = "Weight"
    )
    offset(0)
    legend(off);

    How can I show only for the top (and bottom) panel "price" and "weight" corresponding to the point estimates? I want to avoid having price and weight when not necessary.

    Click image for larger version

Name:	Capture d’écran 2022-04-28 à 10.31.36.png
Views:	1
Size:	298.5 KB
ID:	1661974

  • #2
    A feature of coefplot is that the categorical axis is the same in all subgraphs. This cannot be changed. If you want different categorical axes then you need to generate separate graphs and combine them using graph combine. About as follows:

    Code:
    #delimit;
    sysuse auto, clear;
    reg price mpg;
    est store price;
    summ price;
    loc estm1 = string(round(r(mean),.01));
    reg weight mpg;
    est store weight;
    summ weight;
    loc estm2 = string(round(r(mean),.01));
    coefplot (price weight) || (price weight) ||, keep(mpg) aseq swap
        coeflabel(
            price  = "price {sub:[`estm1']}"
            weight = "weight {sub:[`estm1']}"
        )
        name(g1, replace) nodraw;
    coefplot (price weight) || (price weight) ||, keep(mpg) aseq swap
        coeflabel(
            price  = "price {sub:[`estm2']}"
            weight = "weight {sub:[`estm2']}"
        )
        name(g2, replace) nodraw;
    graph combine g1 g2, cols(1);
    I use a somewhat different approach than you to merge the results from the different models; see http://repec.sowi.unibe.ch/stata/coe...varia.html#h-5 for explanations on the use of aseq and swap when merging results.

    The graph created by graph combine looks somewhat different than your graph because the first row now also has an x-axis; I believe it is not possible to have graph combine only plot the x-axis for the last row.

    So here's another approach. You would use command addplot (type ssc install addplot) to manipulate the labels in the graph after it has been created by coefplot. Example (assuming that locals estm1 and estm2 are still in memory):

    Code:
    coefplot (price weight) || (price weight) || (price weight) || (price weight)
        ||, keep(mpg) aseq swap;
    addplot 1:, ylabel(1 "price {sub:[`estm1']}" 2 "weight {sub:[`estm1']}") norescaling;
    addplot 3:, ylabel(1 "price {sub:[`estm2']}" 2 "weight {sub:[`estm2']}") norescaling;
    ben

    Comment

    Working...
    X