Announcement

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

  • Stata Runs .Do File without errors to plot graph but nothing happens

    I've run into a problem after working with a .do file and dataset to draw a series of graphs, prior iterations of the code (albeit different versions) drew and saved the graphs just fine. There isn't any error message or anything, it just won't save the graph or display it at all. Stata runs the .Do file and then displays "end of .do file" after it.

    Here's some example code at the end:

    * Loop over each province
    foreach primaryprovince of local primaryprovince {

    * Generate a line plot of sigacts vs modate for the current province
    twoway line sigacts modate if primaryprovince == "`primaryprovince'",
    title("Monthly SIGACTs in `primaryprovince', 2005-2014")
    xtitle("") ytitle("Total")
    legend(off)
    tlabel(2005m1(12)2014m12)
    xlabel(, angle(vertical))

    * Shade the july - January periods in each year
    || recast(area)
    line(, y(0) lpattern(shortdash))
    if primaryprovince == "`primaryprovince'" &
    month(modate) >= 7 &
    (month(modate) <= 12 | month(modate) == 1),
    color(gs12)

    * Add a July marker on the X axis for each year
    || scatteri
    7, ypos(0) mcolor(black) msize(large) mlab("July")
    if primaryprovince == "`primaryprovince'" &
    month(modate) == 7,

    * Add a demarcation line on the Prvoince "Kabul" for August 2003 labeled "NATO deployment"
    || line
    21.5, yscale(alt) lcolor(blue) lpattern(dash)
    text(, position(11 21.5) color(blue) size(large))
    if primaryprovince == "`primaryprovince'" &
    month(modate) == 8 &
    year(modate) == 2003,
    yline(, lpatten(shortdash))

    * Save the plot as an image
    graph export "sigacts_`primaryprovince'.png"
    }

    Can anyone help me figure out what is going wrong?

    Thanks!

  • #2
    Cross-posted and answered at https://stackoverflow.com/questions/...othing-happens

    Please note our policy about cross-posting, which is that you are asked to tell us about it. https://www.statalist.org/forums/help#crossposting

    Comment


    • #3
      Sorry, I was unaware of the policy. I tried compound double quotes on local primary province, local macro definition but it's still returning the same no error, no graph output.

      Comment


      • #4
        All members are asked to read the FAQ Advice before posting: that's mentioned on our home page and in the prompt for a new thread.

        Comment


        • #5
          The other main issue is that, like on Stack, you need a minimum reproducible example. That is, you need to provide us data with the dataex command such that it adequately reproduces the issue you have. Without a dataset to work with, along with the code to work on it, any diagnosis that might be given could just be useless. So, it's not that there's no solution, it's that we can't get to a useful solution if you don't provide enough information to answer the question.

          In other words, code is not enough. If show us your data, I imagine this'll be much easier.

          Comment


          • #6
            Sorry, I'm not sure how to do use the dataex command, how can I share my dataset?

            Comment


            • #7
              We have help files for this reason (h dataex), but, for a good visual example, this shows you how. All you really need to do is type in
              Code:
              dataex
              into your Stata terminal, though.

              But, recall what I said above: the example data isn't enough. The example data must be such that we can replicate the problem you're having. So, ensure that the example data reproduces the issue, which in this case seems like graphs won't generate. So, in other words, we need data and code to try to solve this.

              Comment


              • #8
                Now cross-posted at https://www.reddit.com/r/stata/comme...to_plot_graph/

                See also #2 once again.
                Last edited by Nick Cox; 02 May 2023, 11:58.

                Comment


                • #9
                  This is crossposted to reddit and stackoverflow but here is the code I'm currently working with now with dataex and appropriate example data.
                  https://pastebin.com/tsGFFhRe

                  Sorry I'm so difficult, I'm muddling through this.

                  Comment


                  • #10
                    Thank you for the data example. This is a minimal working example with your data. Note that your data example only contains data for Kabul for the month of April. I did not bother with trying to work on the embellishments of the graph as they are not remotely close to correct. This at least gives you the basic framework for the graph, and you can build on it from here.

                    Code:
                    * Loop over each province
                    foreach province of local primaryprovince {
                     
                        * Generate a line plot of sigacts vs modate for the current province
                        twoway (line sigacts modate if primaryprovince == "`province'", ///
                          title("Monthly SIGACTs in `province', 2005-2014") ///
                          xtitle("") ytitle("Total") ///
                          legend(off) ///
                          tlabel(2005m1(12)2014m12) ///
                          xlabel(, angle(vertical)) )
                          
                        * Save the plot as an image
                        *graph export "sigacts_`province'.png"
                    }

                    Comment


                    • #11
                      Leonardo Guizzetti got you started. This is the code that he ignored.

                      Code:
                      || recast(area) line(, y(0) lpattern(shortdash)) ///
                      if primaryprovince == "`primaryprovince'" & month(modate) >= 7 & (month(modate) <= 12 | month(modate) == 1), ///
                      color(gs12) ///
                      || scatteri 7, ypos(0) mcolor(black) msize(large) mlab("July") if primaryprovince == "`primaryprovince'" & month(modate) == 7,m ///
                      || line 21.5, yscale(alt) lcolor(blue) lpattern(dash) text(, position(11 21.5) color(blue) size(large)) ///
                      if primaryprovince == "`primaryprovince'" & month(modate) == 8 & year(modate) == 2003, ///
                      yline(, lpatten(shortdash))
                      Unfortunately he's right. Whoever, or whatever, wrote this is confused and/or guessing wildly.

                      month() and year() are functions for extracting year and month of year from daily dates. Here they are being applied to monthly dates, which won't work as was intended.

                      Setting aside the function call, what is intended by

                      Code:
                      month >= 7 & (month <= 12 | month == 1)
                      as it boils down to

                      Code:
                      month >= 7 & month <= 12
                      ?

                      The local macro calls should be to province to match Leonardo's scripts.

                      I will stop there as

                      Code:
                      recast(area)
                      the scatteri call

                      the line calls

                      are all essentially illegal and in any case I can't guess at what was intended.

                      It is probably better to get the graphs working as in Leonardo's code and then tell us what else you want.

                      Comment


                      • #12
                        Hi all, thanks for your help. The code was busted from the beginning and thank you for informing me on the actual function of
                        Code:
                        month()
                        and
                        Code:
                        year()
                        do. I ended up scrapping the code that was given to me as it's completely nonfunctional and then restarting from scratch. This is what I rewrote and it is, at least so far, functional.
                        https://pastebin.com/NRvQLGJr

                        Comment


                        • #13
                          Thanks for posting code, although it's immensely better to post it here -- as below -- and not expect people to go off to some other site. (Some copy and paste results from PasteBin to other places carry line numbers too, which need to be removed.)

                          Your code runs -- although necessarily with data only for Kabul, most graphs are blank -- but the option on the last command

                          Code:
                          xline(2005m7(12)2014m12, lcolor(gray*0.20))
                          fails, You can get a line every July with something like


                          Code:
                          xli(546(12)642)


                          Code:
                          * Example generated by -dataex-. For more info, type help dataex
                          * dataex primaryprovince type month year1
                          clear
                          input str20 primaryprovince str22 type float(month year1)
                          "Kabul" "" 4 2009
                          "Kabul" "ISAF" 4 2011
                          "Kabul" "ISAF" 4 2013
                          "Kabul" "INS" 4 2013
                          "Kabul" "INS" 4 2014
                          "Kabul" "" 4 2008
                          "Kabul" "Civilians" 4 2011
                          "Kabul" "" 4 2012
                          "Kabul" "Afghan Military" 4 2013
                          "Kabul" "Civilians" 4 2013
                          "Kabul" "INS" 4 2013
                          "Kabul" "" 4 2014
                          "Kabul" "INS" 4 2014
                          "Kabul" "Civilians" 4 2014
                          "Kabul" "" 4 2014
                          "Kabul" "" 4 2009
                          "Kabul" "INS" 4 2011
                          "Kabul" "" 4 2012
                          "Kabul" "INS" 4 2014
                          "Kabul" "INS" 4 2014
                          "Kabul" "" 4 2007
                          "Kabul" "ISAF" 4 2007
                          "Kabul" "AUP" 4 2009
                          "Kabul" "" 4 2009
                          "Kabul" "INS" 4 2014
                          "Kabul" "INS" 4 2014
                          "Kabul" "GIRoA" 4 2014
                          "Kabul" "" 4 2010
                          "Kabul" "Afghan Military" 4 2010
                          "Kabul" "Afghan Military" 4 2013
                          "Kabul" "" 4 2014
                          "Kabul" "INS" 4 2014
                          "Kabul" "INS" 4 2014
                          "Kabul" "Afghan Military" 4 2014
                          "Kabul" "Civilians" 4 2007
                          "Kabul" "ISAF" 4 2008
                          "Kabul" "ISAF" 4 2013
                          "Kabul" "Afghan Military" 4 2013
                          "Kabul" "ISAF" 4 2013
                          "Kabul" "" 4 2008
                          "Kabul" "Civilians" 4 2009
                          "Kabul" "" 4 2009
                          "Kabul" "Civilians" 4 2006
                          "Kabul" "ISAF" 4 2007
                          "Kabul" "" 4 2010
                          "Kabul" "ISAF" 4 2012
                          "Kabul" "Civilians" 4 2013
                          "Kabul" "" 4 2014
                          "Kabul" "" 4 2009
                          "Kabul" "ISAF" 4 2011
                          "Kabul" "Civilians" 4 2011
                          "Kabul" "AUP" 4 2009
                          "Kabul" "ISAF" 4 2009
                          "Kabul" "ISAF" 4 2010
                          "Kabul" "Afghan Military" 4 2009
                          "Kabul" "" 4 2009
                          "Kabul" "ISAF" 4 2010
                          "Kabul" "" 4 2011
                          "Kabul" "" 4 2009
                          "Kabul" "" 4 2009
                          "Kabul" "Infrastructure" 4 2010
                          "Kabul" "" 4 2011
                          "Kabul" "Afghan Military" 4 2014
                          "Kabul" "ISAF" 4 2014
                          "Kabul" "" 4 2007
                          "Kabul" "ISAF" 4 2008
                          "Kabul" "ISAF" 4 2012
                          "Kabul" "" 4 2013
                          "Kabul" "" 4 2008
                          "Kabul" "ISAF" 4 2008
                          "Kabul" "Civilians" 4 2011
                          "Kabul" "INS" 4 2012
                          "Kabul" "" 4 2013
                          "Kabul" "ISAF" 4 2013
                          "Kabul" "ISAF" 4 2011
                          "Kabul" "" 4 2012
                          "Kabul" "Civilians" 4 2012
                          "Kabul" "ISAF" 4 2012
                          "Kabul" "INS" 4 2012
                          "Kabul" "INS" 4 2012
                          "Kabul" "" 4 2013
                          "Kabul" "ABP" 4 2014
                          "Kabul" "GIRoA" 4 2014
                          "Kabul" "GIRoA" 4 2014
                          "Kabul" "" 4 2012
                          "Kabul" "" 4 2012
                          "Kabul" "Civilians" 4 2012
                          "Kabul" "Civilians" 4 2012
                          "Kabul" "ISAF" 4 2013
                          "Kabul" "ANCOP" 4 2014
                          "Kabul" "" 4 2008
                          "Kabul" "AUP" 4 2008
                          "Kabul" "AUP" 4 2011
                          "Kabul" "INS" 4 2011
                          "Kabul" "" 4 2012
                          "Kabul" "" 4 2007
                          "Kabul" "ISAF" 4 2009
                          "Kabul" "" 4 2009
                          "Kabul" "INS" 4 2011
                          "Kabul" "" 4 2014
                          end
                           
                          replace primaryprovince = subinstr(primaryprovince, "Federally Administer", "Federally Administered", .)
                          replace primaryprovince = subinstr(primaryprovince, "Jawzjan", "Jowzjan", .)
                          replace primaryprovince = subinstr(primaryprovince, "KKhost", "Khost", .)
                          replace primaryprovince = subinstr(primaryprovince, "SGhazni", "Ghazni", .)
                          replace primaryprovince = subinstr(primaryprovince, "S Ghazni", "Ghazni", .)
                          replace primaryprovince = subinstr(primaryprovince, "Sari Pul", "Sar-e Pul", .)
                          replace primaryprovince = subinstr(primaryprovince, "a Herat", "Hirat", .)
                          replace primaryprovince = subinstr(primaryprovince, "aPaktika", "Paktika", .)
                          replace primaryprovince = subinstr(primaryprovince, "dPaktika", "Paktika", .)
                          replace primaryprovince = subinstr(primaryprovince, "dParwan", "Parwan", .)
                          replace primaryprovince = subinstr(primaryprovince, "eKhost", "Khost", .)
                          replace primaryprovince = subinstr(primaryprovince, "gSamangan", "Samangan", .)
                          replace primaryprovince = subinstr(primaryprovince, "hKapisa", "Kapisa", .)
                          replace primaryprovince = subinstr(primaryprovince, "hLogar", "Logar", .)
                          replace primaryprovince = subinstr(primaryprovince, "hWardak", "Wardak", .)
                          replace primaryprovince = subinstr(primaryprovince, "iHelmand", "Helmand", .)
                          replace primaryprovince = subinstr(primaryprovince, "lBaghlan", "Baghlan", .)
                          replace primaryprovince = subinstr(primaryprovince, "lSamangan", "Samangan", .)
                          replace primaryprovince = subinstr(primaryprovince, "nBalkh", "Balkh", .)
                          replace primaryprovince = subinstr(primaryprovince, "nHerat", "Herat", .)
                          replace primaryprovince = subinstr(primaryprovince, "uHerat", "Herat", .)
                          replace primaryprovince = subinstr(primaryprovince, "Day Kundi", "Daykundi", .)
                          replace primaryprovince = subinstr(primaryprovince, "Hirat", "Herat", .)
                          replace primaryprovince = subinstr(primaryprovince, ") Herat", "Herat", .)
                          replace primaryprovince = subinstr(primaryprovince, "JGhazni", "Ghazni", .)
                          replace primaryprovince = subinstr(primaryprovince, "Paktya", "Paktiya", .)
                           
                           
                          * create year-month date
                          gen modate = ym(year1, month)
                          format modate %tm
                          format %tmMon_CCYY modate
                          gen type10 = 1
                          tempfile datax
                          save `datax'
                           
                          keep if type == "ISAF"
                          replace type10 = 2
                          la def type10 1 "Total" 2 "ISAF"
                          la val type10 type10
                          append using `datax'
                          ta type10
                           
                          * aggregate
                          contract primaryprovince modate type10, zero
                          rename _freq sigacts
                          label var sigacts "Significant Activities"
                          label var type10 "Type"
                          label var primaryprovince "Province"
                          label var modate "Time"
                          label var modate "Period"
                           
                          * remove missing
                          drop if primaryprovince==""
                          drop if modate==.
                           
                           
                          /* Define a list of provinces */
                          local provinces ""Badakhshan" "Badghis" "Baghlan" "Balkh" "Bamyan" "Daykundi" "Farah" "Faryab" "Federally Administered Tribal Areas" "Ghazni" "Ghor" "Helmand" "Herat" "Jowzjan" "Kabul" "Kandahar" "Kapisa" "Khost" "Khyber Pakhtunkhwa" "Kunar" "Kunduz" "Laghman" "Logar" "Nangarhar" "Nimroz" "Nuristan" "Paktika" "Paktiya" "Panjshir" "Parwan" "Samangan" "Sar-e Pul" "Takhar" "Uruzgan" "Wardak" "Zabul""
                           
                          /* Loop over each province */
                          foreach province of local provinces {
                           
                          /* Generate a line plot of sigacts vs modate for the current province and type10 */
                          twoway (line sigacts modate if type10==1 & primaryprovince == "`province'",lcolor(green)) (line sigacts modate if type10==2 & primaryprovince == "`province'",lcolor(red)), title("Monthly SIGACTs in `province', 2005-2014") xtitle("") ytitle("Totals") legend(label(1 "Total") label(2 "ISAF")) tlabel(2005m1(6)2014m12) xlabel(, angle(vertical))
                           
                          /* Save the plot as an image */
                          graph export "sigacts_`province'.png", replace
                          }
                           
                          twoway (line sigacts modate if type10==1 & primaryprovince == "Badakhshan",lcolor(green)) (line sigacts modate if type10==2 & primaryprovince == "Badakhshan",lcolor(red)), title("Monthly SIGACTs in Badakhshan, 2005-2014") xtitle("") ytitle("Totals") legend(label(1 "Total") label(2 "ISAF")) tlabel(2005m1(6)2014m12) xlabel(, angle(vertical)) xline(2005m7(12)2014m12, lcolor(gray*0.20))

                          Comment

                          Working...
                          X