library(tidyverse)
library(openintro)
library(tidyverse)
library(openintro)
library(infer)
library(DATA606)

Exercise 1

  1. Describe the distribution of responses in this sample. How does it compare to the distribution of responses in the population. Hint: Although the sample_n function takes a random sample of observations (i.e. rows) from the dataset, you can still refer to the variables in the dataset with the same names. Code you presented earlier for visualizing and summarising the population data will still be useful for the sample, however be careful to not label your proportion p since you’re now calculating a sample statistic, not a population parameters. You can customize the label of the statistics to indicate that it comes from the sample.

Since we are sampling proportions, we use the variable p-hat for the proportion sample, rather than p for proportion which refers to the population as a whole. The distribution of these proportions should be approximately normal for sufficient n

Exercise 2

  1. Would you expect the sample proportion to match the sample proportion of another student’s sample? Why, or why not? If the answer is no, would you expect the proportions to be somewhat different or very different? Ask a student team to confirm your answer.

I believe no. This is because the 50 people in samp1 are randomly selected so the sample mean should be slightly different for each iteration. If we re-run sampl chunk to choose 50 above: 1st time p_hat was .16, 2nd time was .24.

Exercise 3

Take a second sample, also of size 50, and call it samp2. How does the sample proportion of samp2 compare with that of samp1? Suppose we took two more samples, one of size 100 and one of size 1000. Which would you think would provide a more accurate estimate of the population proportion?

Upon running below it seems size 1000 would be more accurate, but not necessarily the case. 100 may be big enough.

global_monitor <- tibble(
  scientist_work = c(rep("Benefits", 80000), rep("Doesn't benefit", 20000))
)
samp2 <- global_monitor %>%
  sample_n(50)

samp2 %>%
  dplyr::count(scientist_work) %>%
  dplyr::mutate(p_hat = n /sum(n))

Exercise 4

How many elements are there in sample_props50? Describe the sampling distribution, and be sure to specifically note its center. Make sure to include a plot of the distribution in your answer. 4 elements (replicate, scientist_work, n, p_hat); it is approximaely normal with a center p of .1999, or rounded .20 as is the actual population mean, se = .0566.

There was 15000 samples taken, meaning there are 15000 data pts used to make this plot. This plot is mostly normally distributed. Also with a slight skew to the right.The center seems to be around .2 which is consistent with the population parameter.

sample_props50 <- global_monitor %>%
                    rep_sample_n(size = 50, reps = 15000, replace = TRUE) %>%
                    dplyr::count(scientist_work) %>%
                    dplyr::mutate(p_hat = n /sum(n)) %>%
                    dplyr::filter(scientist_work == "Doesn't benefit")
library(DATA606)
x1 <- mean(sample_props50$p_hat)
x2 <- 1-mean(sample_props50$p_hat)
n=50
se <- sqrt(x1 * x2 / n)
x1
se
sample_props50 %>% 
  ggplot() +
  geom_histogram(aes(x = p_hat), binwidth = 0.05) +
  xlab("sample proportions")

Exercise 5

To make sure you understand how sampling distributions are built, and exactly what the rep_sample_n function does, try modifying the code to create a sampling distribution of 25 sample proportions from samples of size 10, and put them in a data frame named sample_props_small. Print the output. How many observations are there in this object called sample_props_small? What does each observation represent?

There is 25 observations. Each observation counts the number of times “doesn’t benefit” appears in the sample variable for each sample

sample_props_small <- global_monitor %>%
  rep_sample_n(size = 10, reps = 25, replace = TRUE) %>%
  dplyr::count(scientist_work) %>%
  dplyr::mutate(p_hat = n /sum(n)) %>%
  dplyr::filter(scientist_work == "Doesn't benefit")
sample_props_small

Exercise 6

Use the app below to create sampling distributions of proportions of Doesn’t benefit from samples of size 10, 50, and 100. Use 5,000 simulations. What does each observation in the sampling distribution represent? How does the mean, standar error, and shape of the sampling distribution change as the sample size increases? How (if at all) do these values change if you increase the number of simulations? (You do not need to include plots in your answer.)

Each observation is the proportion that came out of one simulation. n=10

mean= .22 se= .11 n=50

mean= .2 se= .06 n=100

mean= .2 se= .04

Upon observing above, the mean of these gets closer to .2, the true mean as you increase the number of samples. The se decreases which means that as you increase the sample size there is less variability. The distribution begins to appear normal when you make the sample larger, since at n=10 there is a right skew.

Exercise 7

Take a sample of size 15 from the population and calculate the proportion of people in this sample who think the work scientists do enchances their lives. Using this sample, what is your best point estimate of the population proportion of people who think the work scientists do enchances their lives?

Based on this sampling the best estimate is that 80% believe that the work of scientists is beneficial.

set.seed(6666)
samp3 <- global_monitor %>%
  sample_n(15)

samp3 %>%
  dplyr::count(scientist_work) %>%
  dplyr::mutate(p.hat = n /sum(n))

Exercise 8

Since you have access to the population, simulate the sampling distribution of proportion of those who think the work scientists do enchances their lives for samples of size 15 by taking 2000 samples from the population of size 15 and computing 2000 sample proportions. Store these proportions in as sample_props15. Plot the data, then describe the shape of this sampling distribution. Based on this sampling distribution, what would you guess the true proportion of those who think the work scientists do enchances their lives to be? Finally, calculate and report the population proportion.

I believe it looks normal but skewed to the left. My guess is 75% from the histogram for proportion who said “Benefits”. Actual proportion is 80%

sample_props15 <- global_monitor %>% rep_sample_n(size = 15, reps = 2000, replace = TRUE) %>%
                    dplyr::count(scientist_work) %>%
                    dplyr::mutate(p_hat = n /sum(n)) %>%
                    dplyr::filter(scientist_work == "Benefits")
ggplot(data = sample_props15, aes(x = p_hat)) +
  geom_histogram(binwidth = 0.02) +
  labs(
    x = "p_hat (Benefits)",
    title = "Sampling distribution of p_hat",
    subtitle = "Sample size = 15, Number of samples = 2000")
mean(sample_props15$p_hat)
global_monitor %>%
  dplyr::count(scientist_work) %>%
  dplyr::mutate(p = n /sum(n))

Exercise 9

Change your sample size from 15 to 150, then compute the sampling distribution using the same method as above, and store these proportions in a new object called sample_props150. Describe the shape of this sampling distribution and compare it to the sampling distribution for a sample size of 15. Based on this sampling distribution, what would you guess to be the true proportion of those who think the work scientists do enchances their lives?

It appears a good estimate of around 80%

sample_props150 <- global_monitor %>% rep_sample_n(size = 150, reps = 2000, replace = TRUE) %>%
                    count(scientist_work) %>%
                    mutate(p_hat = n /sum(n)) %>%
                    filter(scientist_work == "Benefits")

ggplot(data = sample_props150, aes(x = p_hat)) +
  geom_histogram(binwidth = 0.02) +
  labs(
    x = "p_hat (Benefits)",
    title = "Sampling distribution of p_hat",
    subtitle = "Sample size = 150, Number of samples = 2000")

Exercise 10

Of the sampling distributions from 2 and 3, which has a smaller spread? If you’re concerned with making estimates that are more often close to the true value, would you prefer a sampling distribution with a large or small spread?

The spread of the n=150 sampling distribution is smaller than the sampling distribution where n=15. a smaller spread which gives a closer approximation because the standard error will be smaller, so the proportions will be closer to the mean