Announcement

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

  • Draw Multiple Charts Using Dataex Command

    Hello all,

    I have a panel data, with a timeline from (1985-2020) with 217 countries and I'm trying to draw a chart highlighting GDP growth rates "growth" over time "year" but only from 1986-1996, since I have some missing observations (after the count only 58 countries have the observations for the sought out timeline)

    The main idea here is highlighted in the picture below. For every country (if data are available) I want to draw the country's growth and a fitted line for it. (Germany's example below)

    I've added "countryid" column to assign a specific number for each country I have, I've looked into dataex help but didn't fully understand how to apply that in my case.

    I was advised to include these into my dataex command countryid, year, growth, and the country with their full names (I did find that odd since I'm already using the countryid):

    dataex if inrange(countryid 157, year 1986, 1996, growth 1986, 1996, Poland)

    Any suggestions are appreciated!
    Thanks

    Click image for larger version

Name:	Screenshot 2024-02-12 at 15.07.21.png
Views:	1
Size:	160.5 KB
ID:	1743222 Click image for larger version

Name:	Screenshot 2024-02-12 at 15.00.14.png
Views:	1
Size:	518.6 KB
ID:	1743223

  • #2
    Cross-posted at https://www.reddit.com/r/stata/comme...for_different/

    Please note that it's a rule on Reddit and a request here to tell people about cross-posting. For requests here, please back up and read the FAQ Advice all posters are asked to read.

    The dataex command you post is a very wild guess. See

    Code:
    help inrange()
    to find that the syntax supported is quite different. Something like

    Code:
    dataex countryid country year growth if inrange(year, 1986, 1996) 
    may be closer to what you and we need. That suggestion assumes that you have a variable country as well as countryid.

    This post and the thread on Reddit are likely to confuse many people. Do you want one graph for all countries or a graph for each country? That is the main issue in terms of code. But one answer seems easy: a graph with all 58 countries will just be a hopeless mess. The legend alone will take up far too much space.

    Comment


    • #3
      Nick Cox Okay noted!

      I did mention that I want every country in a single graph as you already stated all in one will be chaotic.

      Comment


      • #4
        "every country in a single graph" remains utterly ambiguous to me. #1 starts

        I'm trying to draw a chart highlighting GDP growth rates
        but then contradicts itself.

        Oh well. Let's have that data example please.

        Comment


        • #5
          I'm a STATA newbie, so apologies for any misunderstanding. If you look at Germany example provided above (slide picture). I want to do similar thing, meaning for every country I want to draw ITS OWN CHART with the growth and year on the x and y axis and a fitted line. Hence, I need 58 charts.

          By data example you mean the results of what came out from the dataex command or the data file I'm opening in STATA?

          Comment


          • #6
            I mean the results of the dataex command I gave in #2 -- or as said a correction of that if you have different variable name(s).

            I am puzzled in turn as you've been a member here since October and posted 30 times -- and presumably glanced at several threads. Examples of the use of dataex are not far to seek.

            Nevertheless you've, it seems, yet to read the FAQ Advice https://www.statalist.org/forums/help if you've not got as far as https://www.statalist.org/forums/help#spelling ....

            I am mostly in meetings from now until later, but anyone else is as always free to pick up the thread.

            Comment


            • #7
              Nick Cox I use STATA sporadically, maybe once or twice a year.

              Here's dataex results

              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input float countryid str30 country int year float growth
               1 "Afghanistan"         1986           .
               1 "Afghanistan"         1987           .
               1 "Afghanistan"         1988           .
               1 "Afghanistan"         1989           .
               1 "Afghanistan"         1990           .
               1 "Afghanistan"         1991           .
               1 "Afghanistan"         1992           .
               1 "Afghanistan"         1993           .
               1 "Afghanistan"         1994           .
               1 "Afghanistan"         1995           .
               1 "Afghanistan"         1996           .
               2 "Albania"             1986           .
               2 "Albania"             1987           .
               2 "Albania"             1988           .
               2 "Albania"             1989           .
               2 "Albania"             1990           .
               2 "Albania"             1991           .
               2 "Albania"             1992           .
               2 "Albania"             1993           .
               2 "Albania"             1994           .
               2 "Albania"             1995           .
               2 "Albania"             1996           .
               3 "Algeria"             1986           .
               3 "Algeria"             1987           .
               3 "Algeria"             1988           .
               3 "Algeria"             1989           .
               3 "Algeria"             1990           .
               3 "Algeria"             1991           .
               3 "Algeria"             1992           .
               3 "Algeria"             1993           .
               3 "Algeria"             1994           .
               3 "Algeria"             1995           .
               3 "Algeria"             1996           .
               4 "American Samoa"      1986           .
               4 "American Samoa"      1987           .
               4 "American Samoa"      1988           .
               4 "American Samoa"      1989           .
               4 "American Samoa"      1990           .
               4 "American Samoa"      1991           .
               4 "American Samoa"      1992           .
               4 "American Samoa"      1993           .
               4 "American Samoa"      1994           .
               4 "American Samoa"      1995           .
               4 "American Samoa"      1996           .
               5 "Andorra"             1986           .
               5 "Andorra"             1987           .
               5 "Andorra"             1988           .
               5 "Andorra"             1989           .
               5 "Andorra"             1990           .
               5 "Andorra"             1991           .
               5 "Andorra"             1992           .
               5 "Andorra"             1993           .
               5 "Andorra"             1994           .
               5 "Andorra"             1995           .
               5 "Andorra"             1996           .
               6 "Angola"              1986           .
               6 "Angola"              1987           .
               6 "Angola"              1988           .
               6 "Angola"              1989           .
               6 "Angola"              1990           .
               6 "Angola"              1991           .
               6 "Angola"              1992           .
               6 "Angola"              1993           .
               6 "Angola"              1994           .
               6 "Angola"              1995           .
               6 "Angola"              1996           .
               7 "Antigua and Barbuda" 1986           .
               7 "Antigua and Barbuda" 1987           .
               7 "Antigua and Barbuda" 1988           .
               7 "Antigua and Barbuda" 1989           .
               7 "Antigua and Barbuda" 1990           .
               7 "Antigua and Barbuda" 1991           .
               7 "Antigua and Barbuda" 1992           .
               7 "Antigua and Barbuda" 1993           .
               7 "Antigua and Barbuda" 1994           .
               7 "Antigua and Barbuda" 1995           .
               7 "Antigua and Barbuda" 1996           .
               8 "Argentina"           1986   .06153377
               8 "Argentina"           1987   .02704869
               8 "Argentina"           1988 -.010899333
               8 "Argentina"           1989 -.071569495
               8 "Argentina"           1990  -.02467214
               8 "Argentina"           1991    .0913311
               8 "Argentina"           1992   .07937291
               8 "Argentina"           1993    .0820698
               8 "Argentina"           1994   .05836201
               8 "Argentina"           1995 -.028452097
               8 "Argentina"           1996    .0552669
               9 "Armenia"             1986           .
               9 "Armenia"             1987           .
               9 "Armenia"             1988           .
               9 "Armenia"             1989           .
               9 "Armenia"             1990           .
               9 "Armenia"             1991           .
               9 "Armenia"             1992           .
               9 "Armenia"             1993           .
               9 "Armenia"             1994           .
               9 "Armenia"             1995           .
               9 "Armenia"             1996           .
              10 "Aruba"               1986           .
              end
              ------------------ copy up to and including the previous line ------------------

              Listed 100 out of 9420 observations
              Use the count() option to list more

              Once again, thank you for your time and assistance.

              Comment


              • #8
                This works with your example data. The only graph shown is for Argentina, but that's a side-effect of your rules.

                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input float countryid str30 country int year float growth
                 1 "Afghanistan"         1986           .
                 1 "Afghanistan"         1987           .
                 1 "Afghanistan"         1988           .
                 1 "Afghanistan"         1989           .
                 1 "Afghanistan"         1990           .
                 1 "Afghanistan"         1991           .
                 1 "Afghanistan"         1992           .
                 1 "Afghanistan"         1993           .
                 1 "Afghanistan"         1994           .
                 1 "Afghanistan"         1995           .
                 1 "Afghanistan"         1996           .
                 2 "Albania"             1986           .
                 2 "Albania"             1987           .
                 2 "Albania"             1988           .
                 2 "Albania"             1989           .
                 2 "Albania"             1990           .
                 2 "Albania"             1991           .
                 2 "Albania"             1992           .
                 2 "Albania"             1993           .
                 2 "Albania"             1994           .
                 2 "Albania"             1995           .
                 2 "Albania"             1996           .
                 3 "Algeria"             1986           .
                 3 "Algeria"             1987           .
                 3 "Algeria"             1988           .
                 3 "Algeria"             1989           .
                 3 "Algeria"             1990           .
                 3 "Algeria"             1991           .
                 3 "Algeria"             1992           .
                 3 "Algeria"             1993           .
                 3 "Algeria"             1994           .
                 3 "Algeria"             1995           .
                 3 "Algeria"             1996           .
                 4 "American Samoa"      1986           .
                 4 "American Samoa"      1987           .
                 4 "American Samoa"      1988           .
                 4 "American Samoa"      1989           .
                 4 "American Samoa"      1990           .
                 4 "American Samoa"      1991           .
                 4 "American Samoa"      1992           .
                 4 "American Samoa"      1993           .
                 4 "American Samoa"      1994           .
                 4 "American Samoa"      1995           .
                 4 "American Samoa"      1996           .
                 5 "Andorra"             1986           .
                 5 "Andorra"             1987           .
                 5 "Andorra"             1988           .
                 5 "Andorra"             1989           .
                 5 "Andorra"             1990           .
                 5 "Andorra"             1991           .
                 5 "Andorra"             1992           .
                 5 "Andorra"             1993           .
                 5 "Andorra"             1994           .
                 5 "Andorra"             1995           .
                 5 "Andorra"             1996           .
                 6 "Angola"              1986           .
                 6 "Angola"              1987           .
                 6 "Angola"              1988           .
                 6 "Angola"              1989           .
                 6 "Angola"              1990           .
                 6 "Angola"              1991           .
                 6 "Angola"              1992           .
                 6 "Angola"              1993           .
                 6 "Angola"              1994           .
                 6 "Angola"              1995           .
                 6 "Angola"              1996           .
                 7 "Antigua and Barbuda" 1986           .
                 7 "Antigua and Barbuda" 1987           .
                 7 "Antigua and Barbuda" 1988           .
                 7 "Antigua and Barbuda" 1989           .
                 7 "Antigua and Barbuda" 1990           .
                 7 "Antigua and Barbuda" 1991           .
                 7 "Antigua and Barbuda" 1992           .
                 7 "Antigua and Barbuda" 1993           .
                 7 "Antigua and Barbuda" 1994           .
                 7 "Antigua and Barbuda" 1995           .
                 7 "Antigua and Barbuda" 1996           .
                 8 "Argentina"           1986   .06153377
                 8 "Argentina"           1987   .02704869
                 8 "Argentina"           1988 -.010899333
                 8 "Argentina"           1989 -.071569495
                 8 "Argentina"           1990  -.02467214
                 8 "Argentina"           1991    .0913311
                 8 "Argentina"           1992   .07937291
                 8 "Argentina"           1993    .0820698
                 8 "Argentina"           1994   .05836201
                 8 "Argentina"           1995 -.028452097
                 8 "Argentina"           1996    .0552669
                 9 "Armenia"             1986           .
                 9 "Armenia"             1987           .
                 9 "Armenia"             1988           .
                 9 "Armenia"             1989           .
                 9 "Armenia"             1990           .
                 9 "Armenia"             1991           .
                 9 "Armenia"             1992           .
                 9 "Armenia"             1993           .
                 9 "Armenia"             1994           .
                 9 "Armenia"             1995           .
                 9 "Armenia"             1996           .
                10 "Aruba"               1986           .
                end
                
                gen long obsno = _n 
                
                su countryid, meanonly 
                
                local max = r(max)
                
                * loop over numeric identifiers 
                forval c = 1/`max' { 
                    
                    * look up the country name 
                    su obsno if countryid == `c', meanonly 
                    local which = country[r(min)]
                    
                    * do we have enough data? 
                    qui count if countryid == `c' & !missing(growth) & inrange(year, 1986, 1996)
                    
                    if r(N) == 11 { 
                        scatter growth year if countryid == `c' & inrange(year, 1986, 1996) ///
                        || lfit growth year if countryid == `c' & inrange(year, 1986, 1996), ///
                        ytitle(growth) xtitle("") subtitle("`which'", place(w)) name(G`c', replace) legend(order(2) pos(12)) 
                        
                        graph export G`c'.png, replace 
                    }
                    else {
                        di _n "`c' `which': not enough data"
                    }
                }

                P.S. The last link in #6 is still on your reading list.

                Comment


                • #9
                  Nick Cox Apologies for the late response. I can't thank you enough. As highlighted previously, I don't have data for all countries. Thank you ever so much!


                  P.S. The last link in #6 is still on your reading list. - As it stands I'm reading all provided links.
                  Last edited by Anass Wajib; 17 Feb 2024, 05:43.

                  Comment

                  Working...
                  X