Announcement

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

  • Generate a new variable that contains a value under a probability

    Hi everyone,

    I have data with five variables: decision, x1, p1, x2, and p2. The decision is an option that someone can choose that contains information on whether this person could get x1 or x2 under p1 p2 probability.
    For example, a person chooses decision one where he could get either x1 (5) with a probability of 0.7 or x2 (10) with a probability of 0.3. How to generate a new variable that reveals the value between x1 and x2 under that probability? Could anyone help me with what command I can use or what the code maybe is?

    Thank you for helping me.

  • #2
    The expected value is the weighted average:

    Code:
    gen wanted= x1*p1 + x2*p2

    Comment


    • #3
      Sorry sir, what I actually mean is how the new variable could generate a value between x1 and x2 under those probabilities. For instance, after I choose that decision under x1 (5) p1 (0.7) or x2 (10) p2 (0.3), the value I got is x2, that is 10. I want to store that information on the new variable. I really appreciate any help you can provide.

      Comment


      • #4
        I have to guess the structure of your data, but here is a way using the -cond()- function.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input float(x1 p1 x2 p2) str5 decision
        5 .7 10 .3 "x2"
        3 .5  5 .5 "x1"
        2 .1  7 .9 "x2"
        end
        
        gen wanted= cond(decision=="x1", x1, cond(decision== "x2", x2, .))
        Res.:

        Code:
        . l
        
             +---------------------------------------+
             | x1   p1   x2   p2   decision   wanted |
             |---------------------------------------|
          1. |  5   .7   10   .3         x2       10 |
          2. |  3   .5    5   .5         x1        3 |
          3. |  2   .1    7   .9         x2        7 |
             +---------------------------------------+

        If this is not it, provide a data example as above showing the structure of your data and your wanted variable.
        Last edited by Andrew Musau; 30 Apr 2022, 10:41.

        Comment


        • #5
          My apology for the unclear explanation again. This is the data example:
          Click image for larger version

Name:	Untitled.png
Views:	1
Size:	6.5 KB
ID:	1662406


          Assume I ran an experiment in which John participated. The interface shows three alternatives that John can choose (showed by how many his allocations to the alternatives). In alternative 1, John could get either money of $10 with a probability of 0.3 or $15 with a probability of 0.7, and he decided to allocate one of his tokens to this alternative. However, John still doesn't know how much money he would get (10 or 15) since there are probabilities within those two values.

          From that situation, I want Stata to reveal what John would get in Alternative 1 ($10 or $15) under the probability of those two values since he decided to allocate one of his tokens to this alternative. However, if he did not decide to allocate his token (like in Alternative 3), the column "reveal" in Row 3 would not be shown. Based on that condition, I still don't know how to generate this 'reveal' variable. Please help me. Thank you.
          Last edited by Muhammad Rafi Prakoso; 30 Apr 2022, 21:01.

          Comment


          • #6
            Do you just need a textual description? If not, fill exactly what should be entered for each observation of "reveal".

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input str20(participant decision) float(x1 p1 x2 p2 allocation) str5
            "John" "Alternative 1" 10 .3 15 .7 1
            "John" "Alternative 2" 15 .5 20 .5 1
            "John" "Alternative 3" 20 .8 25 .2 0
            end
            
            gen reveal = "("+ string(x1)+ " [p="+ string(p1)+"] or "+ string(x2)+" [p=" +string(p2)+"])"  if allocation
            Res.:

            Code:
            . l
            
                 +------------------------------------------------------------------------------------+
                 | partic~t        decision   x1   p1   x2   p2   alloca~n                     reveal |
                 |------------------------------------------------------------------------------------|
              1. |     John   Alternative 1   10   .3   15   .7          1   (10 [p=.3] or 15 [p=.7]) |
              2. |     John   Alternative 2   15   .5   20   .5          1   (15 [p=.5] or 20 [p=.5]) |
              3. |     John   Alternative 3   20   .8   25   .2          0                            |
                 +------------------------------------------------------------------------------------+

            Comment


            • #7
              According to my lecturer, the reveal variable should consist of a number drawn from the probability, meaning that if John chooses Alternative 1, then the reveal column should give information of either 10 or 15 after Stata computes the probability between x1, that is 10 with probability 0.3 and x2, that is 15 with probability 0.7. For example, if Stata computes the value drawn from the probabilistic choice is x1, then the revealed variable in Row 1 would show number 10, that is x1 itself (I mean in integer format).

              I already try with this code but it didn't work, the reveal column only shows 1 and 0 value, not the x1 or x2: gen reveal = x1*rbinomial(1, p1) | x2*rbinomial(1, p2) if allocation > 0. I don't know what the problem is with my code.
              Last edited by Muhammad Rafi Prakoso; 30 Apr 2022, 22:11.

              Comment


              • #8
                EDITED: You want Stata to choose one of the options. One way would be to generate a uniform random variable between 0 and 1 and if the value falls below p1 or is equal to p1, choose x1, otherwise x2. Do not set seed when doing this, I only do it so that my code generates the same results if you run it.

                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input str20(participant decision) float(x1 p1 x2 p2 allocation) str5
                "John" "Alternative 1" 10 .3 15 .7 1
                "John" "Alternative 2" 15 .5 20 .5 1
                "John" "Alternative 3" 20 .8 25 .2 0
                end
                
                set seed 1234
                gen rand= runiform()
                gen wanted= cond(rand<=p1, x1, x2) if allocation
                Res.:

                Code:
                . l
                
                     +-----------------------------------------------------------------------------+
                     | partic~t        decision   x1   p1   x2   p2   alloca~n       rand   wanted |
                     |-----------------------------------------------------------------------------|
                  1. |     John   Alternative 1   10   .3   15   .7          1   .9472316       15 |
                  2. |     John   Alternative 2   15   .5   20   .5          1   .0522234       15 |
                  3. |     John   Alternative 3   20   .8   25   .2          0   .9743183        . |
                     +-----------------------------------------------------------------------------+
                Last edited by Andrew Musau; 30 Apr 2022, 22:41.

                Comment


                • #9
                  Dear Mr. Andrew, thank you so much for your responses and I am really sorry if my explanation is less clear. My question is already well answered and your reply really helps me a lot. I appreciate it so much.

                  Comment

                  Working...
                  X