Announcement

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

  • Inlist function

    Hi everyone,

    I need to double check that the inlist function below will not "count" the same value twice.
    For context, I am scoring a cognitive function test. Participants were told to remember 10 words. They then had 20 attempts at saying the 10 words back. The words have been given a numerical value from 1 to 10. I need to confirm that according to the syntax below, if a participant said the first word three times, they only get 1 point and not 3. I can't think of an assert statement that would check this effectively.

    Thank you,
    Emma

    gen cpait_word = 0
    quietly foreach word in cpait_Word1_20 cpait_Word1_21 cpait_Word1_22 cpait_Word1_23 cpait_Word1_24 ///
    cpait_Word1_25 cpait_Word1_26 cpait_Word1_27 cpait_Word1_28 cpait_Word1_29 ///
    cpait_Word1_30 cpait_Word1_31 cpait_Word1_32 cpait_Word1_33 cpait_Word1_34 ///
    cpait_Word1_35 cpait_Word1_36 cpait_Word1_37 cpait_Word1_38 cpait_Word1_39 {
    replace cpait_word = cpait_word + inlist(`word', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) if `word' !=.
    }
    tab cpait_word, m

  • #2
    No, the code would score 3 points. And it has nothing to do with -inlist()-. If the respondent gives any response between 1 and 10, then when that particular response variable comes up in the loop, the respondent will get another point.

    I think this is one of those problems that will be very difficult to solve in the wide layout you have. It's easy in long layout:

    Code:
    // CREATE TOY DATA
    clear*
    set obs 10
    gen long respondent_id = _n
    
    set seed 1234
    
    forvalues i = 20/39 {
        gen cpait_Word1_`i' = runiformint(0,30)
    }
    
    //    GO LONG.  (IF NO RESPONDENT_ID VARIABLE EXISTS, CREATE ONE FIRST)
    reshape long cpait_Word1_, i(respondent_id) j(response_num)
    //    SCORE THE RESPONSES
    by respondent_id cpait_Word1_, sort: gen score = inlist(cpait_Word1_, 1, 2, 3, 4, 5, ///
        6, 7, 8, 9, 10) if _n == 1
    by respondent_id: replace score = sum(score)
    by respondent_id: replace score = score[_N]
    
    reshape wide
    If you look at respondent_id #1 in this example, you will see that the response 3 is picked 3 times, but is only counted once in the scoring.

    I've brought the data back to wide layout at the end here, but depending on what you plan to do from this point, you might find it better to leave the data long. Most analyses in Stata are easier with long data.
    Last edited by Clyde Schechter; 01 Dec 2017, 15:59.

    Comment


    • #3
      Other solution.
      Code:
      forval i=1/10 {
      egen _match`i' = anymatch(cpait_Word1_*), v(`i')
      }
      
      egen cpait_word = rowtotal(_match*)
      drop _match*
      Last edited by Romalpa Akzo; 01 Dec 2017, 21:17.

      Comment


      • #4
        Thank you both!

        Comment

        Working...
        X