Announcement

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

  • Changing order of rows in tables made with tabout

    Hello Statalisters,

    I am using the Ian Watson's tabout command which I got from SSC (ssc inst tabout). I would like to know whether tabout offers the opportunity to change the order of the rows. To state an example (the code requires that bot.tex and top.tex are set up in the working directory):

    Code:
    ssc inst tabout
    sysuse auto, clear
    tabout rep78 using table12.txt, ///
    c(mean mpg) f(1c) clab(MPG) sum npos(tufte) ///
    rep ///
    style(tex) bt ///
    topf(top.tex) botf(bot.tex) topstr(4cm) botstr(auto.dta)
    Here I would like to sort by the mean of the variable MPG per group so that group 5 would be in the first row, group 4 in the second and so forth. Is there anyone out there who could explain to me how to do this?

    Best regards,
    Roberto
    Last edited by Roberto Liebscher; 29 May 2014, 15:45.

  • #2
    Roberto,

    Only a few Stata programs support reversing the order of categorical variables. I don't know if tabout includes such a feature, but I doubt it. In general, the way to deal with this is to create a new variable:

    Code:
    gen revrep78=6-rep78
    tabout revrep78 ...
    You can also use the recode statement if you want more control:

    Code:
    recode rep78 (5=1) (4=2) (3=3) (2=4) (1=5), gen(revrep78)
    Regards,
    Joe

    Comment


    • #3
      Thanks Joe for your suggestion. I presume that tabout works the way tabstat does. So my problem may be discussed in tabstat terms. Following your suggestion my initial table:
      Code:
      sysuse auto, clear
      tabstat mpg, stats(mean) by(rep78)
      would change to
      Code:
      recode rep78 (5=1) (4=2) (1=3) (3=4) (2=5), gen(revrep78)
      tabstat mpg, stats(mean) by(revrep78)
      This would work insofar that the mean values of mpg appear in descending order. However, instead of having the wrong categories 1, 2, 3, 4 and 5 besides the mean values I would like to see something like:
      5 27.363
      4 21.667
      1 21.000
      3 19.433
      2 19.125
      Any help is highly appreciated.

      Thanks,
      Roberto

      Comment


      • #4
        Roberto,

        Thanks for clarifying; I think I understand better what you are trying to do. I'm not sure if what you want to do is possible (without tweaking an ado-file), but before we go down too many rabbit trails, are you trying to sort the table in descending order of the mean, or is that just a coincidence? Whatever the solution, it is important to know whether you want a random order (like 5,4,1,3,2) or whether you want the ordering to be done in a systematic way.

        Regards,
        Joe

        UPDATE: Never mind, just found the answer in your post.

        Comment


        • #5
          Roberto,

          I don't see much hope of tweaking tabstat or tabout any time soon. tabstat does things in a way that is not amenable to sorting by the mean, and tabout is very complex. However, others may have ideas or other programs that they are aware of that might do this. You might also consider contacting the tabout author.

          In the meantime, here is another possibility that doesn't have the bells and whistles of tabstat/tabout:

          Code:
          statsby meanmpg=r(mean), by(rep78): summarize mpg
          sort meanmpg
          list
          Regards,
          Joe

          Comment


          • #6
            Thanks again, Joe. This is a convenient way to achieve the right order. The only problem with this solution is that I have to copy the final result into my Tex Editor and it needs further formatting in Tex which I was trying to avoid using tabout. But as you say it is a nice meantime solution.

            I will also write a mail to Ian Watson. Maybe he knows how to handle this issue. I will give an update later.

            Comment


            • #7
              Ian confirmed that tabout does not support such a feature at the moment. For frequency tables he recommended Ben Jann's fre package which allows for an ascending or descending order of the rows.

              Comment

              Working...
              X