Announcement

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

  • Dealing with periods and percentage numbers in order to draw a tsset time graph

    Hi Stata people;

    This is the data that i'am working on:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str9 period str4(rpretaxation g)
    "0-1000"    "4,5%" "0,0%"
    "1000-1500" "4,5%" "0,1%"
    "1500-1700" "4,5%" "0,2%"
    "1700-1820" "5,1%" "0,5%"
    "1820-1913" "5,0%" "1,5%"
    "1913-1950" "5,1%" "1,8%"
    "1950-2012" "5,3%" "3,8%"
    "2012-2050" "4,3%" "3,3%"
    "2050-2100" "4,3%" "1,5%"
    end
    As you can see, I have a time variable called "period" with a "-" between years that is stoping me from considering that variable as a time variable, so it needs some fixing.
    Also, the two variables "pretaxation" and "g" are percentage variables that are defined using the "%" symbol, which is making Stata treat those as string forms, yet I want those variables to be treated as numerican variable for my goal.

    Any help on how to fix all these issues?

    Thanks!

  • #2
    I doubt that tsset will help here, or is a good idea, as your intervals are of greatly different lengths.

    Otherwise the easy part of an answer is to push your percent variables through destring.

    It's fortuitous but fortunate that your string variable will sort as desired.

    I got this far:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str9 period str4(rpretaxation g)
    "0-1000"    "4,5%" "0,0%"
    "1000-1500" "4,5%" "0,1%"
    "1500-1700" "4,5%" "0,2%"
    "1700-1820" "5,1%" "0,5%"
    "1820-1913" "5,0%" "1,5%"
    "1913-1950" "5,1%" "1,8%"
    "1950-2012" "5,3%" "3,8%"
    "2012-2050" "4,3%" "3,3%"
    "2050-2100" "4,3%" "1,5%"
    end
    
    destring rpretaxation g, replace dpcomma ignore("%")
    
    graph dot (asis) rpretaxation, over(period, label(labsize(*0.7))) vertical exclude0 ///
    ytitle(Better title needed here)
    Click image for larger version

Name:	taxation.png
Views:	1
Size:	32.7 KB
ID:	1711598

    Comment


    • #3
      Nick Cox Thanks for the tip, it kinda helped me get to what I want.
      Yet, is it possible, in this context, to have a twoway line graph? Because I do think that way, the graph is going to be a bit more clean

      Comment


      • #4
        Surely, other graphs are possible. See twoway line in the help or manuals. Here's one:


        Code:
        * Example generated by -dataex-. To install: ssc install data
        clear
        input str9 period str4(rpretaxation g)
        "0-1000"    "4,5%" "0,0%"
        "1000-1500" "4,5%" "0,1%"
        "1500-1700" "4,5%" "0,2%"
        "1700-1820" "5,1%" "0,5%"
        "1820-1913" "5,0%" "1,5%"
        "1913-1950" "5,1%" "1,8%"
        "1950-2012" "5,3%" "3,8%"
        "2012-2050" "4,3%" "3,3%"
        "2050-2100" "4,3%" "1,5%"
        end
        
        destring r g , ignore("%") dpcomma replace 
        
        split period, p("-") destring 
        expand 2 in L 
        replace period1 = period2 in L 
        
        levelsof period1, local(levels)
        line r g period1 , lc(red blue) c(J J) xla(`levels', ang(v) labsize(small)) ///
        legend(off) text(4.8 800 "rpretaxation", color(red)) text(0.3 800 "g", color(blue)) ytitle("%", orient(horiz)) xtitle("") yla(, ang(h))
        Click image for larger version

Name:	taxation2.png
Views:	1
Size:	16.0 KB
ID:	1711718

        Comment


        • #5
          Nick Cox Thanks for the help

          Comment


          • #6
            A more unusual scale is reverse square root, as discussed at https://journals.sagepub.com/doi/pdf...867X1201200210

            Here I mylabels from the Stata Journal to get the axis labels: on that see https://journals.sagepub.com/doi/pdf...6867X221141058


            Code:
            * Example generated by -dataex-. To install: ssc install data
            clear
            input str9 period str4(rpretaxation g)
            "0-1000"    "4,5%" "0,0%"
            "1000-1500" "4,5%" "0,1%"
            "1500-1700" "4,5%" "0,2%"
            "1700-1820" "5,1%" "0,5%"
            "1820-1913" "5,0%" "1,5%"
            "1913-1950" "5,1%" "1,8%"
            "1950-2012" "5,3%" "3,8%"
            "2012-2050" "4,3%" "3,3%"
            "2050-2100" "4,3%" "1,5%"
            end
            
            destring r g , ignore("%") dpcomma replace 
            
            split period, p("-") destring 
            expand 2 in L 
            replace period1 = period2 in L 
            
            levelsof period1, local(levels)
            line r g period1 , lc(red blue) c(J J) xla(`levels', ang(v) labsize(small)) ///
            legend(off) text(4.8 800 "rpretaxation", color(red)) text(0.3 800 "g", color(blue)) ytitle("%", orient(horiz)) xtitle("") yla(, ang(h))
            
            gen xaxis = sqrt(2100 - period1)
            
            mylabels `levels', myscale(sqrt(2100 - @)) local(tlabels)
            
            local where = sqrt(1300)
            line r g xaxis, lc(red blue) c(J J) xla(`tlabels', ang(v) labsize(small)) ///
            legend(off) text(4.8 `where' "rpretaxation", color(red)) text(0.3 `where' "g", color(blue)) ytitle("%", orient(horiz)) xtitle("") yla(, ang(h)) xsc(reverse)
            Click image for larger version

Name:	taxation3.png
Views:	1
Size:	16.2 KB
ID:	1711783

            Comment

            Working...
            X