Announcement

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

  • Adding 2-Way Plot Options to User Written Commands

    Let's say there's a command we're using to do some causal effect estimation. We run the command, and afterwards, it produces a pretty graph that's meant to be illustrative and informative. Well if you're at all like me, you like to edit your graphs in detail. Sure, I could go into the ado file and make changes, but what if I want to exercise total control over how the graph looks, or at least, more control than the current command offers? Is it possible to add two-way plot options, to a graph option for an existing command? I have no idea, since I've never written a graphics command before, so PRESUMABLY Stata Corp has some standardized way to to this.

    Lets play with some real data, shall we?

    Code:
    import delim "https://raw.githubusercontent.com/danilofreire/homicides-sp-synth/master/data/df.csv", clear
    
    
    cap which synth
    
    if _rc { // installs the synth command if you don't have it
        
    ssc install synth, replace all
    
    
    *Abadie's synthetic control estimator
    
    
    }
    
    
    xtset code year, y // declares it panel data
    
    loc int_date = 1999
    
    rename proportionextremepoverty expov
    
    cls
    
    g treated = 1 if code == 35 & year >= 1999
    
    replace treated = 0 if treated == .
    
    
    cls
    
    synth homiciderates homiciderates stategdpcapita ///
        stategdpgrowthpercent ///
        populationextremepovertyimp ///
        yearsschoolingimp ///
        giniimp ///
        expov ///
        populationprojectionln, ///
            trunit(35) ///
            trperiod(1999) ///
            fig //
    Okay so far, we've gotten some data from a previously published paper and ran one of his specifications. We've also made a graph, displaying the usual stuff synth gives us in this instance. What if I wanted to add a title to this graph, though? What if I wanted to add the equivalent of synth ... fig(ti("This is a graph title")? Or what if I wanted to change the graph scheme to something different than what the authors intended like synth ... fig(scheme(yourscheme)? How might I edit this command for my own purposes, such that I don't always need to make a separate graph 10 lines below my estimation command because the graph that came with the command isn't good enough for me?

    I imagine I'll need to do some minor surgery on the ado file itself; how might I go about this? I know how to access the ado file (I use adoedit to change ado files on the fly), but what syntactic modifications would I need to make to have it do what I want? For context, the plot option is around line 656 in the ado file for synth, where the figure option is specified.

  • #2
    As you are using -synth- (from SSC), you may specify -keep()- option to save all variables you need for plotting to a dataset based on which you may re-plot a graph.

    Comment


    • #3
      Fei Wang Indeed, but I'm asking if I can modify the command to not need to do that step. The figure option already makes the basic graph, I was wondering if I could add suboptions to that option.

      I like the keep option, but it would be nice if I could do some basic modifications to the graph produced by the fig option without having to go into the dataset keep produces, so I was curious if there was a way to do that, likely by just... somehow adding the standard twoway options, to the figure option to synth.

      Comment


      • #4
        As far as I know, -synth- does not support graph adjustment by adding twoway options.

        Comment


        • #5
          Fei Wang

          synth does not support graph adjustment
          Precisely.

          I'm asking if you or anyone knows how I could modify the existing ado-file to synth so that it does support two-way options for the figure.

          Or, if it's even possible to do that. Perhaps.... Nick Cox might know, if he gets a minute to comment?

          Comment


          • #6
            In general, you can do this via syntax, where you might (minimally) support passthru options to the underlying geologic graphing command. This is a type of option supported by -syntax-. Inside the ado program, you would have a call to the graphing command with the various passthru options specified (or some sensible defaults in their absence).

            Comment


            • #7
              I loosely agree with Leonardo Guizzetti but hope the solution is simpler than he implies.

              Looking at the version of synth that is on SSC the main syntax call currently ends

              Code:
                                                   allopt ///
                                                     * ///
                                                     ]
              which I would edit to

              Code:
                                                   allopt ///
                                                   graphopts(string asis)
                                                     * ///
                                                     ]
              and then the main twoway call

              Code:
              twoway (line _Y_treated _time, lcolor(black)) (line _Y_synthetic _time, lpattern(dash) lcolor(black)), ytitle("`dvar'") xtitle("`tvar'") xline(`trperiod', lpattern(shortdash) lcolor(black))
              should be modified to

              Code:
              twoway (line _Y_treated _time, lcolor(black)) (line _Y_synthetic _time, lpattern(dash) lcolor(black)), ytitle("`dvar'") xtitle("`tvar'") xline(`trperiod', lpattern(shortdash) lcolor(black)) `graphopts'
              Hence for example your syntax might now include

              Code:
              graphopts(title("Some preferred title") lcolor(blue))
              The option name here graphopts() is at your discretion: what matters is passing any extra or different options to twoway as if in a bag.

              Warning: Not tested. See

              Code:
              help syntax
              for more.
              Last edited by Nick Cox; 22 Nov 2021, 01:32.

              Comment


              • #8
                I was about to suggest essentially the same approach as Nick in #7; a little more tailored to the suggested syntax near the end of #1:

                Code:
                [...]
                syntax [...] ,           ///
                    [...]                ///
                    FIGure               /// <- already present
                    FIGure2(string asis) /// <- new
                [...]
                graph twoway ... , `figure2'
                I would like to add some comments. First, adding `figure2' (or graphopts, as Nick suggested) after the options that are already present might well suffice because most graphic options can be repeated, and later options usually overwrite earlier options. However, this does not provide fine-tuning of the different line plots that make up the overall graph. Another, more general, drawback with modifying ado-files is that you might need to do so repeatedly in future updated versions of synth.ado. Therefore, I might prefer the approach suggested in #2, and write a wrapper around synth that produces the desired graph. Alternatively (or additionally), contact the author of synth and suggest adding fine-tuning options for the graphs as others mike benefit from those, too.

                Comment


                • #9
                  daniel klein makes some excellent points. He is bang on that whatever tweaks you make for private use have to be tweaked every time synth is updated. And @Fei Wang's strategy, although more work, is good long-term.

                  Comment

                  Working...
                  X