Simple Probs

October 24, 2014
By

(This article was first published on » R, and kindly contributed to R-bloggers)

Crystal-Ball Somebody said me that it’d be really nice to see a posting with simple simulations for the runoff this weekend. Answering such a call, this is the best I could come up with. The following is a highly simplified simulation that does not account for time trends nor for house effects. But it’s still theoretically sound as it takes a vector of polls, instead of single polls individually.

# collect some of the latest polls 
dilma <- c(53,54, 52,46.7,52, 50.5, 43.6)

# Set the estimated percent for Dilma
# based on the average of several national polls
propDilma = mean(dilma)

# Set the standard deviation
# this measures the variability between the different polls.
sdDilma = sd(dilma)

# Function to simulate a single election
simElection <- function(prop,sd){
  return(rnorm(1,mean=prop,sd=sd))
}

# Simulate the percent Dilma in 1000 elections
simPropDilma = replicate(1000, simElection(propDilma,sdDilma))

# Calculate the percent of times Dilma wins
perDilmaWin = mean(simPropDilma > 50)
perDilmaWin
[1] 0.517 

hist(simPropDilma, freq=FALSE, xlab="Popular Vote", 
     main="Distribution of Dilma's Popular Vote",
     col="red", xlim=c(30,70),  ylim=c(0, .15) )

curve(dnorm(x, mean=mean(simPropDilma), sd=sd(simPropDilma)), add=TRUE, col="blue", lwd=2) 

Hist_Dilma

To leave a comment for the author, please follow the link and comment on their blog: » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)