Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2021-fall/materials/tutorial_11/tutorial_11.ipynb
2051 views
Kernel: R

Tutorial 11 - Introduction to Statistical Inference

Lecture and Tutorial Learning Goals:

After completing this week's lecture and tutorial work, you will be able to:

  • Describe real world examples of questions that can be answered with the statistical inference methods.

  • Name common population parameters (e.g., mean, proportion, median, variance, standard deviation) that are often estimated using sample data, and use computation to estimate these.

  • Define the following statistical sampling terms (population, sample, population parameter, point estimate, sampling distribution).

  • Explain the difference between a population parameter and sample point estimate.

  • Use computation to draw random samples from a finite population.

  • Use computation to create a sampling distribution from a finite population.

  • Describe how sample size influences the sampling distribution.

### Run this cell before continuing. library(tidyverse) library(repr) library(digest) library(infer) options(repr.matrix.max.rows = 6) source('tests_tutorial_11.R') source('cleanup_tutorial_11.R')

Virtual sampling simulation

In this tutorial you will study samples and sample means generated from different distributions. In real life, we rarely, if ever, have measurements for our entire population. Here, however, we will make simulated datasets so we can understand the behaviour of sample means.

Suppose we had the data science final grades for a large population of students.

# run this cell to simulate a finite population set.seed(20201) # DO NOT CHANGE students_pop <- tibble(grade = (rnorm(mean = 70, sd = 8, n = 10000))) students_pop

Question 1.0
{points: 1}

Visualize the distribution of the population (students_pop) that was just created by plotting a histogram using binwidth = 1 in the geom_histogram argument. Name the plot pop_dist and give x-axis a descriptive label.

options(repr.plot.width = 8, repr.plot.height = 6) # ... <- ggplot(..., ...) + # geom_...(...) + # ... + # ggtitle("Population distribution") # your code here fail() # No Answer - remove if you provide an answer pop_dist
test_1.0()

Question 1.1
{points: 3}

Describe in words the distribution above, comment on the shape, center and how spread out the distribution is.

DOUBLE CLICK TO EDIT THIS CELL AND REPLACE THIS TEXT WITH YOUR ANSWER.

Question 1.2
{points: 1}

Use summarise to calculate the following population parameters from the students_pop population:

  • mean (use the mean function)

  • median (use the median function)

  • standard deviation (use the sd function)

Name this data frame pop_parameters which has the column names pop_mean, pop_med and pop_sd.

# your code here fail() # No Answer - remove if you provide an answer pop_parameters
test_1.2()

Question 1.2.1
{points: 1}

Draw one random sample of 5 students from our population of students (students_pop). Use summarize to calculate the mean, median, and standard deviation for these 5 students.

Name this data frame ests_5 which should have column names mean_5, med_5 and sd_5. Use the seed 4321.

set.seed(4321) # DO NOT CHANGE! # your code here fail() # No Answer - remove if you provide an answer ests_5
test_1.2.1()

Question 1.2.2 Multiple Choice:
{points: 1}

Which of the following is the point estimate for the average final grade for the population of data science students (rounded to two decimal places)?

A. 70.03

B. 69.76

C. 73.52

D. 8.05

Assign your answer to an object called answer1.2.2. Your answer should be a single character surrounded by quotes.

# your code here fail() # No Answer - remove if you provide an answer
test_1.2.2()

Question 1.2.3
{points: 1}

Draw one random sample of 100 students from our population of students (students_pop). Use summarize to calculate the mean, median and standard deviation for these 100 students.

Name this data frame ests_100 which has the column names mean_100, med_100 and sd_100. Use the seed 4321.

set.seed(4321) # DO NOT CHANGE! # your code here fail() # No Answer - remove if you provide an answer ests_100
test_1.2.3()

Exploring the sampling distribution of the sample mean for different populations

We will create the sampling distribution of the sample mean by taking 1500 random samples of size 5 from this population and visualize the distribution of the sample means.

Question 1.3
{points: 1}

Draw 1500 random samples from our population of students (students_pop). Each sample should have 5 observations. Name the data frame samples and use the seed 4321.

# ... <- rep_sample_n(..., size = ..., reps = ...) set.seed(4321) # DO NOT CHANGE! # your code here fail() # No Answer - remove if you provide an answer head(samples) tail(samples) dim(samples)
test_1.3()

Question 1.4
{points: 1}

Group by the sample replicate number, and then for each sample, calculate the mean. Name the data frame sample_estimates. The data frame should have the column names replicate and sample_mean.

# your code here fail() # No Answer - remove if you provide an answer head(sample_estimates) tail(sample_estimates)
test_1.4()

Question 1.5
{points: 1}

Visualize the distribution of the sample estimates (sample_estimates) you just calculated by plotting a histogram using binwidth = 1 in the geom_histogram argument. Name the plot sampling_distribution and give the plot (using ggtitle) and the x axis a descriptive label.

