# Data latest polls polls = NULL polls <- data.frame( rbind( Opinium = c(43, 47, 1156), Survation = c(44, 48, 1000), ICM = c(41, 45, 1175) )) # set up for decimals polls[, 1:2] <- polls[, 1:2]/100 # weighting polls wtd.polls <- rbind(polls, c(apply(polls[,1:2],2, weighted.mean, polls[,3]), sum(polls[,3]))) names(wtd.polls) <- c("Yes","No","n") wtd.polls$Others = 1-wtd.polls$Yes-wtd.polls$No # Adjusting for the undecideds for the final results wtd.polls[5,] <- data.frame(wtd.polls[4,1:2] + wtd.polls[4,1:2]/ sum(wtd.polls[4,1:2])*wtd.polls[4,4],n=wtd.polls[4,3],Others=0) ################################################## ## If we have more than two categories (Yes, No, and DK), Beta distribution won't work properly. ## The following is a function which I used to draw from a dirichlet distribution. ##The Dirichlet distribution is the multidimensional generalization of the beta distribution. It's the canonical Bayesian distribution for the parameter estimates of a multinomial distribution. ## The Dirichlet function returns a matrix with n rows, each containing a single Dirichlet random deviate. ################################################## row= 5 prob2win = function(row,export=1){ p=rdirichlet(100000, wtd.polls$n[row] * c(wtd.polls$Yes[row], wtd.polls$No[row], 1-wtd.polls$Yes[row]-wtd.polls$No[row])+1 ) if(export==1){ mean(p[,1]