In my data, each participant has obtained a score (SC1) in a game. I want to know for a participant with certain characteristics (female with a score of 10) what their probabilities are of having the highest score in a group of 4. In my case, the participant with id==13 has these characteristics. I'm using forvalues to create 1000 groups and calculate how often the person with id==13 has the highest score. I want to create groups with the id==13 person and 3 others. To ensure that Stata does not take the id==13 person as one of the three others, I assign this person rannum 1 (the highest possible). I have created the following code:
forvalues i=1/1000 {
set seed 37025
generate rannum`i' = runiform()
replace rannum`i' = 1 if id==13 //Assign the highest random number to person id==13 to avoid them having one of the three lowest numbers
sort rannum`i'
generate grp`i' = .
replace grp`i' = 1 in 1/3 //Assign 3 people with the lowest random number to the group
replace grp`i' = 1 if id==13 //Assign person with id==13 to the group
bysort grp`i': egen rnk`i'=rank(SC1) if grp`i'==1, field. //Give every person in the group a performance rank from 1 - 4
generate win`i' = .
replace win`i' = 1 if rnk`i'==1 //Generate a dummy that is 1 if the participant obtained rank 1
}
egen wins = rowtotal(win*) if id==13 //Calculate how often person with id==13 obtained rank 1
The issue that I encounter is that my results are not reproducible. I thought this would be avoided by using the 'set seed' command and sorting the data. Still, every time I run the commands I get a different value for the total amount of wins.
Can someone spot why my results are not reproducible and what I can do about it?
Related Posts with Loop results creating random groups are not reproducible
Marginsplot legend labeling with plotoptsDear all, I'm want to plot an an interaction effect, with labels in the legend. Thus, I'm using thi…
What is Stata's full algorithm for automatically labelling axes on plots?Hi all, For reasons that aren't really worth going into, I'm trying to reproduce the algorithm that…
Calculating average of observationsconditional to Id and yearHello I have a fairly tricky question which might either have a very simple solution I fail to see o…
Graph bar axis and labels issuesI am trying to create a graphbar using Code: graph bar (percent) if account_formal==1, /// over(fe…
Generating columns for each unique value within a groupHello, I have monthly individual program data with the following variables: individual identifiers …
Subscribe to:
Post Comments (Atom)
0 Response to Loop results creating random groups are not reproducible
Post a Comment