Announcement

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

  • Scoring combinations of dummy variables

    Good day all,

    I'm not an experienced Stata user.

    I have a sample containing 4 dummy variables (A,B,C,D) and I need to score combinations of these 4 variables from 0 to 3. 0 being low quality, 3 being highest quality.

    How can I best write a script that will score these combinations?

    I worked out the combinations and scores in the below list.
    A B C D Score
    0 1 1 1 3
    1 1 1 0 3
    1 1 0 1 3
    1 0 1 1 3
    1 1 1 1 3
    0 1 1 0 2
    1 0 1 0 2
    0 0 0 1 1
    0 0 1 0 1
    0 1 0 0 1
    1 0 0 0 1
    0 0 1 1 1
    1 1 0 0 1
    0 1 0 1 1
    1 0 0 1 1
    0 0 0 0 0
    I would appreciate any help on this.

    Kind regards,

    Joshua

  • #2
    your rule is not clear; for example, the second-to-last row of your examples has two "1's" but the new "Score" variable is "1"; please clarify what the rule should be so we can provide help; also, please read the FAQ for better ways to show your data

    Comment


    • #3
      My apologies,

      Indeed, this version is better formatted.

      So, second-to-last row is saying if variable A and D are equal to 1 total quality score receives a rating of 1. This logic will apply for each row,

      Hoepfully this is more clear?

      Click image for larger version

Name:	score.PNG
Views:	1
Size:	7.9 KB
ID:	1360438

      Comment


      • #4
        first, is this the complete set of rules: score gets a "1" for a B, another for a C and another for either an A or a D for a max of 3?

        second, the question is not so much how well formatted the data are (though this is more legible, thank you) but how easy is is to import into Stata; the user-written command -dataex- (available from SSC) makes it easy to import, and thus try various codes, in Stata - please read the FAQ

        Comment


        • #5
          This is the complete set of rules, yes.

          I did not include a dataex import because I don't know how to translate these rules in Stata syntax. I was thinking IF-statements but that does not seem efficient so I'm a bit lost.

          Would you have any idea?

          Comment


          • #6
            first, -dataex- is for data, not for syntax or "rules"; if, however, the above guess is correct, as you state, then:
            Code:
            gen byte Score = B+C
            replace Score=Score+1 if (A==1 | D==1)
            replace Score=Score-1 if A==1 & D==1
            untested because you did not use -dataex- to make it easy to import your data

            Comment


            • #7
              Thank you!

              It gives my good results except for 3 rules. I tried modifying the code with replace commands to correct the three rules but it is returning 0 instead of the correct Score.

              Would you know what I'm doing wrong?

              Kind regards,

              Joshua

              Code:
              gen byte Score = B+C
              replace Score=Score+1 if (A==1 | D==1)
              replace Score=Score-1 if A==1 & D==1
              
              replace Score=Score==1 if A==1 & B==1 & C==0 & D==0
              replace Score=Score==1 if A==1 & B==0 & C==0 & D==1
              replace Score=Score==3 if A==1 & B==1 & C==0 & D==1

              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input str1(A B C D)
              "A" "B" "C" "D"
              "0" "1" "1" "1"
              "1" "1" "1" "0"
              "1" "1" "0" "1"
              "1" "0" "1" "1"
              "1" "1" "1" "1"
              "0" "1" "1" "0"
              "1" "0" "1" "0"
              "0" "0" "0" "1"
              "0" "0" "1" "0"
              "0" "1" "0" "0"
              "1" "0" "0" "0"
              "0" "0" "1" "1"
              "1" "1" "0" "0"
              "0" "1" "0" "1"
              "1" "0" "0" "1"
              "0" "0" "0" "0"
              end

              Comment


              • #8
                why are you using double equals signs in your "new" rules? try again with just one equals sign (note that I am not clear what you are doing so this guess may well be wrong; however, it seems very unlikely that you want two equal signs)

                Comment


                • #9
                  Hi Rich,

                  Thank you for your reply.

                  If I use single equal signs it is giving me a invalid syntax error.

                  How would you correct these 3 rules in the best way?

                  Kind regards,

                  Roderick

                  Comment


                  • #10
                    sorry about that; how about:
                    Code:
                    replace score=1 if ...
                    where I have been a bit lazy

                    Comment


                    • #11
                      Thank you very much!

                      Indeed all rules are correct now.

                      Comment

                      Working...
                      X