Announcement

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

  • Bar Graph with multiple dummy variables on x-Axis

    Hi,

    I am trying to create a bar graph in Stata, that describes a mean of one variable (v1) for several dummy variables (v2, v3, v4).
    I would like to have the dummy variables on the x-Axis and the mean of v1 for that variables o the y-Axis.
    I selected Graphics → Bar Charts → Main → Mean v1 but can't figure out, where to set the dummy variables, as I don't want to group data.

    Is there a possibility to that that?
    Thanks!

  • #2
    There is no data example here, contrary to our standing request at FAQ Advice #12.

    The graph you want is, if I understand correctly, the mean for each distinct value of each indicator (*) variable, as otherwise you would be plotting the same mean repeatedly.

    (*) Cox and Schechter in https://www.stata-journal.com/articl...article=dm0099 discuss terminology and encourage the term indicator. I've heard too many stories of the term dummy being horribly misunderstood as disparaging, and it's ugly any way. This is perhaps an uphill struggle against a term entrenched in some disciplines, but I will fight this good fight while I can.

    Again, if I understand correctly, this is covered by designplot from the Stata Journal, which you need to install before you can use it. Running a search as below will yield download links. I suggest reading the 2014 paper briskly, which is accessible directly at https://www.stata-journal.com/articl...article=gr0061 but downloading the program files from the latest update, at the time of writing gr0061_3. If you do this yourself, you will see a clickable link.

    Code:
    . search designplot, sj
    
    Search of official help files, FAQs, Examples, and Stata Journals
    
    SJ-19-3 gr0061_3  . . . . . . . . . . . . . . . Software update for designplot
            (help designplot if installed)  . . . . . . . . . . . . . .  N. J. Cox
            Q3/19   SJ 19(3):748--751
            any attempt to use the missing option of graph dot,
            graph hbar, or graph bar is now ignored and advice on
            what to do instead is shown
    
    SJ-17-3 gr0061_2  . . . . . . . . . . . . . . . Software update for designplot
            (help designplot if installed)  . . . . . . . . . . . . . .  N. J. Cox
            Q3/17   SJ 17(3):779
            help file updated
    
    SJ-15-2 gr0061_1  . . . . . . . . . . . . . . . Software update for designplot
            (help designplot if installed)  . . . . . . . . . . . . . .  N. J. Cox
            Q2/15   SJ 15(2):605--606
            bug fixed for Stata 14
    
    SJ-14-4 gr0061  Design plots for graphical summary of a response given factors
            (help designplot if installed)  . . . . . . . . . . . . . .  N. J. Cox
            Q4/14   SJ 14(4):975--990
            produces a graphical summary of a numeric response variable
            given one or more factors


    This code is for a reproducible example. If you wanted to jazz it up, say by changing some bar colours, then that is easiest in the Graph Editor. The overall bar colour can be controlled from designplot. In my own work I am making more use of a pale fill colour and a stronger outline color, exemplified here by say bar(1, lcolor(blue) fcolor(blue*0.2))


    Code:
    clear
    set obs 100
    set seed 2803
    gen v1 = exp(rnormal(0,1))
    gen v2 = runiform() > 1/4
    gen v3 = runiform() > 1/2
    gen v4 = runiform() > 3/4
    
    designplot v1 v2 v3 v4, min(1) max(1) recast(bar) variablelabels scheme(s1color)
    Click image for larger version

Name:	designplot.png
Views:	1
Size:	25.2 KB
ID:	1632787

    Last edited by Nick Cox; 21 Oct 2021, 17:48.

    Comment


    • #3
      Thank you, that is really helpful!

      Is it possible, to only display the "1" of v2-v3?
      I am working with pweights. In the helpfile I found, that weights can be applied to this code. However, when I run the code I get the error "pweights not allowed".

      Comment


      • #4
        See the help for designplot which explains that

        A. fweights and aweights are supported, which means that pweights aren't.

        B The calculations are done by summarize and linking through shows that summarize doesn't support pweights, which is the reason for A.

        Otherwise it seems that you want a graph with

        a. The mean of v1 for v2 = 1

        b. The mean of v1 for v3 = 1

        c. The mean of v1 for v4 = 0

        d. The mean of v1 for v4 = 1

        I think you are going to need to assemble a small dataset of results and then you can call graph bar directly. That is programmable, but you will need to write your own program or do-file to do it.

        If there were no pweights, this should work


        Code:
        gen y = . 
        gen x = _n in 1/4 
        
        su v1 if v2 == 1, meanonly 
        replace y =  r(mean) in 1 
        su v1 if v3 = 1, meanonly 
        replace y = r(mean) in 2 
        su v1 if v4 = 0, meanonly 
        replace y = r(mean) in 3 
        su v1 if v4 = 1, meanonly 
        replace y = r(mean) in 4 
        
        graph bar (asis) y, over(x)
        That is minimal. You still need to define value labels for the x axis, think up axis titles, and above all replace the summarize by a calculation using mean with pweights. If you wanted more space between some bars than others, that's extra code (not volunteering!). .


        The graph requested in post #1 as I understood it is a standard graph in my view, just not supported officially, which why I wrote designplot.

        The graph requested in post #3 no doubt makes sense from your perspective but it's a somewhat idiosyncratic graph requiring custom programming.

        Comment

        Working...
        X