Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How random is my shuffle function?

Tags:

random

r

I am programming a psychology experiment, I need to shuffle the order of stimuli for each participant. I have a function which randomly orders my stimuli, which my program then reads out of a .txt file. Does the pseudo-random algorithm which is used by default in sample (as shown in my "shuffle" function below) shuffle things up adequately to realistically be expected not to produce any systematic bias in any stimulus position or pattern of stimulus positions over the course of the experiment (4500 trials)?

stimulus <- c("a", "b", "c", "d", "e")
shuffle <- function (x) { as.data.frame(sample((t(x)))) } 
shuffle (stimulus)
like image 800
luke123 Avatar asked Sep 10 '26 21:09

luke123


2 Answers

I'd say yes, and you can graph this. If it were truly random we would expect a uniform distribution of values at each position in the shuffled order, so let's repeat the experiment a lot and graph the results....

#  Repeat experiment 10,000 times
res <- replicate( 10000 , shuffle(stimulus) )
out <- do.call( rbind , res )

#  Plot
par( mfrow = c( 3 , 2 ) )
for( i in 1:ncol(out)){
  hist( out[,i] , main = paste0("Values at position: " , i ) )
}

Each histogram is the distribution of values in each position. 5 positions so 5 histograms. There is an even distribution of the possible values at each location so I'd say your values are being assigned to each position with an even probability (which is the default for sample). enter image description here

like image 144
Simon O'Hanlon Avatar answered Sep 13 '26 16:09

Simon O'Hanlon


The random number generators in R are excellent - the language is aimed at statisticians. A couple of points.

  1. See ?RNG for details about the random number generators used.

  2. Use set.seed to make your shuffling reproducible

    set.seed(1)
    
  3. You could simplify your code to:

    stimulus = c("a", "b", "c", "d", "e")
    data.frame(sh=sample(stimulus))
    
like image 27
csgillespie Avatar answered Sep 13 '26 16:09

csgillespie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!