Announcement

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

  • Reshape help

    Dear all,

    I'm dealing with some data from a conjoint experiment. Currently, it is in wide format (each row an individual), and I'd like to get it into long format. Here is some dummy data from the current wide format:

    Code:
    clear
    input id    govA_c1_1    govA_c1_2    govB_c1_1    govB_c1_2    choice_1    choice_2
    1    1    2    1    2    1    2
    2    2    1    2    1    2    1
    3    2    2    1    1    2    1
    4    2    1    2    1    1    2
    end
    To explain, respondents were given the task of choosing between two governments (gov) with a set of characteristics, here reduced to just one with two values (c1). They had to make this decision twice. Thus, govA_c1_1 notes the characteristic value for government A in the first round, govA_c1_2 the second round, etc. The choice columns indicate which government (1 or 2) they chose.

    I'd like to get this into a format such that each row identifies each contest/government/respondent combination. The row 'c1' shows characteristic 1 value for governments 1/2 at each contest. Choice indicates whether respondent chose that government in that contest.

    Code:
    clear
    input id    contest no.    c1    choice
    1    1    1    1
    1    1    1    0
    1    2    2    0
    1    2    2    1
    2    1    2    0
    2    1    1    1
    2    2    2    0
    2    2    1    1
    end
    Right now, I can't seem to do this correctly; either getting an error or some nonsensical format. I would guess I'm missing an intermediary step or omitting something from the command.

    I hope this makes sense and I haven't made a mistake up to this point.

    Thanks!

  • #2
    We can't comment on what you did wrong here without seeing any code. But without understanding all of this, or even most of it, it seems to me that https://www.stata.com/support/faqs/d...-with-reshape/ may apply especially the idea that you may need two reshapes to get where you want to be.

    I did this which may help.

    Code:
    clear
    input id    govA_c1_1    govA_c1_2    govB_c1_1    govB_c1_2    choice_1    choice_2
    1    1    2    1    2    1    2
    2    2    1    2    1    2    1
    3    2    2    1    1    2    1
    4    2    1    2    1    1    2
    end
    
    reshape long  govA_ govB_, i(id choice_1 choice_2) j(which) string 
    
    reshape long gov , i(id choice_1 choice_2 which) j(WHICH) string
    
    list, sepby(id)
    
         | id   choice_1   choice_2   which   WHICH   gov |
         |------------------------------------------------|
      1. |  1          1          2    c1_1      A_     1 |
      2. |  1          1          2    c1_1      B_     1 |
      3. |  1          1          2    c1_2      A_     2 |
      4. |  1          1          2    c1_2      B_     2 |
         |------------------------------------------------|
      5. |  2          2          1    c1_1      A_     2 |
      6. |  2          2          1    c1_1      B_     2 |
      7. |  2          2          1    c1_2      A_     1 |
      8. |  2          2          1    c1_2      B_     1 |
         |------------------------------------------------|
      9. |  3          2          1    c1_1      A_     2 |
     10. |  3          2          1    c1_1      B_     1 |
     11. |  3          2          1    c1_2      A_     2 |
     12. |  3          2          1    c1_2      B_     1 |
         |------------------------------------------------|
     13. |  4          1          2    c1_1      A_     2 |
     14. |  4          1          2    c1_1      B_     2 |
     15. |  4          1          2    c1_2      A_     1 |
     16. |  4          1          2    c1_2      B_     1 |
         +------------------------------------------------+
    I didn't really grasp how A and B appear as variable name parts in your input, but not as part of your output, but that's not your fault as I didn't really try to internalise your description.

    Comment

    Working...
    X