[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.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
# 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)
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.