options(repr.plot.width = 8, repr.plot.height = 6) # your code here fail() # No Answer - remove if you provide an answer sampling_distribution_5
test_1.5()

Question 1.6
{points: 3}

Describe in words the distribution above, comment on the shape, center and how spread out the distribution is. Compare this sampling distribution to the population distribution of students' grades above.

DOUBLE CLICK TO EDIT THIS CELL AND REPLACE THIS TEXT WITH YOUR ANSWER.

Question 1.6.1
{points: 3}

Repeat Q1.3 - 1.5, but now for 100 observations:

  1. Draw 1500 random samples from our population of students (students_pop). Each sample should have 100 observations. Use the seed 4321.

  2. Group by the sample replicate number, and then for each sample, calculate the mean (call this column sample_mean_100).

  3. Visualize the distribution of the sample estimates you calculated by plotting a histogram using binwidth = 0.5 in the geom_histogram argument. Name the plot sampling_distribution_100 and give the plot title (using ggtitle) and the x axis a descriptive label.

set.seed(4321) # DO NOT CHANGE! # your code here fail() # No Answer - remove if you provide an answer sampling_distribution_100
set.seed(4321) # DO NOT CHANGE! # We check that you've created objects with the right names below # But all other tests were intentionally hidden so that you can practice deciding # when you have the correct answer. test_that('Did not create objects named sampling_distribution_100', { expect_true(exists("sampling_distribution_100")) })

Question 1.6.2
{points: 3}

Suppose we do not know the parameter value for the population of data science students (as is usually the case in real life). Compare your point estimates for the population mean from Q1.2.1 and 1.2.3 above. Which of the two point estimates is more likely to be closer to the actual value of the average final grade of the population of data science students? Briefly explain. (Hint: look at the sampling distributions for your samples of size 5 and size 100 to help you answer this question).

DOUBLE CLICK TO EDIT THIS CELL AND REPLACE THIS TEXT WITH YOUR ANSWER.

Question 1.7
{points: 1}

Let's create a simulated dataset of the number of cups of coffee drunk per week for our population of students. Describe in words the distribution, comment on the shape, center and how spread out the distribution is.

# run this cell to simulate a finite population set.seed(2020) # DO NOT REMOVE coffee_data = tibble(cups = rexp(n = 2000, rate = 0.34)) coffee_dist <- ggplot(coffee_data, aes(cups)) + geom_histogram(binwidth = 0.5) + xlab("Cups of coffee per week") + ggtitle("Population distribution") + theme(text = element_text(size = 20)) coffee_dist

DOUBLE CLICK TO EDIT THIS CELL AND REPLACE THIS TEXT WITH YOUR ANSWER.

Question 1.8
{points: 1}

Draw 1500 random samples from coffee_data. Each sample should have 5 observations. Assign this data frame to an object called coffee_samples_5.

Group by the sample replicate number, and then for each sample, calculate the mean. Name the data frame coffee_sample_estimates_5. The data frame should have the column names replicate and coffee_sample_mean_5.

Finally, create a plot of the sampling distribution called coffee_sampling_distribution_5.

Hint: a bindwidth of 1 is a little too big for this data, try a bindwidth of 0.5 instead.

set.seed(4321) # DO NOT CHANGE! # your code here fail() # No Answer - remove if you provide an answer coffee_sampling_distribution_5
test_1.8()

Question 1.9
{points: 3}

Describe in words the distribution above, comment on the shape, center and how spread out the distribution is. Compare this sampling distribution to the population distribution above.

DOUBLE CLICK TO EDIT THIS CELL AND REPLACE THIS TEXT WITH YOUR ANSWER.

Question 2.0
{points: 1}

Draw 1500 random samples from coffee_data. Each sample should have 5 observations. Assign this data frame to an object called coffee_samples_30.

Group by the sample replicate number, and then for each sample, calculate the mean. Name the data frame coffee_sample_estimates_30. The data frame should have the column names replicate and coffee_sample_mean_30.

Finally, create a plot of the sampling distribution called coffee_sampling_distribution_30.

Hint: use xlim to control the x-axis limits so that they are similar to those in the histogram above. This will make it easier to compare this histogram with that one.

set.seed(4321) # DO NOT CHANGE! # your code here fail() # No Answer - remove if you provide an answer coffee_sampling_distribution_30
test_2.0()

Question 2.1
{points: 3}

Describe in words the distribution above, comment on the shape, center and how spread out the distribution is. Compare this sampling distribution with samples of size 30 to the sampling distribution with samples of size 5.

DOUBLE CLICK TO EDIT THIS CELL AND REPLACE THIS TEXT WITH YOUR ANSWER.

source('cleanup_tutorial_11.R')