Announcement

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

  • Trying to create a new variable of the differences between the means of 2 decile data sets

    I want to create a variable which is the difference between 2 other variables (the log hourly pay of males split into deciles and the log hourly pay of females split into deciles). Both of these other variables are split into deciles, so we want the gap between the first deciles of the 2 variables respectively and so on until the gap between the 2 10th deciles of the two variables all in one variable, is this possible?

  • #2
    Could you please post an excerpt from your data, for example with the dataex package, so that we don't have to imagine what your variables look like? Section 12 of the FAQ has more helpful advice for new list members.
    Code:
    ssc d dataex

    Comment


    • #3
      **must split sexes first**

      gen lHOURPAYMALES=lHOURPAY if SEX==1
      gen lHOURPAYFEMALES=lHOURPAY if SEX==2

      **splits log Hourly pay into MALES and FEMALES**

      xtile dlHOURPAYMALES=lHOURPAYMALES, nq(10)
      xtile dlHOURPAYFEMALES=lHOURPAYFEMALES, nq(10)

      **splits seperate MALES and FEMALES into deciles in each sex**

      gen dlHOURPAY10MALES=lHOURPAYMALES if dlHOURPAYMALES==10
      **top decile of men**

      gen dlHOURPAY10FEMALES=lHOURPAYFEMALES if dlHOURPAYFEMALES==10
      **top decile of female**

      egen avdlHOURPAYm=mean(lHOURPAYMALES), by(dlHOURPAYMALES)
      **generates average of each decile of male data**

      egen avdlHOURPAYf=mean(lHOURPAYFEMALES), by(dlHOURPAYFEMALES)
      **generates average of each decile of female data**

      sorry not sure how to post properly but these are my commands from my do file using labour force survey data

      I am trying to put the differences between avdlHOURPAYm and avdlHOURPAYf into one variable,

      Comment


      • #4
        Originally posted by Oscar Lewis View Post
        sorry not sure how to post properly but these are my commands from my do file using labour force survey data
        The FAQ that I mentioned in post #2 explains that Stata commands should be posted with CODE markup. Please have a look at dataex, share a few observations and then explain what exactly you want to calculate. The description below, taken from your first post, isn't very clear.

        we want the gap between the first deciles of the 2 variables respectively and so on until the gap between the 2 10th deciles of the two variables all in one variable

        Comment


        • #5
          OK, I think I see what you're trying to do here. The approach you've taken won't work because the way you have split the data for males and females into two variables creates a pair of variables, one of which always has missing values, so you can't compare them. Try this:

          Code:
          assert !missing(SEX)
          xtile dl_hour_pay_m = lHOURPAY if SEX == 1, nq(10)
          xtile dl_hour_pay_f = lHOURPAY if SEX == 2, nq(10)
          egen dl_hour_pay = rowmax(dl_hour_pay_m dl_hour_pay_f)
          drop dl_hour_pay_m dl_hour_pay_f
          egen av_dl_hour_pay = mean(dl_hour_pay), by(sex dl_hour_pay)
          
          by dl_hour_pay (sex), sort: gen difference = av_dl_hour_pay[1] - av_dl_hour_pay[_N] ///
              if sex[1] != sex[_N]
          I think that will do what you want. If it doesn't please post back showing some sample data (use -dataex-), the results you got from the code here, and an explanation of what you expected to get.

          Note: Depending on the size of your data set and your distributions of HOURPAY, it is conceivable that -xtile- will not actually create 10 deciles. For that reason, there may be some deciles that apply only to males or only to females. That is the rationale for the -if sex[1] != sex[_N]- part of the final command, to assure that the resulting "difference" is missing value and not a misleading zero.

          Comment


          • #6
            Thank you for your help so far, I have followed the steps that you posted but on the final command; using the bysort at the start; it states that factor variable and time-series operators are not allowed. As to your notes we have used log of HOURPAY to get to fit more of a normal distribution and the data set is large enough

            heres the results from the here,

            Code:
            assert !missing(SEX)
            xtile dlHOURPAYm=lHOURPAY if SEX==1, nq(10)
            xtile dlHOURPAYf=lHOURPAY if SEX==2, nq(10)
            egen dlHOURPAY=rowmax(dlHOURPAYm dlHOURPAYf)
            drop dlHOURPAYm dlHOURPAYf
            egen avdlHOURPAY=mean(dlHOURPAY), by(SEX dlHOURPAY)
            
            by dlHOURPAY(SEX), sort:gen difference=avdlHOURPAY(1) - avdlHOURPAY(N) if SEX(1) !=SEX(N)
            Sorry thats just what you posted again, Im trying to get the difference in means of the deciles of male and female lHOURPAY into 1 variable, i will try and upload some sample data now

            Comment


            • #7
              The problem you are having with the final command is because you are using parentheses () around the 1's and _N's, whereas square brackets [] are required. Also, it's _N, not N: the underscore character cannot be omitted. That said, do not put square brackets around SEX in that command: parentheses are exactly right there.

              Comment


              • #8
                Code:
                assert !missing(SEX)
                
                . xtile dlHOURPAYm=lHOURPAY if SEX==1, nq(10)
                
                . xtile dlHOURPAYf=lHOURPAY if SEX==2, nq(10)
                
                . egen dlHOURPAY=rowmax(dlHOURPAYm dlHOURPAYf)
                
                . drop dlHOURPAYm dlHOURPAYf
                
                . egen avdlHOURPAY=mean(dlHOURPAY), by(SEX dlHOURPAY)
                
                . 
                . by dlHOURPAY(SEX), sort:gen difference=avdlHOURPAY[1] - avdlHOURPAY[_N] if SEX[1] !=SEX[_N]
                factor variables and time-series operators not allowed
                that is what i input this time but the results on the final command was still factor variables and time-series operators not allowed

                Comment


                • #9
                  I am completely at a loss to explain this. I used the -auto- data set and renamed a bunch of variables to match the variable names you are using, and then ran that last command, and it worked with no difficulty:

                  Code:
                  . clear*
                  
                  . sysuse auto
                  (1978 Automobile Data)
                  
                  . xtile dlHOURPAY = price, nq(10)
                  
                  . rename foreign SEX
                  
                  . rename turn avdlHOURPAY
                  
                  . 
                  . by dlHOURPAY(SEX), sort:gen difference=avdlHOURPAY[1] - avdlHOURPAY[_N] if SEX[1] !=SEX[_N]
                  
                  . 
                  end of do-file

                  Comment


                  • #10
                    In the last line, you need a space between "dlHOURPAY" and "(SEX)"

                    Code:
                    by dlHOURPAY (SEX), sort:gen difference=avdlHOURPAY[1] - avdlHOURPAY[_N] if SEX[1] !=SEX[_N]
                    StataNow/MP 19.5 (64-bit x86-64)
                    Win 11

                    Comment


                    • #11
                      I thought that was it, too, but as you can see in #9, even without putting the space there, it still runs without error.

                      Added later: it may be that the parser in Stata version 14.1 is forgiving about the absence of a space, but perhaps Oscar is using an earlier version that is not? Anyway, he should try putting in the space and see if that solves the problem.
                      Last edited by Clyde Schechter; 14 Apr 2016, 18:22.

                      Comment


                      • #12
                        In Stata 13, I get the error:

                        Code:
                        . by dlHOURPAY(SEX), sort:gen difference=avdlHOURPAY[1] - avdlHOURPAY[_N] if SEX[1] !=SEX[_N]
                        factor variables and time-series operators not allowed
                        r(101); t=0.00 19:21:31
                        StataNow/MP 19.5 (64-bit x86-64)
                        Win 11

                        Comment

                        Working...
                        X