Announcement

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

  • quartiles

    I want to do table like this but i dont know how to do it
    can any one help me with codes?






    Click image for larger version

Name:	stata.jpg
Views:	1
Size:	384.0 KB
ID:	1602665

  • #2
    You can calculate quartiles by variables such as year using the user contributed function -egen, xtile()- from the package Egenmore.

    Comment


    • #3
      Code:
      sysuse auto, clear
      xtile qmpg = mpg, nq(4)
      * Table:
      tabstat price, stat(mean p50 min max) by(qmpg)
      * t-test between lowest and highest:
      ttest price if inlist(qmpg, 1, 4), by(qmpg)

      Comment


      • #4
        how i make the table like if i found the quartles then find the t-test how i make the table

        Comment


        • #5
          One way is to copy the output as table, paste onto Excel and manually do the editing/formatting. This may be better if this is just a one-time task.

          To automate the process, one way is to do that through -putexcel-:
          Code:
          help putexcel
          Also, check out this very useful series of blog posts on laying out the results in Excel. Part 1, part 2, part 3.

          Comment


          • #6

            whta is 1 and 4 here (ttest price if inlist(qmpg, 1, 4), by(qmpg)) and when i take t-test for the size give me this ( . * t-test between lowest and highest: . ttest size if inlist(size_4, 1, 4), by(size_4) 1 group found, 2 required r(420);

            Comment


            • #7
              Originally posted by ASMA ALSHAREEF View Post
              whta is 1 and 4 here (ttest price if inlist(qmpg, 1, 4), by(qmpg)) and when i take t-test for the size give me this ( . * t-test between lowest and highest: . ttest size if inlist(size_4, 1, 4), by(size_4) 1 group found, 2 required r(420);
              inlist(qmpg, 1, 4) means "only if qmpg == 1 or qmpg == 4". This two groups were kept because the t-test only used the lowest and highest groups of the four (according to the description above the table in post #1).

              Comment


              • #8
                Thank you Ken
                what if the quartiles are constructed each year,?

                Comment


                • #9
                  Usually we can use the -by()- option, but xtile is special that it does not work with by(), so you'd probably have to create a new quartile variable for each year using -if-. For example:

                  Code:
                  xtile new_var19 = old_var if year == 2019 , nq(4)
                  xtile new_var20 = old_var if year == 2020 , nq(4)
                  xtile new_var21 = old_var if year == 2021 , nq(4)
                  Then you can combine them back into one variable. But remember to label them clearly saying that it's year-based quartlies.

                  For example, this would result in an error:
                  Code:
                  clear
                  sysuse auto
                  bysort foreign: xtile new = mpg, nq(4)
                  But this would work:
                  Code:
                  clear
                  sysuse auto
                  xtile q_domestic = mpg if foreign == 0, nq(4)
                  xtile q_local = mpg if foreign == 1, nq(4)
                  egen q_combined = rowtotal(q_domestic q_local)
                  Last edited by Ken Chui; 12 Apr 2021, 09:49.

                  Comment

                  Working...
                  X