Announcement

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

  • transition matrices in bar charts

    Hello,

    I want to convert my transition matrix (see data/code below) into a "transition bar chart" as mentioned in these slides: https://www.stata.com/meeting/boston...14_nichols.pdf

    That is, I want to plot the output of my tabulate command. There would be one bar each for the three values of lag_x (value of x in 2018) as shown in the screenshot attached which I made in excel. The composition of each bar would be according to the rows in the tabulate output: i.e. the values of x in 2019.I want to make many such figures and am looking for a way to do this in Stata.

    Any help would be greatly appreciated!

    Thanks.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id year) str1 lag_x str32 x
    167 2018 ""  "C"
    167 2019 "C" "C"
    168 2018 ""  "C"
    168 2019 "C" "C"
    169 2018 ""  "C"
    169 2019 "C" "B"
    170 2018 ""  "B"
    170 2019 "B" "B"
    171 2018 ""  "A"
    171 2019 "A" "C"
    172 2018 ""  "B"
    172 2019 "B" "C"
    173 2018 ""  "C"
    173 2019 "C" "C"
    174 2018 ""  "A"
    174 2019 "A" "C"
    188 2018 ""  "A"
    188 2019 "A" "A"
    189 2018 ""  "C"
    189 2019 "C" "C"
    246 2018 ""  "C"
    246 2019 "C" "C"
    247 2018 ""  "C"
    247 2019 "C" "C"
    248 2018 ""  "C"
    248 2019 "C" "C"
    end
    
    qui tsset id year
    
    * transition matrix
    tab lag_x x, row nofreq
    Attached Files

  • #2
    Thanks for the data example.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id year) str1 lag_x str32 x
    167 2018 ""  "C"
    167 2019 "C" "C"
    168 2018 ""  "C"
    168 2019 "C" "C"
    169 2018 ""  "C"
    169 2019 "C" "B"
    170 2018 ""  "B"
    170 2019 "B" "B"
    171 2018 ""  "A"
    171 2019 "A" "C"
    172 2018 ""  "B"
    172 2019 "B" "C"
    173 2018 ""  "C"
    173 2019 "C" "C"
    174 2018 ""  "A"
    174 2019 "A" "C"
    188 2018 ""  "A"
    188 2019 "A" "A"
    189 2018 ""  "C"
    189 2019 "C" "C"
    246 2018 ""  "C"
    246 2019 "C" "C"
    247 2018 ""  "C"
    247 2019 "C" "C"
    248 2018 ""  "C"
    248 2019 "C" "C"
    end
    qui tsset id year
    * transition matrix
    tab lag_x x, row nofreq matcell(T)
    clear
    svmat T
    gen matid=_n
    reshape long T, i(matid) j(which)
    bys matid: egen percent= total(T)
    replace percent= (T/percent)*100
    bys matid (which): gen A= cond(which==1, percent, 0)
    bys matid (which): gen B= cond(which==2, percent+percent[_n-1], 0)
    bys matid (which): gen C= cond(which==3, percent+B[_n-1], 0)
    tw (bar C matid, barw(0.7) bcolor(gray))  (bar B matid, barw(0.7) ///
    bcolor(red)) (bar A matid, barw(0.7) bcolor(blue) scheme(s1color) ///
    leg(order(3 2 1) row(1)) xlab(1/3) xtitle("2018 Status (lag X)") ///
    ytitle("Percent"))
    Res.:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	25.9 KB
ID:	1572207


    Last edited by Andrew Musau; 10 Sep 2020, 10:10.

    Comment


    • #3
      Thank you! That works great

      Comment


      • #4
        Here is another way to do it:


        Code:
        tabplot lag_x x, fraction(lag_x) showval(format(%4.3f)) frame(1)
        where tabplot is from SSC (latest version also in press at the Stata Journal).
        Click image for larger version

Name:	transition.png
Views:	1
Size:	18.1 KB
ID:	1572424

        Comment


        • #5
          Naturally, flip the axes if preferred. I don't know which convention predominates but the arrow of time implies that later is the result of earlier and the earlier values should by that logic go on the horizontal axis. So, my seconf thoughts are to flip the axes!

          A hybrid graph and table is either a stylistic abomination or the best of both worlds.
          Last edited by Nick Cox; 12 Sep 2020, 04:58.

          Comment

          Working...
          X