Announcement

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

  • reshaping a dataset for a discrete choice model/conditional logit?

    Hey folks. I have data from a survey in which I ask each respondent to complete two discrete choice experiments, and they choose between two alternatives (A and B), each with a number of attributes.I want to format my data in a way that treats each of the respondent's 2 choices as an individual observation. I've included an example dataset illustrating the raw output I can download from the survey platform. In this simplified data, there are two choice occasions per respondent, two alternatives per choice occasion (A and B), and two attributes per alternative (dist and wc):

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte id str2(choice1 choice2) byte(dista1 dista2 distb1 distb2 wca1 wca2 wcb1 wcb2)
    1 "A1" "B2" 60  5 30 15 2 3 0 4
    2 "B1" "B2" 40 10  2 15 3 2 3 4
    end
    So in the above data, respondent 1 chose alternative A (dist = 60 and wc = 2) in the first choice occasion, and chose alternative B (dist = 15 and wc = 4) in the second choice occasion, and so on. I want to treat each choice occasion as an individual observation in a conditional logit, so i'm trying to reformat my data to look like this:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(id choiceoccasion) str1 alt byte(chosen dist wc)
    1 1 "A" 1 60 2
    1 1 "B" 0 30 0
    1 2 "A" 0  5 3
    1 2 "B" 1 15 4
    2 3 "A" 0 40 3
    2 3 "B" 1  2 3
    2 4 "A" 0 10 2
    2 4 "B" 1 15 4
    end
    I know this is probably accomplished through some combination of the -expand- and -reshape- commands, but I can't for the life of me figure out how to do it. Any help would be greatly appreciated!

  • #2
    I don't understand why your choiceoccasion variable is 3 and 4 for id==2, so I figured it should be 1 and 2, but please tell me if I misunderstood.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte id str2(choice1 choice2) byte(dista1 dista2 distb1 distb2 wca1 wca2 wcb1 wcb2)
    1 "A1" "B2" 60  5 30 15 2 3 0 4
    2 "B1" "B2" 40 10  2 15 3 2 3 4
    end
    
    reshape long dist wc, i(id) j(alt) string
    gen choiceoccasion = real(substr(alt, 2, 1))
    replace alt = upper(substr(alt, 1, 1))
    gen choice3 = cond(choiceoccasion == 1, choice1, choice2)
    gen chosen = alt == substr(choice3, 1, 1) & choiceoccasion == real(substr(choice3, 2, 1))
    
    drop choice?
    order id choice alt chosen
    sort id choice alt
    list, sepby(id) noobs abbr(14)
    
      +------------------------------------------------+
      | id   choiceoccasion   alt   chosen   dist   wc |
      |------------------------------------------------|
      |  1                1     A        1     60    2 |
      |  1                1     B        0     30    0 |
      |  1                2     A        0      5    3 |
      |  1                2     B        1     15    4 |
      |------------------------------------------------|
      |  2                1     A        0     40    3 |
      |  2                1     B        1      2    3 |
      |  2                2     A        0     10    2 |
      |  2                2     B        1     15    4 |
      +------------------------------------------------+

    Comment


    • #3
      Thanks so much! this is very helpful.

      Comment


      • #4
        Hi, I have the same problem but i have 4 choice occations. How would be the code with 4 occations?

        Comment

        Working...
        X