Announcement

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

  • 'egen' command - How to make Stata select the right level from an attribute?

    Hello, I´m using the following code and it is working but not with the information I would like:

    Code:
    egen choice1 = sum(choice == 1), by(userid)
    egen choice2 = sum(choice == 2), by(userid)
    egen nochoice = sum(choice == 3), by(userid)
    My dataset has 81 respondents who have to choose 12 times (12 choice tasks) among 3 alternatives ("alternative 1", "alternative 2" and "nochoice"). There are different attributes for each alternative: type of access, weekly access, days per week and price. For the first alternative, the attributes were labelled as alt1_type, alt1_weac, alt1_dayp, alt1_pri. However, for the second alternative the attributes were labelled as alt2_type, alt2_weac, alt2_dayp, alt2_pri. The attribute I want to focus on is the "type of access" (present in alt1_type and alt2_type) and the corresponding levels are "mixed gender" and "female only".

    The level ("mixed gender" and "female only") of each attribute ("type of access" present in "alt1_type" and "alt2_type") is shown to the respondant changing randomly between alternative 1 and 2. For example, in the first choice task (from a total of 12 choice tasks) a respondent can choose "female only" that appears under "alt1_type" and obviously "mixed gender" appears under "alt2_type". In the second choice task "mixed gender" appears under "alt1_type" and obviously "female only" appears under "alt2_type". Levels are randomly assigned to each alternative until 12 choice tasks are completed by the survey respondent. For nochoice is always the same level, so no problem in here.

    With the code below I'm assuming that the first alternative is always "female only" (or "mixed gender") and the second is always "mixed gender" (or "female only") and the third is always "nochoice" (that in that case is always the same, so no problem).

    In the dataset I have a variable called "choice" that equals 1 to the first alternative selected: alternative 1. Choice equals 2 to the second alternative selected: alternative 2 and 3 to the third alternative "nochoice". How can I chose the correct level from each attribute?

    Any suggestion is more than helpful! Many thanks!
    Nerea
    Last edited by Nerea Marti; 19 Jul 2017, 16:27.

  • #2
    I think your previous version of this question didn't get a response because it is difficult to be sure one has correctly visualized your data, and in any event without data to play with it's difficult test possible solutions to be sure we're not giving bad advice. So it's not clear that a second try will do much better. I would like to try to help, as I tried on the first topic you posted, but without data I'm at a loss.

    Please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post, looking especially at sections 9-12 on how to best pose your question.

    It would be particularly helpful to post a small hand-made example, perhaps with just two choice tasks for a few respondents.

    In particular, please read FAQ #12 and use dataex and CODE delimiters when posting to Statalist. To install dataex, type ssc install dataex and when that is complete type help dataex to read the simple instructions for using it. Using dataex will enable those who want to help you to quickly and easily create a 100% faithful replica of your situation to test their ideas and code on.

    Comment


    • #3
      Hello William,

      you are right, I cannot say anything else.

      I'm going to try to give you more relevant information. It does not give any error in Stata but doesn't give me the result I want: as I explained before the level of the variable "type of access" for the alternative 1 and 2 is assigned randomly. This is the entire Code that I'm using right now:

      Code:
      /* Summarize choice variable */
      egen choice1 = sum(choice == 1), by(userid)
      egen choice2 = sum(choice == 2), by(userid)
      egen nochoice = sum(choice == 3), by(userid)
      bysort userid: drop if _n>1
      drop block task choice
      gen choice1n = round(choice1/12,.01)
      gen choice2n = round(choice2/12,.01)
      gen nochoicen = round(nochoice/12,.01)
      gen choice12n = choice1n+choice2n
      keep userid choice1n choice2n nochoicen choice12n in_*
      Unfortunately I cannot use dataex because I'm using the Stata version of my University and it takes times to update once is installed. I'll use it once is uptaded but now I just can explain my dataset with screenshots, even though I know it is not recommended... In the first screenshot called "First part - Dataset - Choice task for user 31 and 35", there is an example of two users (31, 35) with the different variables shown before running the code above.

      Once the code has generated choi1n, choice2n and nochoicen, the result is in the second and last screenshot called "Second part - Dataset with choice distribution". Please note that in this part it has been deleted the 12 choice tasks and just one row per user is shown.

      I much appreciate your time and consideration,
      Nerea
      Attached Files
      Last edited by Nerea Marti; 20 Jul 2017, 14:45.

      Comment


      • #4
        I think you are approaching this from the wrong direction. The command(s) you will use to model DCE (which unfortunately I forget at the moment) will want your data structured differently. Each choice task will have two (or perhaps three, if "no choice" is an option) observations, one for each presented choice, with an indicator for the observations actually chosen.

        In Stata terms, you data is currently arranged in a wide layout. The experienced users here generally agree that, with few exceptions, Stata makes it much more straightforward to accomplish complex analyses using a long layout of your data rather than a wide layout of the same data. And, from my recollection, that is what is required by the commands that fit the models you will want to fit.

        You should start by determining, if you do not already know, what Stata command(s) you will be using for this analysis. Then, you should find the documentation for those commands in the appropriate Stata manual PDF included in your Stata installation and accessible through Stata's Help menu. You should read that carefully, and understand how your data needs to be reorganized. Then, using among others the reshape long command, you should get your data into the organization that your commands need. Then, you can try to prepare the tabulations you want. Effort expended solving your current problem with your data organized as it now is will have no payoff in the long run, since your data ultimately will need to be reorganized.

        Comment


        • #5
          William gives excellent advice as always.

          Another cross-posting: http://www.talkstats.com/showthread....m-an-attribute

          Sorry, I don't understand most of this. But

          1. Do please try to read and act on the FAQ Advice. Screenshots are specifically advised against there, and I can barely read this one.

          2. It's a detail but round(, .01) never does what most people think. Binary equivalence to multiples of 1/100 is only possible for numbers ending in .50, .25 and .75. This follows from the fact that Stata is using binary at machine level.

          Comment


          • #6
            And .00.

            Comment


            • #7
              Many thanks for your advices. I'll try to change the layout

              Comment


              • #8
                Aha. Just today I was reminded that asclogit is the command I had in mind for modeling the DCE, and you will see the layout I had in mind discussed at some length in the documentation in the Stata Base Reference Manual PDF included in your installation and accessible from Stata's help menu.

                Comment


                • #9
                  Actually, I tried to use asclogit before I selected the gmnl, that i'm using right now. Thanks again and have a good day!

                  Comment

                  Working...
                  X