Exercise 4. Average size of error
The error \(p-\bar{X}\) is a random variable. In practice, the error is not observed because we do not know the actual proportion of Democratic voters, \(p\). However, we can describe the size of the error by constructing a simulation.
What is the average size of the error if we define the size by taking the absolute value \(\mid p-\bar{X}\mid\) ?
This exercise is part of the course
HarvardX Data Science Module 4 - Inference and Modeling
Exercise instructions
- Use the sample code to generate
errors
, a vector of \(\mid p-\bar{X}\mid\). - Calculate the absolute value of
errors
using theabs
function. - Calculate the average of these values using the
mean
function.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Define `p` as the proportion of Democrats in the population being polled
p <- 0.45
# Define `N` as the number of people polled
N <- 100
# The variable `B` specifies the number of times we want the sample to be replicated
B <- 10000
# Use the `set.seed` function to make sure your answer matches the expected result after random sampling
set.seed(1)
# We generated `errors` by subtracting the estimate from the actual proportion of Democratic voters
errors <- replicate(B, p - take_sample(p, N))
# Calculate the mean of the absolute value of each simulated error. Print this value to the console.