Announcement

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

  • Order of categorical variable

    Hi! I am now regressing on categorical variables, one of them is job satisfaction
    I tabulated it and the order of each category is the following:




    I want to change the order to this: VERY DISSATIS, DISSATISFIED, NEITHER, SATISFD, VERY SATISFD.
    What should I do? Thanks a lot!!

  • #2
    Chendong:
    welcome to this forum.
    Following the FAQ is the best way to make your example/problem readable, get helpful replies and save everybody's time.
    That said, you may want to take a look at -recode-.
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      I agree with Carlo Lazzaro that a data example would be really helpful.

      My guess is that you have a string variable which sorts alphabetically, which is no use here. If so, you want a numeric variable with value labels.


      Code:
      label def numcat 1 "VERY DISSATIS" 2 DISSATISFIED 3 NEITHER 4 SATISFD 5 "VERY SATISFD"
      encode strcat, gen(numcat) label(numcat) .
      
      label def numcat 1 "very dissatisfied"  2 "dissatisfied" 3 "neither" 4 "satisfied" 5 "very satisfied" , modify
      As you can see, I recommend value labels that are mostly or entirely lower case and avoid arbitrary abbreviations. In some contexts, those above might be a little long, but there are solutions for that.

      Comment


      • #4
        And a secondary guess is that you originally had a string variable, and you transformed it into a numeric variable with value labels using the encode command, without using a format to specify the encoding, as Nick's code show us.

        In that case, first
        Code:
        decode numcat, generate(strcat)
        drop numcat
        to return to a string variable, and then follow Nick's advice.

        Comment


        • #5
          Thank you so much for the suggestions!

          I tried the following commands:

          decode jobsatis, generate(strjobsatis)
          drop jobsatis
          label def jobsatis 1 "VERY DISSATIS" 2 "DISSATISFIED" 3 "NEITHER" 4 "SATISFD" 5 "VERY SATISFD"
          encode strjobsatis, gen(jobsatis) label(jobsatis)
          label def jobsatis 1 "very dissatisfied" 2 "dissatisfied" 3 "neither" 4 "satisfied" 5 "very satisfied" , modify

          However, the order has changed to this when tabulating:
          dissatisfied, neither, satisfied, very dissatisfied, very satisfied

          the commands somehow worked but I don't know why "very dissatisfied" is now between "satisfied" and "very satisfied"

          Please give me some further advice.

          Best,
          Chendong

          Comment


          • #6
            The basic problem we have addressing this is that we do not know what the data looked like in post #1. You said you tabulated it and the order was ... and nothing is shown. Perhaps we guessed wrong about how it was coded - it was just a guess.

            At this point, please return to the dataset you described in post #1 and run
            Code:
            dataex jobsatis
            and copy and paste the output of example data (there will be 100 observations, plus at the end there will be definitions of any value label assigned to the variable, into a new reply here.

            Here is an example of dataex output on a very small dataset.
            Code:
            . dataex
            
            ----------------------- copy starting from the next line -----------------------
            [CODE]
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input byte foreign
            0
            0
            0
            0
            0
            0
            1
            1
            end
            label values foreign origin
            label def origin 0 "Domestic", modify
            label def origin 1 "Foreign", modify
            [/CODE]
            ------------------ copy up to and including the previous line ------------------
            
            Listed 8 out of 8 observations
            
            .
            In your dataex output you will select the lines between, but not including, "copy starting from the next line" and "copy up to and including the previous line" and then paste that into your reply.
            Last edited by William Lisowski; 04 Feb 2022, 16:24.

            Comment


            • #7
              Hi William,

              Sorry for the problem in #1. I followed your code, hope you can see it now

              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input byte jobsatis
              2
              2
              2
              2
              2
              4
              2
              1
              .
              1
              1
              2
              2
              2
              .
              1
              1
              1
              1
              2
              2
              .
              1
              2
              .
              2
              .
              1
              2
              5
              1
              1
              2
              2
              2
              2
              2
              .
              2
              5
              .
              .
              2
              2
              .
              3
              1
              2
              2
              1
              1
              .
              2
              .
              .
              .
              1
              4
              1
              1
              3
              .
              .
              .
              2
              .
              2
              .
              .
              4
              2
              3
              2
              .
              3
              .
              .
              3
              1
              2
              5
              2
              .
              .
              .
              .
              .
              .
              .
              2
              2
              4
              3
              1
              .
              2
              1
              4
              2
              4
              end
              label values jobsatis LABP
              label def LABP 1 "VERY    SATISFD", modify
              label def LABP 2 "SATISFD", modify
              label def LABP 3 "NEITHER", modify
              label def LABP 4 "DISSATISFIED", modify
              label def LABP 5 "VERY    DISSATIS", modify
              Thank you very much.

              Comment


              • #8
                We now see that what you wanted was to reverse the order they were originally in.
                Code:
                tab jobsatis
                // turn 5 into 1, 4 into 2, ...
                replace jobsatis = 6-jobsatis
                label def LABP 5 "VERY    SATISFD", modify
                label def LABP 4 "SATISFD", modify
                label def LABP 3 "NEITHER", modify
                label def LABP 2 "DISSATISFIED", modify
                label def LABP 1 "VERY    DISSATIS", modify
                tab jobsatis
                // or if you prefer
                label def LABP 1 "very dissatisfied" 2 "dissatisfied" 3 "neither" 4 "satisfied" 5 "very satisfied" , modify
                tab jobsatis
                Code:
                . tab jobsatis
                
                        jobsatis |      Freq.     Percent        Cum.
                -----------------+-----------------------------------
                 VERY    SATISFD |         20       28.57       28.57
                         SATISFD |         35       50.00       78.57
                         NEITHER |          6        8.57       87.14
                    DISSATISFIED |          6        8.57       95.71
                VERY    DISSATIS |          3        4.29      100.00
                -----------------+-----------------------------------
                           Total |         70      100.00
                
                . // turn 5 into 1, 4 into 2, ...
                . replace jobsatis = 6-jobsatis
                (64 real changes made)
                
                . label def LABP 5 "VERY    SATISFD", modify
                
                . label def LABP 4 "SATISFD", modify
                
                . label def LABP 3 "NEITHER", modify
                
                . label def LABP 2 "DISSATISFIED", modify
                
                . label def LABP 1 "VERY    DISSATIS", modify
                
                . tab jobsatis
                
                        jobsatis |      Freq.     Percent        Cum.
                -----------------+-----------------------------------
                VERY    DISSATIS |          3        4.29        4.29
                    DISSATISFIED |          6        8.57       12.86
                         NEITHER |          6        8.57       21.43
                         SATISFD |         35       50.00       71.43
                 VERY    SATISFD |         20       28.57      100.00
                -----------------+-----------------------------------
                           Total |         70      100.00
                
                . // or if you prefer
                . label def LABP 1 "very dissatisfied" 2 "dissatisfied" 3 "neither" 4 "satisfied" 5 "very satisf
                > ied" , modify
                
                . tab jobsatis
                
                         jobsatis |      Freq.     Percent        Cum.
                ------------------+-----------------------------------
                very dissatisfied |          3        4.29        4.29
                     dissatisfied |          6        8.57       12.86
                          neither |          6        8.57       21.43
                        satisfied |         35       50.00       71.43
                   very satisfied |         20       28.57      100.00
                ------------------+-----------------------------------
                            Total |         70      100.00
                The reason the code in post #3 didn't work was because in post #1 you did not use code delimiters [CODE] and [/CODE] to render your code accurately, and the Forum software squeezed out the extra spaces.
                Last edited by William Lisowski; 04 Feb 2022, 17:03.

                Comment


                • #9
                  Yes the order is exactly what I wanted.
                  Thank you so much William, that really helped me a lot!!!!!!

                  Comment

                  Working...
                  X