Announcement

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

  • Create two count variables that count number of incorrect and correct answers

    Dear Statalist,
    I am having some issues trying to create two count variables in a systematic way using loops. The first count variable (correct) should count, for each student, the number of correct answers while the second (incorrect) should count the number of incorrect answers out of a series of 9 binary variables. To be more precise, below is a sample of my data, which shows students' responses to two questions that assess their knowledge of local authorities' definition and missions.

    In this dataset:
    saying yes (=1) is correct for the variables local_auth_def_3 local_auth_mission_2 local_auth_mission_3 local_auth_mission_4 local_auth_mission_5
    saying yes (=1) is incorrect for the variable local_auth_def_1 local_auth_def_2 local_auth_mission_1 local_auth_mission_6

    Many thanks in advance for your help,

    Guyle


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(studentid local_auth_def_1 local_auth_def_2 local_auth_def_3 local_auth_mission_1 local_auth_mission_2 local_auth_mission_3 local_auth_mission_4 local_auth_mission_5 local_auth_mission_6)
     1 1 0 0 0 0 0 1 0 0
     2 1 0 1 0 1 1 0 0 1
     3 0 1 0 0 1 0 1 1 1
     4 1 0 0 0 0 1 1 0 1
     5 0 1 0 0 0 0 0 1 1
     6 0 1 0 1 1 0 1 1 0
     7 1 0 0 0 1 0 0 1 1
     8 0 1 0 1 0 0 1 0 0
     9 1 1 1 0 1 1 0 1 0
    10 0 1 0 1 1 1 0 0 0
    11 0 0 0 0 1 0 0 0 0
    12 1 1 1 0 0 0 1 0 1
    13 1 1 0 0 1 0 1 1 1
    14 0 0 0 1 1 1 1 1 1
    15 1 0 0 1 1 1 1 1 0
    16 1 1 1 1 1 0 1 0 0
    17 1 1 1 1 0 1 1 0 0
    18 0 1 1 0 1 1 0 0 1
    19 1 1 1 0 1 1 1 1 0
    20 0 1 0 0 0 0 0 1 0
    21 0 1 0 1 1 1 0 0 1
    22 1 0 1 1 1 0 0 0 0
    23 1 1 1 1 0 1 0 0 0
    24 0 1 1 0 0 1 0 0 1
    25 0 0 0 0 1 0 0 0 0
    end

  • #2
    Guyle:
    do you mean something along the following lines?
    Code:
    . egen check=rowmiss( local_auth_def_1- local_auth_mission_6)
    . g wanted=.
    . foreach var of varlist local_auth_def_1 local_auth_def_2 local_auth_mission_1 local_auth_mission_6 {
     replace wanted=1 if `var'==1
      }
    . replace wanted=0 if wanted==. & check==0
    . g wanted_2=.
    . foreach var of varlist local_auth_def_3 local_auth_mission_2 local_auth_mission_3 local_auth_mission_4 local_auth_mission_5 {
      replace wanted_2=1 if `var'==1
      }
    . replace wanted_2 =0 if wanted_2 ==. & check==0
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      You don't need any loops here.

      Consider this code.

      Code:
      local set1 local_auth_def_3 local_auth_mission_2 local_auth_mission_3 local_auth_mission_4 local_auth_mission_5
      local set2 local_auth_def_1 local_auth_def_2 local_auth_mission_1 local_auth_mission_6
      
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte(studentid local_auth_def_1 local_auth_def_2 local_auth_def_3 local_auth_mission_1 local_auth_mission_2 local_auth_mission_3 local_auth_mission_4 local_auth_mission_5 local_auth_mission_6)
       1 1 0 0 0 0 0 1 0 0
       2 1 0 1 0 1 1 0 0 1
       3 0 1 0 0 1 0 1 1 1
       4 1 0 0 0 0 1 1 0 1
       5 0 1 0 0 0 0 0 1 1
       6 0 1 0 1 1 0 1 1 0
       7 1 0 0 0 1 0 0 1 1
       8 0 1 0 1 0 0 1 0 0
       9 1 1 1 0 1 1 0 1 0
      10 0 1 0 1 1 1 0 0 0
      11 0 0 0 0 1 0 0 0 0
      12 1 1 1 0 0 0 1 0 1
      13 1 1 0 0 1 0 1 1 1
      14 0 0 0 1 1 1 1 1 1
      15 1 0 0 1 1 1 1 1 0
      16 1 1 1 1 1 0 1 0 0
      17 1 1 1 1 0 1 1 0 0
      18 0 1 1 0 1 1 0 0 1
      19 1 1 1 0 1 1 1 1 0
      20 0 1 0 0 0 0 0 1 0
      21 0 1 0 1 1 1 0 0 1
      22 1 0 1 1 1 0 0 0 0
      23 1 1 1 1 0 1 0 0 0
      24 0 1 1 0 0 1 0 0 1
      25 0 0 0 0 1 0 0 0 0
      end
      
      egen sum1 = rowtotal(`set1')
      egen sum2 = rowtotal(`set2')
      
      gen  correct = sum1 + (4 - sum2)
      The number of incorrect answers is then got by subtraction from 9. but it is redundant.

      Have a look at the help for egen, as functions such as anycount() offer other solutions.

      Comment


      • #4
        Originally posted by Nick Cox View Post
        You don't need any loops here.

        Consider this code.

        Code:
        local set1 local_auth_def_3 local_auth_mission_2 local_auth_mission_3 local_auth_mission_4 local_auth_mission_5
        local set2 local_auth_def_1 local_auth_def_2 local_auth_mission_1 local_auth_mission_6
        
        (example data omitted)
        
        egen sum1 = rowtotal(`set1')
        egen sum2 = rowtotal(`set2')
        
        gen correct = sum1 + (4 - sum2)
        Well, technically, the (while) loops are just hidden in egen here. You might be able to do without loops (depending on compiled C code for generate) like this:

        Code:
        local set1 local_auth_def_3 local_auth_mission_2 local_auth_mission_3 local_auth_mission_4 local_auth_mission_5
        local set2 local_auth_def_1 local_auth_def_2 local_auth_mission_1 local_auth_mission_6
        
        local sum1 : subinstr local set1 " " "+" , all
        local sum2 : subinstr local set2 " " "+" , all
        
        generate sum1 = `sum1'
        generate sum2 = `sum2'
        
        (code omitted)

        Comment


        • #5
          Carlo Lazzaro, Nick Cox and daniel klein thank you very much for your responses.

          In #2, Carlo Lazzaro 's code seems to tell if any of the listed variables has 1. However, wanted (wanted_2) do not give the number of correct (incorrect) answers per student.


          In #3, I could not proceed any further from
          Code:
          egen sum1 = rowtotal(`set1')
          I got an error message
          Code:
          invalid syntax

          Additionally, I don't think counting the number of incorrect answers is redundant, Nick Cox, since the different modalities are not mutually exclusive. I might be wrong though. Let me know what you think.


          In #4, I got the same invalid syntax error message when I typed:
          Code:
          generate sum1 = `sum1'
          Any idea where the issue is?


          Many thanks,

          Guyle

          Comment


          • #6

            I don't know for sure what the issue is with your application of my code or of Daniel klein's code. Some possibilities are that you didn't define the local macro at all, or that you defined it in a place where it could not be seen. Essentially the definition and the use must be in the same place, e.g. within an interactive session, within a do-file, and so on. For example, executing separate code chunks from a do-file editor window qualifies as two or more places. This pitfall is subtle and bites you if you don't understand but documented at https://journals.sagepub.com/doi/pdf...36867X20931028

            Irrespective of the survey details of precisely which answers are correct and which incorrect, if anyone gets say 7 answers correct it is immediate that they got 2 incorrect. Or that's my logic.

            Comment


            • #7
              I missed a crucial part in your description, namely

              Originally posted by guyle Smith View Post
              saying yes (=1) is incorrect for the variable local_auth_def_1 local_auth_def_2 local_auth_mission_1 local_auth_mission_6
              One way of dealing with this is to reverse the coding for those variables, e.g.

              Code:
              recode local_auth_def_1 local_auth_def_2 local_auth_mission_1 local_auth_mission_6 (0=1) (1=0) // <- DO NOT DO THIS IF USING NICK'S CLEVER SUGGESTION
              However, given there are only correct or incorrect answers, we can derive one from the other knowing the total number of questions (i.e., variables).
              Last edited by daniel klein; 01 Feb 2024, 06:55. Reason: Nick's code handles the issue; DO NOT -recode-

              Comment


              • #8
                I didn't miss the point in #7. My code is directed at dealing with it. In any case that wouldn't explain the syntax errors.

                Comment


                • #9
                  Originally posted by Nick Cox View Post
                  I didn't miss the point in #7. My code is directed at dealing with it.
                  My bad. Indeed, I have overlooked the clever part of subtracting the wrong answers from the number of questions with reverse coding.

                  Comment


                  • #10
                    Thanks Nick Cox and daniel klein for your valuable insights. The code works indeed. I was running it line by line to understand what each command does, instead of running the entire code. I will build on this to count the number of incorrect answers.

                    I see your point in #6 Nick Cox . Thank you.

                    Best regards,

                    Guyle

                    Comment


                    • #11
                      Hi. MAY YOU PLEASE ASSIST. I have a similar challenge. I am new on stata...

                      I am trying to do two things:

                      1.To have a STATA code that will measure levels of knowledge based on the number of correct responses per participant (i.e. No Knowledge=0, Low Knowledge=1-2, Average knowledge=3, High knowledge=4-5)...such that, I can be able to say x% of respondents are at a low level because they only answered 1 or 2 questions correctly.

                      2.To create a composite/index variable from a selected 4 individual variables of a dataset (i.e. create an outcome variable from 4 individual variables). This will help with my multinomial regression model.

                      I will really appreciate...thanks in advance.

                      NB: There are 3 responses for each question (1=Yes, 2=No, 3=Dont know); where the correct responses are "Yes" for all (except the last question where "No" is correct).

                      Comment


                      • #12
                        . qui ds

                        .
                        .
                        .
                        . .

                        .
                        . . dataex `=word("`r(varlist)'", 1)' - `=word("`r(varlist)'", 5)'

                        ----------------------- copy starting from the next line -----------------------
                        Code:
                        * Example generated by -dataex-. To install: ssc install dataex
                        clear
                        input double idnum str7 barcode long sal byte(persno province)
                              130000 "SV02520" 1993605 . 1
                             5008000 "SV34432" 5920215 8 5
                             7984253 "SV36954" 7984253 . 7
                             7984253 "SV37000" 7984253 . 7
                             7984999 "SV37943" 7984999 . 7
                             7985338 "SV36983" 7985338 . 7
                            24004000 "SV06387" 5992646 4 5
                           101003000 "SV08474" 7984253 3 7
                           125006000 "SV04767" 1991704 6 1
                           182001000 "SV08501" 7984999 1 7
                           251002000 "SV08514" 7984999 2 7
                           251004000 "SV08516" 7984999 4 7
                           278001000 "SV08401" 7984735 1 7
                           290070000 "SV31926" 7984253 . 7
                           311070000 "SV36921" 7600909 . 7
                           355210000 "SV36942" 7984253 . 7
                         19936430000 "SV34347" 1993643 . 1
                         36001470000 "SV04989" 3600147 . 3
                         4.72004e+10 "SV30649" 4720040 . 4
                         47400430000 "SV90336" 4740043 . 4
                         56900610000 "SV22414" 5690061 . 5
                         56901230000 "SV35358" 5690123 . 5
                         58302140000 "SV52999" 5830214 . 5
                         59947490000 "SV03782" 5994749 . 5
                         86100930000 "SV51059" 8610093 . 8
                         86101170000 "SV22008" 8610117 . 8
                         86300150000 "SV51052" 8630015 . 8
                         86400990000 "SV65770" 8640099 . 8
                         86401610000 "SV51058" 8640161 . 8
                         86602440000 "SV11891" 8660244 . 8
                         86604440000 "SV11934" 8660444 . 8
                         86804080000 "SV13872" 8680408 . 8
                        160004000101 "SV11221" 1600040 1 1
                        160004000102 ""        1600040 2 1
                        160004000103 "SV19655" 1600040 3 1
                        160004000104 "SV14452" 1600040 4 1
                        160004000105 "SV10511" 1600040 5 1
                        160004000301 ""        1600040 1 1
                        160004000503 ""        1600040 3 1
                        160004000603 ""        1600040 3 1
                        160004000803 ""        1600040 3 1
                        160004000903 ""        1600040 3 5
                        160004001101 "SV12234" 1600040 1 1
                        160004001101 "SV65327" 1600040 1 1
                        160004001102 "SV65313" 1600040 2 1
                        160004001103 ""        1600040 3 1
                        160004001501 ""        1600040 1 1
                        160004001601 "SV10465" 1600040 1 1
                        160004001602 "SV26120" 1600040 2 1
                        160004002101 "SV10481" 1600040 1 1
                        160004002301 ""        1600040 1 1
                        160004002701 "SV65239" 1600040 1 1
                        160004002702 ""        1600040 2 1
                        160004002703 "SV65296" 1600040 3 1
                        160004003201 "SV65301" 1600040 1 1
                        160004003202 "SV65306" 1600040 2 1
                        160004003203 "SV31053" 1600040 3 1
                        160004003204 "SV32985" 1600040 4 1
                        160004003405 ""        1600040 5 1
                        160004003405 ""        1600040 5 1
                        160004003701 "SV65258" 1600040 1 1
                        160004003702 "SV65333" 1600040 2 1
                        160004003703 "SV65340" 1600040 3 1
                        160004003704 "SV52297" 1600040 4 1
                        160004004201 "SV04715" 1600040 1 1
                        160004004202 "SV65315" 1600040 2 1
                        160004004203 "SV65278" 1600040 3 1
                        160004004204 "SV65336" 1600040 4 1
                        160004004205 "SV52352" 1600040 5 1
                        160004004206 "SV67028" 1600040 6 1
                        160004004701 "SV65202" 1600040 1 1
                        160004004702 "SV65324" 1600040 2 1
                        160004004702 "SV65324" 1600040 2 2
                        160004004703 ""        1600040 3 2
                        160004004703 "SV52200" 1600040 3 1
                        160004004704 ""        1600040 4 1
                        160004004705 ""        1600040 5 1
                        160004005301 "SV10455" 1600040 1 1
                        160004005801 "SV08272" 1600040 1 1
                        160004005802 "SV65240" 1600040 2 1
                        160004005803 ""        1600040 3 1
                        160004006301 "SV65325" 1600040 1 1
                        160004006801 "SV65330" 1600040 1 1
                        160004006802 "SV65339" 1600040 2 1
                        160004006803 "SV52301" 1600040 3 1
                        160004008002 "SV13183" 1600040 2 1
                        160004012301 ""        1600040 1 1
                        160004013403 ""        1600040 3 1
                        160004014502 ""        1600040 2 1
                        160004016503 "SV19991" 1600040 3 2
                        160004023404 ""        1600040 4 1
                        160004032103 ""        1600040 3 1
                        162001800501 "SV19417" 1620018 1 1
                        162001800502 "SV22858" 1620018 2 1
                        162001800503 "SV11236" 1620018 3 1
                        162001800504 "SV10582" 1620018 4 1
                        162001800505 ""        1620018 5 1
                        162001800701 "SV65671" 1620018 1 1
                        162001800702 ""        1620018 2 1
                        162001801001 "SV65633" 1620018 1 1
                        end
                        label values province province
                        label def province 1 "Western Cape", modify
                        label def province 2 "Eastern Cape", modify
                        label def province 3 "Northern Cape", modify
                        label def province 4 "Free State", modify
                        label def province 5 "KwaZulu-Natal", modify
                        label def province 7 "Gauteng", modify
                        label def province 8 "Mpumalanga", modify
                        ------------------ copy up to and including the previous line ------------------

                        Listed 100 out of 66615 observations
                        Use the count() option to list more

                        .

                        Comment


                        • #13
                          Sonwabile Mbuma #11 and #12 duplicate your posts in another thread. Please don't repeat posts like this. Far from increasing your chance of getting a reply, such repetition just is confusing and makes it seem a poor use of our time to try to work out what you want.

                          Please read through the entire FAQ Advice.

                          Others: if interested in #11 and #12 please follow https://www.statalist.org/forums/for...index-variable

                          Comment

                          Working...
                          X