Announcement

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

  • Median from multiple variables

    Hi everyone,

    After hours of searching, I cannot figure out how to take the median of multiple columns.
    My attempts:
    1. create a local list that saved all the values of multiple variables, then tried to find the median of the list
    2. create a new variable with the medians for each row; then, took the median of that column
    Is there another way to find the median from multiple columns/variables?

    Thank you all so much!

  • #2
    -help egen- and look at the -rowmedian()- function.

    Comment


    • #3
      Hi Mr. Schechter,

      That was my first attempt: generated a column of medians using -rowmedian()- function; then, took the median of that column.
      My question is a little confusing - I want to get one median from multiple columns. I guess my first attempt would/should produce the same median if I manually took all the values from multiple columns and took the median of this?

      Comment


      • #4
        No; the median of a matrix is not necessarily equal to the median of its row or column vectors. Consider 1, 98, 99; 1, 42, 99; 1, 1, 1. The median of the medians is 42 but the overall median is 1. Often you will get closer than that, but there are no guarantees.

        This worked for me after installing moremata from SSC.

        Code:
        clear
        input y1 y2 y3
        1  1 1
        1 42 43
        1 98 99
        end
        
        mata
        data = st_data(., "y1 y2 y3")
        mm_median(vec(data))
        
          1
        Another method is to reshape long your data first.

        Comment


        • #5
          Hi Professor Cox,

          I dissected your solution:
          Code:
          ssc install moremata
          
          mata
          data = st_data(., "y1 y2 y3", 0)
          mm_median(vec(data))
          end
          Code:
          st_data()
          creates a sub dataset of the columns I want to take the median of/combine
          ., returns all observations
          I added selectvar==0 to exclude missing values
          Code:
          vec()
          stacks multiple columns into one column
          Code:
          mm_median()
          uses median function from -moremata- package

          Question: If vec() transforms the matrix into one column, why can't we treat the resulting column as a regular column? I use can't because I've tried and failed to get any result when I performed calculations using the vector.

          Comment


          • #6
            The effect of

            Code:
            vec()
            is not permanent any more than y = x + 2 would change x by adding 2 to it.

            If I understand you correctly you need an assignment, say
            Code:
            wanted = vec(data)

            Comment


            • #7
              Just to note that #4 was not well phrased: the key sentence should be

              No; the median of a matrix is not necessarily equal to the median of the medians of its row or column vectors.

              Comment


              • #8
                Hi Professor Cox,

                The median function worked so I don't necessarily need an assignment. I was just wondering why this won't work:

                Code:
                wanted = vec(data)
                summarize wanted, detail
                I get an invalid expression error but I think your note about vec() addresses this.

                Comment


                • #9
                  That’s mixes Mata and Stata. wanted is not a Stata variable; you can’t feed a Mata vector directly to a command like summarize.

                  Comment


                  • #10
                    Thank you, Professors Schechter and Cox!

                    Comment

                    Working...
                    X