Question

In: Computer Science

Using R Language The code provided below uses a for loop to simulate conducting 10,000 polls...

Using R Language

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") } 2 # 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()

1. Run this code on your own and find the fraction of the simulations in which less than half the people (3 or fewer) support the Democratic candidate. Compare this result to your answer in Question 5 of the previous section.

2. 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.

3. Graph the simulations so you can visualize the distribution.

4. Change the code again to simulate 10,000 polls of 1,000 people. Find the fraction of simulations in which between 55% and 61% 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.

Solutions

Expert Solution

1. (PS I don't have Question number 5 of the previous section)

Source function

USA_Polling <- function(){
poll_sims = vector(length = 10000, mode = "integer")
for (i in 1:10000)
poll = sample(c("Democrat", "Republican"), size = 8, replace = T, prob = c(.58, .42))
poll_sims[i] = sum(poll == "Democrat")
plot(factor(poll_sims))
library(tidyverse)
qplot(factor(poll_sims)) + geom_bar()
z = sum(poll=="Democrat")
return(z)
}

Now Let's call this function for 100 times, by the following function -

runn_poll <- function(){
s = 0;
for (i in 1:100)
{
result = USA_Polling();
if(result<=3)
s = s+1
}
return(s/100)
}

Now ro get the output, add both the files as source and print the result as -

source('~/R/USA_Polling.R')

source('~/R/run_poll.R')

x = runn_poll();

print(x)

Answer to Q2

Change the first function, 4th line -> size = 10 and re-run the code

USA_Polling <- function(){
poll_sims = vector(length = 10000, mode = "integer")
for (i in 1:10000)
poll = sample(c("Democrat", "Republican"), size = 100, replace = T, prob = c(.58, .42))
poll_sims[i] = sum(poll == "Democrat")
plot(factor(poll_sims))
library(tidyverse)
qplot(factor(poll_sims)) + geom_bar()
z = sum(poll=="Democrat")
return(z)
}

I got a resul of 0.1, You may get different result.

Answer to Q3

Get the result of the first attempt in r1, as

r1 = runn_poll();

Get the

result of the second attempt in r2, after changing if(result<50) in runn_poll(), as

r2 = runn_poll();

Now barplot the results as,

H <- c(r1,r2)

barplot(H)

Answer to Q4

Now the size will be 1000, as

USA_Polling <- function(){
poll_sims = vector(length = 10000, mode = "integer")
for (i in 1:10000)
poll = sample(c("Democrat", "Republican"), size = 1000, replace = T, prob = c(.58, .42))
poll_sims[i] = sum(poll == "Democrat")
plot(factor(poll_sims))
library(tidyverse)
qplot(factor(poll_sims)) + geom_bar()
z = sum(poll=="Democrat")
return(z)
}

and we have to change runn_poll as well to demonstrate only the fraction of simulations in which between 55% and 61%

runn_poll <- function(){
s = 0;
for (i in 1:100)
{
result = USA_Polling();
if(result>550&&result<610) #as there are 1000 people
s = s+1
}
return(s/100)
}


Related Solutions

Using C++ language, create a program that uses a struct with array variables that will loop...
Using C++ language, create a program that uses a struct with array variables that will loop at least 3 times and get the below information: First Name Last Name Job Title Employee Number Hours Worked Hourly Wage Number of Deductions Claimed Then, determine if the person is entitled to overtime and gross pay. Afterwards, determine the tax and net pay. Output everything to the screen. Use functions wherever possible. Bonus Points: Use an input file to read in an unknown...
Please code in Assembly Language Code solution using the provided template AL_Template_Irvine32.asm located towards the bottom...
Please code in Assembly Language Code solution using the provided template AL_Template_Irvine32.asm located towards the bottom of the question.. Debug programs with Visual Studio2017/19. Please add single line or block comments explaining the purpose or functionality of your code statements. 10.) Fibonacci Generator Write a procedure that produces N values in the Fibonacci number series and stores them in an array of doubleword. Input parameters should be a pointer to an array of doubleword, a counter of the number of...
Give a method to simulate BN(4,5,16,25,0.67) using cholesky factorization .Using R simulate this BN when n1=...
Give a method to simulate BN(4,5,16,25,0.67) using cholesky factorization .Using R simulate this BN when n1= 50 and n2= 50 and draw scatter diagram of (x1 ,x2) Estimate m1,m2 and sample corr(X1,X2) .Give regression line of X2 and X1 and also draw this line using method of least squares using R. Comment on your results.
Consider R language definitions using resources provided in the study materials. Define the term data structures...
Consider R language definitions using resources provided in the study materials. Define the term data structures or objects in R and discuss several examples (e.g., vectors, lists, language objects, symbol objects, expression objects, function objects, NULL, etc.). All R objects have attributes. Purpose of attributes?
Write a python code which prints triangle of stars using a loop ( for loop )...
Write a python code which prints triangle of stars using a loop ( for loop ) Remember what 5 * "*" does The number of lines of output should be determined by the user. For example, if the user enters 3, your output should be: * ** *** If the user enters 6, the output should be: * ** *** **** ***** ****** You do NOT need to check for valid input in this program. You may assume the user...
Given the below pseudocode, write the proper code that implements it using MARIE's assembly language:               ...
Given the below pseudocode, write the proper code that implements it using MARIE's assembly language:                    Input a number and store it in X; if X > 1 then    Y := X + X;    X := 0; endif; Y := Y + 1; Output the value of Y; N.B: You should include the MARIE code in your Answer, with an explanation of each instruction in your code beside it. Example:              Subt One         /Subtract 1 from AC Add a...
Answer IN R CODE to get the following. Using the data below, Create a scatterplot of...
Answer IN R CODE to get the following. Using the data below, Create a scatterplot of y vs x Fit a simple linear regression model using y as the response and plot the regression line (with the data) Test whether x is a significant predictor and create a 95% CI around the slope coefficient. Report and interpret the coefficient of determination. For x=20, create a CI for E(Y|X=20). For x=150, can you use the model to estimate E(Y|X=150)? Discuss. Does...
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting...
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting clc, clear, format compact; %define variables k=1; b=-2; x=-1; y=-2; %while loop initialization for k <= 3 disp([num2str(k), ' ',num2str(b),' ',num2str(x),' ',num2str(y),]); y = x^2 -3; if y< b b = y; end x = x+1; k = k+1; end
R Exercise 1. (a) Simulate the rolling of two dice 10,000 times. (b) Identify which rolls...
R Exercise 1. (a) Simulate the rolling of two dice 10,000 times. (b) Identify which rolls of the dice are in the event A, the dice add up to a perfect square (4 or 9). Determine what proportion of the 10,000 rolls are in A. (c) Identify which rolls of the dice are in the event B, the dice add up to an even number. Determine what proportion of the 10,000 rolls are in B. (d) Find out which rolls...
(This is for java) I need to rewrite this code that uses a while loop. public...
(This is for java) I need to rewrite this code that uses a while loop. public class Practice6 {      public static void main (String [] args) {         int sum = 2, i=2;        do { sum *= 6;    i++;    } while (i < 20); System.out.println("Total is: " + sum); }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT