This simulation creates a vector of eight sounds (1, 2, 3, 4, 5, 6, 7, 8). This vector is repeated 180 times. For each iteration, the order of the 8*180 sounds is randomized. For each iteration, a loop goes through the sounds to identify the repetitions of identical sounds and to count the number of these different repetitions.
First we clear our workspace.Then we run the simulation. Change “NS” to as many simulations as you want.
# Clear environment, graphics and console
rm(list=ls())
graphics.off()
cat('\014')
# Run simulation
set = rep(1:8,180) # eight sounds times 180
end = data.frame(sound = 0:1) # non repeating end
NS = 10 # number of simulations
for (n in 1:NS){ # nr of simulations
# Randomize the sounds
exp = data.frame(sound = sample(set,size = 180*8, replace = FALSE))
# this makes sure there are no repetitions in the end of a experiment
exp = rbind(exp, end)
reps = data.frame(matrix(ncol = 8, nrow = 0))
colnames(reps) = c('s1', 's2', 's3', 's4', 's5', 's6','s7','s8')
indx = 1 # Index
nsame = 1 # counter for number of identical numbers
while (indx != 1442){ # loops through all indices
comparison = 1
while (comparison == 1){ # loops through repetitions of sounds
# compare current index sound with next sound of repetition loop
if (exp$sound[indx] == exp$sound[indx+nsame]){
# if index sound is repeated, add one to repetition counter
nsame = nsame + 1
}else{ # when no more repetitions
comparison = 0
# store any repetitions above one into the last row of corresponding
# index sound
if (nsame > 1){reps[length(reps[,1])+1,exp$sound[indx]] = nsame}
# move index to the sound after amount of repetitions
indx = indx + nsame
nsame = 1 # reset repetition counter
}
}
}
#
for (snd in 1:8){
# make an empty data frame one sound at a time (one row)
template = data.frame(matrix(0, ncol = 12, nrow = 1))
colnames(template) = c('1','2','3','4','5','6','7','8','9','10',
'sound','iteration')
template$sound[1] = paste('s', snd, sep = '') # store current sound
template$iteration[1] = n # store current iteration
# Type of repetitions
nreps = as.integer(names(table(reps[snd])))
# Number of repetitions
template[1,c(nreps)] = as.integer(table(reps[snd]))
if (n == 1 & snd == 1){ # create output with first sound and simulation
output = template
} else {
output = rbind(output, template) # add simulations row by row
}
}
}
tmp = output[2]+output[3]*2+output[4]*3+output[5]*4+output[6]*5+output[7]*6+
output[8]*7+output[9]*8+output[10]*9
output$sum = tmp[,1]
rm(list=setdiff(ls(), c('output')))
Because 10000 simulations take a long time, read in a prepared simulation.
#write.csv(x = output,file = 'output.csv', row.names = F)
library(readr)
output <- read_csv("output.csv")
Calculate mean sound repetitions. Percent of repetitions across all sounds across trials, averaged across all simulations.
d = aggregate(output$sum, list(output$iteration), sum)
colnames(d) = c('iteration', 'sum')
mean(d$sum)/1440*100
## [1] 12.42226
boxplot(output[,2:5],xlab = 'Number of same tones in a row', ylab = 'Frequency',
main = 'Sound order simulation (k=10000)')