Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The code provided below uses a for loop to simulate conducting 10,000 polls of 8 people in which each person has 58% probability of being
The code provided below uses a for loop to simulate conducting 10,000 polls of 8 people in which each person has 58% probability of being a supporter of the Democratic candidate and a 42% probability of being a supporter of the Republican. The way the loop works is it runs through the code inside the loop 10,000 times, but changing the value of i with each iteration (i is 1 in the first iteration, 10,000 in the last).
# Define a vector of integers that has 10,000 elements. poll_sims = vector(length = 10000, mode = "integer") # for loop to simulate 10,000 polls for (i in 1:10000) { # Do a poll of 8 people in which each person has a 58% chance of supporting the # Democratic candidate and 42% chance of supporting the Republican. poll = sample(c("Democrat", "Republican"), size = 8, replace = T, prob = c(.58, .42)) # Count the number of people who support the Democrat and store the result in the # poll_sims vector as the ith result. poll_sims[i] = sum(poll == "Democrat")
}
# Visualise the poll_sims vector using basic R plot(factor(poll_sims)) # Visualise the poll_sims vector using tidyverse library(tidyverse) qplot(factor(poll_sims)) + geom_bar()
- Modify the code above to run simulations of the situation from problem (5) above (where 46% support the Democrat and 54% the Republican). Find the fraction of the simulations in which less than half the people (3 or fewer) support the Republican candidate. Compare this result to your answer in Question 5 of the previous section.
- Change the code to simulate 10,000 polls of 100 people (rather than 10,000 polls of 8). Find the fraction of simulations in which less than half the people support the Democratic candidate. In other words, use the simulations to approximate the likelihood that a poll of 100 people will incorrectly guess the winner of the election.
- Graph the simulations so you can visualize the distribution.
- Change the code again to simulate 10,000 polls of 1,000 people. Find the fraction of simulations in which between 43% and 49% of the people support the Democratic candidate. In other words, use the simulations to approximate the likelihood that a poll of 1,000 people will be off from the true probability by 3% or less.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started