## simluation ############################################################ pingpong <- get.ping.pong.ball reset.weights() # create a non-weighted function to compare pingpong.nonweighted <- function () { return(sample(member.names, 1)) } # function to run a simulation and generate 1000 classes run.simulation <- function (pingpong.fun, questions=10) { d <- c() # run the comand 1000 times for (i in seq(1,1000)) { all.prev <- c() reset.weights() for (j in seq(1,questions)) { selected <- pingpong.fun() all.prev <- append(all.prev, selected) } d <- append(d, table(all.prev)) missing.names <- names(w)[sapply(names(w), function (x) {!x %in% names(table(all.prev))})] d.tmp <- rep(0, length(missing.names)) names(d.tmp) <- missing.names d <- append(d, d.tmp) } total.nums <- sapply(names(w), function (x) {sum(d[names(d) == x])}) cat("Number of total selections:\n") print(total.nums) cat(paste("SD:", sd(total.nums), "\n")) return(d) } d.nw <- run.simulation(pingpong.nonweighted, 15) d.w <- run.simulation(pingpong, 15) library(ggplot2) graph.sim.output <- function (d, label.text="") { qplot(as.factor(names(table(d))), as.integer(table(d)), geom="bar") + scale_x_discrete("Number of questions asked to participant") + scale_y_continuous("Number of occurances") + opts(title = paste(label.text, "- 1000 classes of 15 questions")) } p.weighted <- graph.sim.output(d.w, "weighted") p.nonweighted <- graph.sim.output(d.nw, "non-weighted") png("simulation_weighted.png") print(p.weighted) dev.off() png("simulation_unweighted_random.png") p.nonweighted dev.off()