Announcement

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

  • Use aux() to add markers with number of observations in coefplot

    Hello,

    I am using coefplot (with STATA 17) to plot coefficients from multiple models on the same plot and since the number of observations varies from model to model, I would like to attach markers that indicate the number of observations in each model.

    I know that coefplot maintains a number of internal variables that can be used with malabel, including @se and @pval. But the number of observations in the model is not one of these, so it would have to be accessed using aux().

    However, when I attempt to use aux() to access N and then use it as a marker label, I receive the message “e(N) not found”. I assume this must be because I have not correctly identified where exactly the scalar “N” is stored for each model and am not correctly calling it using aux(). Could you help me identify the correct aux() call to fetch the N for each model?

    I have an illustrative example using the auto.dta dataset below. The code for attaching the label comes from this example of labels at the end of confidence intervals (https://repec.sowi.unibe.ch/stata/co...ers.html#h-3-4). There is one example in the coefplot documentation of using aux and N to edit coefplot, in this case changing the marker size relative to the number of observations. (https://repec.sowi.unibe.ch/stata/co...rkers.html#h-4) I tried to follow the principle in this example, but I receive the message "e(N) not found" or "e(_N) not found".


    Code:
    estimates clear
    sysuse auto, clear
    eststo modelA: reg price mpg
    eststo modelB: reg price trunk
    eststo modelC: reg price length
    
    *check to see what is among the list of scalars
    estimates restore modelA
    ereturn list
    *e(N) appears among the list of scalars
    
    coefplot modelA modelB modelC, /*
    */ aux(N) /*
    */ mlabel(string(@aux1)) mlabcolor(none) /*
    */ addplot(scatter @at @ul, ms(i) mlabel(@mlbl) mlabcolor(black) mlabpos(3) mlabsize(vsmall))
    *If I try to access N using aux(N) I get the message "e(N) not found"
    
    coefplot modelA modelB modelC, /*
    */ aux(_N) /*
    */ mlabel(string(@aux1)) mlabcolor(none) /*
    */ addplot(scatter @at @ul, ms(i) mlabel(@mlbl) mlabcolor(black) mlabpos(3) mlabsize(vsmall))
    *If I try to access N using aux(_N) I get the message "e(_N) not found"
    
    
    coefplot modelA modelB modelC, /*
    */ mlabel(string(@pval)) mlabcolor(none) /*
    */ addplot(scatter @at @ul, ms(i) mlabel(@mlbl) mlabcolor(black) mlabpos(3) mlabsize(vsmall))
    *This, with the internal variable stored under @pval, works as intended
    Last edited by Alexandra Zeitz; 12 Sep 2023, 09:27.

  • #2
    coefplot is from SSC (FAQ Advice #12). Note that the reference is to a matrix and not a scalar. Below, I use estadd which is part of estout from SSC to post the matrix with the number of observations to e(). Consider:


    Code:
    sysuse auto, clear
    reg price mpg
    mat N1= e(N)*J(`=rowsof(e(b))', `=colsof(e(b))', 1)
    estadd mat N1= N1
    est sto modelA
    
    *check to see what is among the list of scalars
    estimates restore modelA
    ereturn list
    
    
    coefplot modelA , /*
    */ aux(N1) /*
    */ mlabel("{it: N}=" + string(@aux1)) mlabcolor(none) /*
    */ addplot(scatter @at @ul, ms(i) mlabel(@mlbl) mlabcolor(black) mlabpos(3) mlabsize(medsmall))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	8.1 KB
ID:	1726917

    Last edited by Andrew Musau; 12 Sep 2023, 13:29.

    Comment


    • #3
      This is very helpful, thank you!

      Comment

      Working...
      X