Simple Probs

[This article was first published on » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)