Announcement

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

  • Running Mann-Whitney test on averages over 20 periods for a repeated experiment

    • Dear all,

      I am running Mann-Whitney tests on data from 6 different treatments of an economic experiment (panel data): several groups of 4 subjects per treatment over 20 periods.

      As performing the test on each observation will give me dependence problems, I would like to run it on the average values of each subject for the 20 periods, and even on the average values of each group.

      At the moment, to run the test for the individual profit (ex. between treatments 1 and 4) over the whole sample I am using the command:

      ranksum profit if Newtreatment==1 | Newtreatment==4, by (Newtreatment)

      Can anybody tell me how to do for the average values?

      I have created a new variable calculating the averages, but I would like to have the test only on one value per subject or per group and not on the 20 identical values of the 20 periods.

      Many thanks in advance,

      Stef

  • #2
    Use the bysort command and the pseudo variable _n to just use one observation per subject:

    Code:
    bysort subject: ranksum YourAverageVariable of inlist(Newtreatment, 1, 4) &  (_n == 1), by(Newtreatment)  // inlist() is easier, too.

    Comment


    • #3
      That should, of course, be *if* inlist(), not *of*. Sorry for the typo.

      Comment


      • #4
        Dear Mike,
        Many thanks for it. I used the code you suggested and I get the following error:
        -------------------------------------------------------------------------------------------------------------------------------------------
        -> Newplayername = leem1 10
        no observations

        -------------------------------------------------------------------------------------------------------------------------------------------
        -> Newplayername = leem1 11
        no observations

        and so on for the other subjects.


        Also, when I try to compare certain treatments, such as 5 and 6, I get the following:

        -> Newplayername = leem1 10
        1 group found, 2 required
        r(499);


        I have tried to send you my dataset, but no way of attaching it to the post (no .dta, no .txt, and also I cannot upload as URL as in all case it does not recognise it as an image file...). So I have uploaded the file on Google Drive and here below is the URL to access it.


        https://drive.google.com/file/d/1vSf...ew?usp=sharing


        The code you suggested, adapted to my data, becomes:

        "bysort Newplayername: ranksum mean_profit_player if inlist(Newtreatment, 1, 4) & (_n == 1), by(Newtreatment)"


        I really thank you in advance for your help.


        Best,

        Stef

        Comment


        • #5
          The link that allows modifying the file:

          https://drive.google.com/file/d/1vSf...ew?usp=sharing

          Comment


          • #6
            It's impossible to diagnose your problem from seeing a tiny snippet of your code. You didn't tell us anything about using "Newplayername." Further, I and most people here won't open someone else's file in the way that you ask, as that's a dangerous practice. Please read the FAQ (as all new members are asked to do), and it will give you information regarding how to post code and sample data on StataList. You can then post the relevant material again and we should be able figure out what is going on.

            Comment


            • #7
              Thank you Mike and apologies for the dataset: as you can see I am new in the forum and not yet 'expert'. Hopefully I will get better with the time.

              I then used the Dataexe command in Stata to have an example of my dataset, here below. Not to exceed the allowed size, I have taken away several variables, but I have left those involved in the test.

              Particularly, "Newplayername" is the identifier of each subject in the experiment, and combines the playername given in the laboratory (here "leem 1") and the session in which the subject takes part. So "leem1 10" is the Newplayername of subject "leem 1" participating in session "10".

              There are only 100 observations, I can send more if needed.

              Again many thanks.

              Stef

              input str1 Groupe byte(profit round) str6 playername str2 Session str9 Newplayername str4 Newgroupe float(mean_profit_player Newtreatment)
              "1" 2 19 "leem1" "10" "leem1 10" "1 10" 1 5
              "1" -2 16 "leem1" "10" "leem1 10" "1 10" 1 5
              "1" -2 18 "leem1" "10" "leem1 10" "1 10" 1 5
              "1" -2 11 "leem1" "10" "leem1 10" "1 10" 1 5
              "1" -3 17 "leem1" "10" "leem1 10" "1 10" 1 5
              "1" 3 14 "leem1" "10" "leem1 10" "1 10" 1 5
              "1" 2 6 "leem1" "10" "leem1 10" "1 10" 1 5
              "1" 3 13 "leem1" "10" "leem1 10" "1 10" 1 5
              "1" 3 12 "leem1" "10" "leem1 10" "1 10" 1 5
              "1" -4 20 "leem1" "10" "leem1 10" "1 10" 1 5
              "1" 3 15 "leem1" "10" "leem1 10" "1 10" 1 5
              "1" 3 10 "leem1" "10" "leem1 10" "1 10" 1 5
              "1" 3 7 "leem1" "10" "leem1 10" "1 10" 1 5
              "1" 3 8 "leem1" "10" "leem1 10" "1 10" 1 5
              "1" 3 9 "leem1" "10" "leem1 10" "1 10" 1 5
              "1" 0 7 "leem1" "11" "leem1 11" "1 11" .8666667 5
              "1" 1 17 "leem1" "11" "leem1 11" "1 11" .8666667 5
              "1" 1 16 "leem1" "11" "leem1 11" "1 11" .8666667 5
              "1" 1 19 "leem1" "11" "leem1 11" "1 11" .8666667 5
              "1" 1 20 "leem1" "11" "leem1 11" "1 11" .8666667 5
              "1" 1 18 "leem1" "11" "leem1 11" "1 11" .8666667 5
              "1" 1 11 "leem1" "11" "leem1 11" "1 11" .8666667 5
              "1" 1 14 "leem1" "11" "leem1 11" "1 11" .8666667 5
              "1" 1 10 "leem1" "11" "leem1 11" "1 11" .8666667 5
              "1" 1 9 "leem1" "11" "leem1 11" "1 11" .8666667 5
              "1" 0 8 "leem1" "11" "leem1 11" "1 11" .8666667 5
              "1" 1 15 "leem1" "11" "leem1 11" "1 11" .8666667 5
              "1" 1 6 "leem1" "11" "leem1 11" "1 11" .8666667 5
              "1" 1 13 "leem1" "11" "leem1 11" "1 11" .8666667 5
              "1" 1 12 "leem1" "11" "leem1 11" "1 11" .8666667 5
              "1" 3 18 "leem1" "6" "leem1 6" "1 6" .6666667 3
              "1" 2 17 "leem1" "6" "leem1 6" "1 6" .6666667 3
              "1" 2 16 "leem1" "6" "leem1 6" "1 6" .6666667 3
              "1" 3 19 "leem1" "6" "leem1 6" "1 6" .6666667 3
              "1" -2 10 "leem1" "6" "leem1 6" "1 6" .6666667 3
              "1" -2 14 "leem1" "6" "leem1 6" "1 6" .6666667 3
              "1" -1 13 "leem1" "6" "leem1 6" "1 6" .6666667 3
              "1" -2 20 "leem1" "6" "leem1 6" "1 6" .6666667 3
              "1" -3 11 "leem1" "6" "leem1 6" "1 6" .6666667 3
              "1" 3 9 "leem1" "6" "leem1 6" "1 6" .6666667 3
              "1" 3 7 "leem1" "6" "leem1 6" "1 6" .6666667 3
              "1" 2 15 "leem1" "6" "leem1 6" "1 6" .6666667 3
              "1" -2 12 "leem1" "6" "leem1 6" "1 6" .6666667 3
              "1" 1 6 "leem1" "6" "leem1 6" "1 6" .6666667 3
              "1" 3 8 "leem1" "6" "leem1 6" "1 6" .6666667 3
              "1" 2 20 "leem1" "7" "leem1 7" "1 7" .26666668 4
              "1" -2 18 "leem1" "7" "leem1 7" "1 7" .26666668 4
              "1" 1 19 "leem1" "7" "leem1 7" "1 7" .26666668 4
              "1" -2 8 "leem1" "7" "leem1 7" "1 7" .26666668 4
              "1" -3 7 "leem1" "7" "leem1 7" "1 7" .26666668 4
              "1" 0 11 "leem1" "7" "leem1 7" "1 7" .26666668 4
              "1" 1 17 "leem1" "7" "leem1 7" "1 7" .26666668 4
              "1" 1 6 "leem1" "7" "leem1 7" "1 7" .26666668 4
              "1" 1 16 "leem1" "7" "leem1 7" "1 7" .26666668 4
              "1" 0 13 "leem1" "7" "leem1 7" "1 7" .26666668 4
              "1" 2 12 "leem1" "7" "leem1 7" "1 7" .26666668 4
              "1" 1 10 "leem1" "7" "leem1 7" "1 7" .26666668 4
              "1" 0 14 "leem1" "7" "leem1 7" "1 7" .26666668 4
              "1" 0 9 "leem1" "7" "leem1 7" "1 7" .26666668 4
              "1" 2 15 "leem1" "7" "leem1 7" "1 7" .26666668 4
              "1" 2 12 "leem1" "8" "leem1 8" "1 8" 2.2666667 3
              "1" 2 18 "leem1" "8" "leem1 8" "1 8" 2.2666667 3
              "1" 2 10 "leem1" "8" "leem1 8" "1 8" 2.2666667 3
              "1" 2 8 "leem1" "8" "leem1 8" "1 8" 2.2666667 3
              "1" 2 9 "leem1" "8" "leem1 8" "1 8" 2.2666667 3
              "1" 2 14 "leem1" "8" "leem1 8" "1 8" 2.2666667 3
              "1" 4 15 "leem1" "8" "leem1 8" "1 8" 2.2666667 3
              "1" 2 7 "leem1" "8" "leem1 8" "1 8" 2.2666667 3
              "1" 2 16 "leem1" "8" "leem1 8" "1 8" 2.2666667 3
              "1" 2 17 "leem1" "8" "leem1 8" "1 8" 2.2666667 3
              "1" 2 6 "leem1" "8" "leem1 8" "1 8" 2.2666667 3
              "1" 2 13 "leem1" "8" "leem1 8" "1 8" 2.2666667 3
              "1" 4 20 "leem1" "8" "leem1 8" "1 8" 2.2666667 3
              "1" 2 19 "leem1" "8" "leem1 8" "1 8" 2.2666667 3
              "1" 2 11 "leem1" "8" "leem1 8" "1 8" 2.2666667 3
              "1" 3 20 "leem1" "9" "leem1 9" "1 9" 2.2 4
              "1" 3 18 "leem1" "9" "leem1 9" "1 9" 2.2 4
              "1" 3 16 "leem1" "9" "leem1 9" "1 9" 2.2 4
              "1" 3 19 "leem1" "9" "leem1 9" "1 9" 2.2 4
              "1" 2 14 "leem1" "9" "leem1 9" "1 9" 2.2 4
              "1" 2 9 "leem1" "9" "leem1 9" "1 9" 2.2 4
              "1" 3 12 "leem1" "9" "leem1 9" "1 9" 2.2 4
              "1" 1 11 "leem1" "9" "leem1 9" "1 9" 2.2 4
              "1" 3 15 "leem1" "9" "leem1 9" "1 9" 2.2 4
              "1" 2 7 "leem1" "9" "leem1 9" "1 9" 2.2 4
              "1" 2 10 "leem1" "9" "leem1 9" "1 9" 2.2 4
              "1" 2 17 "leem1" "9" "leem1 9" "1 9" 2.2 4
              "1" 2 8 "leem1" "9" "leem1 9" "1 9" 2.2 4
              "1" 1 13 "leem1" "9" "leem1 9" "1 9" 2.2 4
              "1" 1 6 "leem1" "9" "leem1 9" "1 9" 2.2 4
              "2" 2 10 "leem10" "10" "leem10 10" "2 10" 1.0666667 5
              "2" 2 9 "leem10" "10" "leem10 10" "2 10" 1.0666667 5
              "2" 1 18 "leem10" "10" "leem10 10" "2 10" 1.0666667 5
              "2" 1 6 "leem10" "10" "leem10 10" "2 10" 1.0666667 5
              "2" 2 20 "leem10" "10" "leem10 10" "2 10" 1.0666667 5
              "2" -4 17 "leem10" "10" "leem10 10" "2 10" 1.0666667 5
              "2" 2 19 "leem10" "10" "leem10 10" "2 10" 1.0666667 5
              "2" 2 14 "leem10" "10" "leem10 10" "2 10" 1.0666667 5
              "2" 2 16 "leem10" "10" "leem10 10" "2 10" 1.0666667 5
              "2" 2 15 "leem10" "10" "leem10 10" "2 10" 1.0666667 5
              end
              [/CODE]

              Comment


              • #8
                We need to see *exactly* what commands you gave to Stata up to where the error occurred, and exactly what Stata's response was. (That's what I was indicating by "code.") That's easier and more definitive to work with than a description of what you did. The best thing to do is to copy and paste from your results window in Stata.

                Comment


                • #9
                  OK, this is the code from the very beginning, including the generation of my variables:


                  . use "D:\Faro\Stata files\ExpeComMod\Data sessions 1 to 13.dta"

                  . do "C:\Users\farolfi\AppData\Local\Temp\STD1650_00000 0.tmp"

                  . * Generation of variables
                  . * create Newplayername
                  . tostring session, generate(Session)
                  Session generated as str2

                  . gen Newplayername = playername + " " + Session

                  .
                  . *create Newgroupe
                  . tostring groupe, generate(Groupe)
                  Groupe generated as str1

                  . gen Newgroupe = Groupe + " " + Session

                  .
                  end of do-file

                  . do "C:\Users\farolfi\AppData\Local\Temp\STD1650_00000 0.tmp"

                  . * Mean Profit and Investment for agent
                  . egen mean_profit_player = mean( profit ), by ( Newplayername )

                  . egen mean_investment_player = mean( investment ), by ( Newplayername )

                  . *ACTIVITY numeric from activity string
                  . encode activity, generate(ACTIVITY)

                  . * Dummies for treatments
                  . generate Puzzle = traitement == "Puzzle"

                  . generate Model = traitement == "Model"

                  . generate Context = with_context == "True"

                  . generate Rien = traitement == "Rien"

                  .
                  . gen Newtreatment = 1

                  . replace Newtreatment = 2 if Model == 0 & Context == 1
                  (1,260 real changes made)

                  . replace Newtreatment = 3 if Model == 1 & Context == 0
                  (600 real changes made)

                  . replace Newtreatment = 4 if Model == 0 & Context == 0
                  (1,020 real changes made)

                  . replace Newtreatment = 5 if Rien == 1 & Context == 0
                  (540 real changes made)

                  . replace Newtreatment = 6 if Rien == 1 & Context == 1
                  (540 real changes made)

                  .
                  end of do-file

                  . bysort Newplayername: ranksum mean_profit_player if inlist(Newtreatment, 1, 4) & (_n == 1), by(Newtreatment)

                  -------------------------------------------------------------------------------------------------------------------------------------------
                  -> Newplayername = leem1 10
                  no observations

                  -------------------------------------------------------------------------------------------------------------------------------------------
                  -> Newplayername = leem1 11
                  no observations

                  -------------------------------------------------------------------------------------------------------------------------------------------
                  -> Newplayername = leem1 6
                  no observations

                  ".
                  .
                  .
                  Etc. for the other subjects..."

                  Comment


                  • #10
                    I can see now that my guesses about the structure of your data were quite wrong and my original advice was consequently pretty bad. This is why presenting concrete data examples and code is so important when asking questions. I think you need:
                    Code:
                    egen just1 = tag(Newplayername)
                    ranksum mean_profit_player if (just1 ==1) &  inlist(Newtreatment,3,4), by(Newtreatment)
                    To constrast other treatment combinations,such as 1 vs. 5, use this command with appropriately chosen combinations of treatment codes, such as:
                    Code:
                    ranksum mean_profit_player if (just1 ==1) &  inlist(Newtreatment, 1,5), by(Newtreatment)
                    A broader comment: You are doing various odd things with your data that indicate you have some deeper misunderstandings that go quite beyond what can be addressed here on StataList. For example, you are making string variables out of numeric ones when there is no reason to do so and which in fact makes things harder for you. Except for things like names and addresses, one almost never wants to use string variables in a program like Stata. I think you would benefit from reading about "value labels" in Stata, as I'm guessing that you want to use strings to better identify the meanings of variables' values, which is *not* best done by using string-valued variables. This is just one oddity that I see; there are various others.
                    Another piece of advice would be to choose variable names very carefully, as it makes it easier for you and other people to think about your data. So, for example, while Stata won't care, human beings will be confused by your variable "Newplayername," which is not a player name at all.

                    Without intending offense, I'd say that this is as far as I'm willing to go with your question, as the data and code are sufficiently messy as to reduce my interest.

                    Comment


                    • #11
                      Thank you Mike for your time and great help. And also for the suggestions. Works perfectly!
                      No more need on this question.
                      And I have learned a lot on how to interact in the forum (was my initiation...).
                      Also, I will work my datasets and next time I will bring a much less 'odd' one. Promised.
                      Best,
                      Stef

                      Comment

                      Working...
                      X