{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "51749b984dba60c987776752607d1c99",
"grade": false,
"grade_id": "cell-f9cd89ad16c3393f",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"# Worksheet 11 - Introduction to Statistical Inference\n",
"\n",
"### Lecture and Tutorial Learning Goals:\n",
"\n",
"After completing this week's lecture and tutorial work, you will be able to:\n",
"- Describe real world examples of questions that can be answered with the statistical inference methods.\n",
"- 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.\n",
"- Define the following statistical sampling terms (population, sample, population parameter, point estimate, sampling distribution).\n",
"- Explain the difference between a population parameter and sample point estimate.\n",
"- Use computation to draw random samples from a finite population.\n",
"- Use computation to create a sampling distribution from a finite population.\n",
"- Describe how sample size influences the sampling distribution.\n",
"\n",
"This worksheet covers parts of [the Inference chapter](https://datasciencebook.ca/inference.html) of the online textbook. You should read this chapter before attempting the worksheet."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "88682e994eaa9eca03cc6cf6eb57338a",
"grade": false,
"grade_id": "cell-5e46ba6301dcd126",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"### Run this cell before continuing.\n",
"library(tidyverse)\n",
"library(repr)\n",
"library(infer)\n",
"library(cowplot)\n",
"options(repr.matrix.max.rows = 6)\n",
"source('tests.R')\n",
"source('cleanup.R')"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "01ab7b5c0258210456f4d7b3a5967d28",
"grade": false,
"grade_id": "cell-3d0ceca6b0e205c4",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"**Question 1.1** Matching:\n",
"
{points: 1}\n",
"\n",
"Read the mixed up table below and assign the variables in the code cell below a number to match the the term to it's correct definition. Do not put quotations around the number or include words in the answer, we are expecting the assigned values to be numbers.\n",
"\n",
"| Terms | Definitions |\n",
"|----------------|------------|\n",
"|
point estimate |
1. the entire set of entities/objects of interest |\n", "|
population |
2. selecting a subset of observations from a population where each observation is equally likely to be selected at any point during the selection process|\n", "|
random sampling |
3. a numerical summary value about the population |\n", "|
representative sampling |
4. a distribution of point estimates, where each point estimate was calculated from a different random sample from the same population |\n", "|
population parameter |
5. a collection of observations from a population |\n", "|
sample |
6. a single number calculated from a random sample that estimates an unknown population parameter of interest |\n", "|
observation |
7. selecting a subset of observations from a population where the sample’s characteristics are a good representation of the population’s characteristics |\n", "|
sampling distribution |
8. a quantity or a quality (or set of these) we collect from a given entity/object |"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "4559758c579dba962bc3008180f8d6db",
"grade": false,
"grade_id": "cell-02ca79301e6453c8",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"point_estimate <- NULL\n",
"population <- NULL\n",
"random_sampling <- NULL\n",
"representative_sampling <- NULL\n",
"population_parameter <- NULL\n",
"sample <- NULL\n",
"observation <- NULL\n",
"sampling_distribution <- NULL\n",
"\n",
"# your code here\n",
"fail() # No Answer - remove if you provide an answer"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "c8ca5e019bf6267ce3d1f23d5d068859",
"grade": true,
"grade_id": "cell-3e20f546495eda51",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"test_1.1()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "dcb6a452d8c2256dcf79da53b1837c71",
"grade": false,
"grade_id": "cell-4b8df8ad154a3900",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"### Virtual sampling simulation\n",
"\n",
"In real life, we rarely, if ever, have measurements for our entire population. Here, however, we will pretend that we somehow were able to ask every single Candian senior what their age is. We will do this so that we can experiment to learn about sampling and how this relates to estimation.\n",
"\n",
"Here we make a simulated dataset of ages for our population (all Canadian seniors) bounded by realistic values ($\\geq$ 65 and $\\leq$ 117):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "6093f4924cfa63aaaf299e375043e850",
"grade": false,
"grade_id": "cell-40ed1f5c6c055f97",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"# run this cell to simulate a finite population\n",
"set.seed(4321) # DO NOT CHANGE\n",
"can_seniors <- tibble(age = (rexp(2000000, rate = 0.1)^2) + 65) |> \n",
" filter(age <= 117, age >= 65)\n",
"can_seniors"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "914b146004106cfe2547ec3882a331af",
"grade": false,
"grade_id": "cell-9b660b53ec2a2962",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"**Question 1.2** \n",
"
{points: 1}\n",
"\n",
"A distribution defines all the possible values (or intervals) of the data and how often they occur. Visualize the distribution of the population (`can_seniors`) that was just created by plotting a histogram using `binwidth = 1` in the `geom_histogram` argument. Name the plot `pop_dist` and give the x-axis a descriptive label."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "45a5e3726cb30b9a559bfc1e821bcd0a",
"grade": false,
"grade_id": "cell-986bdfedad27901f",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"options(repr.plot.width = 8, repr.plot.height = 7)\n",
"# ... <- ggplot(..., ...) + \n",
"# geom_...(...) +\n",
"# ... +\n",
"# ggtitle(\"Population distribution\")\n",
"\n",
"# your code here\n",
"fail() # No Answer - remove if you provide an answer\n",
"pop_dist"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "b7486723eff3d1b358ba62dafeab9643",
"grade": true,
"grade_id": "cell-9e86388006978a3c",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"test_1.2()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "9360edcbe368fb7eef8f994af60a7aad",
"grade": false,
"grade_id": "cell-a6d7523259c0ec45",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"**Question 1.3** \n",
"
{points: 1}\n",
"\n",
"Distributions are complicated to communicate, thus we often want to represent them by a single value or small number of values. Common values used for this include the mean, median, standard deviation, etc). \n",
"\n",
"Use `summarize` to calculate the following population parameters from the `can_seniors` population:\n",
"- mean (use the `mean` function)\n",
"- median (use the `median` function)\n",
"- standard deviation (use the `sd` function)\n",
"\n",
"Name this data frame `pop_parameters` which has the column names `pop_mean`, `pop_med` and `pop_sd`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "036c9553c3c1a5f32341e4d3ae7e620d",
"grade": false,
"grade_id": "cell-e351a7d3e55d1948",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# your code here\n",
"fail() # No Answer - remove if you provide an answer\n",
"pop_parameters"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "167e4f38e1b0523d2f5df6362dc66e04",
"grade": true,
"grade_id": "cell-ebeb7e12fd1b5f86",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"test_1.3()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "52a2293385b1ebf57dfe84fc7c22c991",
"grade": false,
"grade_id": "cell-f06bf6c52b4b30be",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"**Question 1.4** \n",
"
{points: 1}\n",
"\n",
"In real life, we usually are able to only collect a single sample from the population. We use that sample to try to infer what the population looks like.\n",
"\n",
"Take a single random sample of 40 observations from the Canadian seniors population (`can_seniors`). Name it `sample_1`. Use 4321 as your seed."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "938271028f786cceb732ad31b4333bf8",
"grade": false,
"grade_id": "cell-d8e06640ab5ea386",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"set.seed(4321) # DO NOT CHANGE!\n",
"# ... <- ... |> \n",
"# rep_sample_n(...)\n",
"# your code here\n",
"fail() # No Answer - remove if you provide an answer\n",
"sample_1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "105169b1c57d83a8e1ca25c2a5dada13",
"grade": true,
"grade_id": "cell-e494fbea71d264cc",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"test_1.4()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "cb2e4e6a7ae8001f80fe6eb4552a5e28",
"grade": false,
"grade_id": "cell-752cc4982153643f",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"**Question 1.5** \n",
"
{points: 1}\n",
"\n",
"Visualize the distribution of the random sample you just took (`sample_1`) that was just created by plotting a histogram using `binwidth = 1` in the `geom_histogram` argument. Name the plot `sample_1_dist` and give the plot the title \"Sample 1 Distribution\" (using `ggtitle`) and the x-axis a descriptive label."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "54b765184c4d75aab891405a7371b140",
"grade": false,
"grade_id": "cell-826dafd6cb26adeb",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"options(repr.plot.width = 8, repr.plot.height = 7)\n",
"# your code here\n",
"fail() # No Answer - remove if you provide an answer\n",
"sample_1_dist"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "6c4d62cfaa79b32c5b7beb43aeefc985",
"grade": true,
"grade_id": "cell-170ce4a2c9de09e4",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"test_1.5()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "6729aec48f544f1958f80761d6d0f614",
"grade": false,
"grade_id": "cell-3c4b35ed554cb337",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"**Question 1.6** \n",
"
{points: 1}\n",
"\n",
"Use `summarize` to calculate the following point estimates from the random sample you just took (`sample_1`):\n",
"- mean \n",
"- median \n",
"- standard deviation \n",
"\n",
"Name this data frame `sample_1_estimates` which has the column names `sample_1_mean`, `sample_1_med` and `sample_1_sd`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "0adc936486ce2bbb4be8e3249e881c0e",
"grade": false,
"grade_id": "cell-fe9efdbe457ea8e7",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# your code here\n",
"fail() # No Answer - remove if you provide an answer\n",
"sample_1_estimates"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "32e5c30d7c25f043a8be3cde1a2d87ce",
"grade": true,
"grade_id": "cell-021bf21e4a9b93ae",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"test_1.6()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "4ad2e78a8d90eb66e4ce473255309586",
"grade": false,
"grade_id": "cell-ad24cb4b28616d3c",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"Let's now compare our random sample to the population from which it was drawn. In `ggplot`, it is possible to display multiple charts together by using the function `plot_grid` from a separate package called `cowplot`. We can use the `ncol` parameter to control how many columns of plots the grid contains. Since we want to compare the distributions' shape and position on the x-axis, it is most effective to concatenate these charts vertically in a single column."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "2ec06b4839038c295c9d5637764a5fd1",
"grade": false,
"grade_id": "cell-604e99e0db84fe98",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"# run this code cell\n",
"options(repr.plot.width = 7, repr.plot.height = 7)\n",
"plot_grid(pop_dist, sample_1_dist, ncol = 1)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "96b62573abeb50fe420efa6aa0d70ecd",
"grade": false,
"grade_id": "cell-19718631d33c85a5",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"And now let's compare the point estimates (mean, median and standard deviation) with the true population parameters we were trying to estimate:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "7e1c7143609a200cc836bda2267af13c",
"grade": false,
"grade_id": "cell-ce56a190681ef9e9",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"# run this cell\n",
"pop_parameters\n",
"sample_1_estimates |> select(-replicate)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "8cc06e0c996a9e672a8335df5c9b3bf6",
"grade": false,
"grade_id": "cell-bb9b0aefa59610ab",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"**Question 1.7** Multiple Choice\n",
"
{points: 1}\n",
"\n",
"After comparing the population and sample distributions above, and the true population parameters and the sample point estimates, which statement below **is not** correct:\n",
"\n",
"A. The sample point estimates are close to the values for the true population parameters we are trying to estimate\n",
"\n",
"B. The sample distribution is of a similar shape to the population distribution\n",
"\n",
"C. The sample point estimates are identical to the values for the true population parameters we are trying to estimate\n",
"\n",
"*Assign your answer to an object called `answer1.7`. Your answer should be a single character surrounded by quotes.*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "74cc2ef9a395eef9f5de1567c52125cc",
"grade": false,
"grade_id": "cell-8ff972ce63d3c314",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# your code here\n",
"fail() # No Answer - remove if you provide an answer"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "05d234881df5d96df021a0e44630a97d",
"grade": true,
"grade_id": "cell-d5cee0eb7c6aebc8",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"test_1.7()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "2134dab16ee4c61cad02eddf2d22a753",
"grade": false,
"grade_id": "cell-8babcf95fe04cfa6",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"**Question 1.8.0** \n",
"
{points: 1}\n",
"\n",
"What if we took another sample? What would we expect? Let's try! \n",
"\n",
"Take another random sample of size 40 from population (using a different random seed this time so that you get a different sample), visualize its distribution (give the plot the title \"Sample 2 Distribution\" using `ggtitle`), and calculate the point estimates for the sample mean, median and standard deviation. Name your random sample of data `sample_2`, name your visualization `sample_2_dist`, and finally name your estimates `sample_2_estimates`, which has the column names `sample_2_mean`, `sample_2_med` and `sample_2_sd`. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "16744c64a8a2ad12c4347e8134aa28d2",
"grade": false,
"grade_id": "cell-716cd961bf3e0e5c",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"set.seed(2020) # DO NOT CHANGE!\n",
"\n",
"# your code here\n",
"fail() # No Answer - remove if you provide an answer"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "4cec1a8c3192f1e01dda4860afc7e2bd",
"grade": true,
"grade_id": "cell-87ae342a039c4eb0",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"test_1.8.0()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "d5944dedaac7da38d138f20b25355cbc",
"grade": false,
"grade_id": "cell-84562b964d37b15f",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"**Question 1.8.1** \n",
"
{points: 1}\n",
"\n",
"After comparing the distribution and point estimates of this second random sample from the population with that of the first random sample and the population, which of the following statements below **is not** correct:\n",
"\n",
"A. The sample distributions from different random samples are of a similar shape to the population distribution, but they vary a bit depending which values are captured in the sample\n",
"\n",
"B. The sample point estimates from different random samples are close to the values for the true population parameters we are trying to estimate, but they vary a bit depending which values are captured in the sample\n",
"\n",
"C. Every random sample from the same population should have an identical set of values and yield identical point estimates.\n",
"\n",
"*Assign your answer to an object called `answer1.8.1`. Your answer should be a single character surrounded by quotes.*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "b0ea9d71df6964823f4b7096bcc9f8bb",
"grade": false,
"grade_id": "cell-17f98eceba8ee494",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# your code here\n",
"fail() # No Answer - remove if you provide an answer"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "264b0dabc987cd05ca3e054658379bd3",
"grade": true,
"grade_id": "cell-00564a8c56afa5c2",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"test_1.8.1()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "31036d29b44c32c93ceb3c64c62f29cd",
"grade": false,
"grade_id": "cell-d1e256c58e03abb7",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"### Exploring the sampling distribution of an estimate\n",
"\n",
"Just how much should we expect the point estimates of our random samples to vary? To build an intuition for this, let's experiment a little more with our population of Canadian seniors. To do this we will take 1500 random samples, and then calculate the point estimate we are interested in (let's choose the mean for this example) for each sample. Finally, we will visualize the distribution of the sample point estimates. This distribution will tell us how much we would expect the point estimates of our random samples to vary for this population for samples of size 40 (the size of our samples)."
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "9e83e2ac250dada0e8a856dd82fa5729",
"grade": false,
"grade_id": "cell-2f445a196c50749f",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"**Question 1.9** \n",
"
{points: 1}\n",
"\n",
"Draw 1500 random samples from our population of Canadian seniors (`can_seniors`). Each sample should have 40 observations. Name the data frame `samples` and use the seed `4321`. Here we use the functions `head()`, `tail()` and `dim()` to view the first few rows, the last few rows and the dimension of the data set respectively. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "9d65fe2a45cac52df462ba1f1a472950",
"grade": false,
"grade_id": "cell-695ac307a193414a",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"set.seed(4321) # DO NOT CHANGE!\n",
"# ... <- rep_sample_n(..., size = ..., reps = ...)\n",
"# your code here\n",
"fail() # No Answer - remove if you provide an answer\n",
"head(samples)\n",
"tail(samples)\n",
"dim(samples)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "db41aec22a47bc0783b90eee3de69184",
"grade": true,
"grade_id": "cell-c58c6bf8b51e07c5",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"test_1.9()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "bd1129a97b739c58ff44902de836d69c",
"grade": false,
"grade_id": "cell-54d3669aec34e1c1",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"**Question 2.0** \n",
"
{points: 1}\n",
"\n",
"Group by the sample replicate number, and then for each sample, calculate the mean as the point estimate. Name the data frame `sample_estimates`. The data frame should have the column names `replicate` and `mean_age`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "b23a4184d470fc3e91944e140d7decff",
"grade": false,
"grade_id": "cell-db790cf4f047972a",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# your code here\n",
"fail() # No Answer - remove if you provide an answer\n",
"head(sample_estimates)\n",
"tail(sample_estimates)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "940c4e0d26e77f5cb34a983c25578a2a",
"grade": true,
"grade_id": "cell-009a8fb2b91b528c",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"test_2.0()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "27e5c561747ac4e8a0f4850e547b790d",
"grade": false,
"grade_id": "cell-b37ce66b7bf8e8bf",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"**Question 2.1** \n",
"
{points: 1}\n",
"\n",
"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`. Give the plot the title \"Sampling Distribution of the Sample Means\" using `ggtitle`, and give the x-axis a descriptive label."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "b83c4ae2ed2ee44444e3f9ee7a493ade",
"grade": false,
"grade_id": "cell-cc60d0afb72d45c2",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"options(repr.plot.width = 8, repr.plot.height = 7)\n",
"# your code here\n",
"fail() # No Answer - remove if you provide an answer\n",
"sampling_distribution"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "edd643490aee3b0fd3c60d1819d6ce9e",
"grade": true,
"grade_id": "cell-63a57e1040dc4a79",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"test_2.1()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "734abe600753723b931a5e88d6198ce2",
"grade": false,
"grade_id": "cell-e1b528491ee83745",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"**Question 2.2** \n",
"
{points: 1}\n",
"\n",
"Let's refresh our memories: what is the mean age of the whole population (we calculated this above)? *Assign your answer to an object called `answer2.2`. Your answer should be a single number reported to two decimal places.*\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "dffeac9b2d29cafe43ced6d56fd97919",
"grade": false,
"grade_id": "cell-b3dc0d016c0b2fa0",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# your code here\n",
"fail() # No Answer - remove if you provide an answer\n",
"answer2.2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "2d2f92082669d167fbc4f10317fe33d3",
"grade": true,
"grade_id": "cell-859e93c62242915f",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"test_2.2()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "1a0b1397dff77ed38c42d37f1ca4648c",
"grade": false,
"grade_id": "cell-cd211c77f0a630b0",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"**Question 2.3** Multiple Choice\n",
"
{points: 1}\n",
"\n",
"Considering the true value for the population mean, and the sampling distribution you created and visualized in **question 2.1**, which of the following statements below **is not** correct:\n",
"\n",
"A. The sampling distribution is centered at the true population mean\n",
"\n",
"B. All the sample means are the same value as the true population mean\n",
"\n",
"C. Most sample means are at or very near the same value as the true population mean\n",
"\n",
"D. A few sample means are far away from the same value as the true population mean\n",
"\n",
"*Assign your answer to an object called `answer2.3`. Your answer should be a single character surrounded by quotes.*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "040a187753df7834f42e80ad57e4cd23",
"grade": false,
"grade_id": "cell-aee3630dbf16cba9",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# your code here\n",
"fail() # No Answer - remove if you provide an answer\n",
"answer2.3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "5254dc9c444c09d871b1ff2dbaa3c866",
"grade": true,
"grade_id": "cell-d79f9703b99565f3",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"test_2.3()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "880295b89d665f09eb50eb60280975da",
"grade": false,
"grade_id": "cell-e77dc5e07d87e667",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"**Question 2.4** True/False\n",
"
{points: 1}\n",
"\n",
"Taking a random sample and calculating a point estimate is a good way to get a \"best guess\" of the population parameter you are interested in. True or False?\n",
"\n",
"*Assign your answer to an object called `answer2.4`. Your answer should be either \"True\" or \"False\", surrounded by quotes.*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "f4f8949e9529b3e798d3f8890a7d9fea",
"grade": false,
"grade_id": "cell-6f8050a70cec0677",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# your code here\n",
"fail() # No Answer - remove if you provide an answer\n",
"answer2.4"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "f7c9b34fa6ada8f3c33544d04da5a11d",
"grade": true,
"grade_id": "cell-83c05f5c808126c1",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"test_2.4()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "7126f8ab1f7737176325c9e27b5e68bb",
"grade": false,
"grade_id": "cell-2465e27157c638d7",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"### The influence of sample size on the sampling distribution\n",
"\n",
"What happens to our point estimate when we change the sample size? Let's answer this question by experimenting! We will create 3 different sampling distributions of sample means, each using a different sample size. As we did above, we will draw samples from our Canadian seniors population. We will visualize these sampling distributions and see if we can see a pattern when we vary the sample size."
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "fe394bde873fe5dec542b90a354b5877",
"grade": false,
"grade_id": "cell-34e0ccb715d582e3",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"**Question 2.5** \n",
"
{points: 1}\n",
"\n",
"Using the same strategy as you did above, draw 1500 random samples from the Canadian seniors population (`can_seniors`), each of size 20. For each sample, calculate the mean age and assign this data to a column called `mean_age`. \n",
"\n",
"Then, visualize the distribution of the sample estimates (means) you just calculated by plotting a histogram using `binwidth = 1` in the `geom_histogram` argument. Name the plot `sampling_distribution_20`. Give the plot the title \"Sampling Distribution (n=20)\" using `ggtitle`, and give the x-axis a descriptive label. Also specify the x-axis limits to be 65 and 95 using `xlim(c(65, 95))`.\n",
"\n",
"Set the seed as 4321 when you collect your samples."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "5c49d8c10d02a496e31396cbee1dd162",
"grade": false,
"grade_id": "cell-b749043da4922d58",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"set.seed(4321) # DO NOT CHANGE THIS!\n",
"options(repr.plot.width = 8, repr.plot.height = 7)\n",
"\n",
"# your code here\n",
"fail() # No Answer - remove if you provide an answer\n",
"sampling_distribution_20"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "dfc6e4edd8508059a95b2ac23691d3a6",
"grade": true,
"grade_id": "cell-fae3123c38b31ad8",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"test_2.5()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "998ab25d8618eae2a530516d1f47ef93",
"grade": false,
"grade_id": "cell-54728427a6fc9f21",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"**Question 2.6** \n",
"
{points: 1}\n",
"\n",
"Using the same strategy as you did above, draw 1500 random samples from the Canadian seniors population (`can_seniors`), each of size 100. For each sample, calculate the mean age and assign this data to a column called `mean_age`. \n",
"\n",
"Then, visualize the distribution of the sample estimates (means) you just calculated by plotting a histogram using `binwidth = 1` in the `geom_histogram` argument. Name the plot `sampling_distribution_100`. Give the plot the title \"Sampling Distribution (n=100)\" using `ggtitle`, and give the x axis a descriptive label. Also specify the x-axis limits to be 65 and 95 using `xlim(c(65, 95))`.\n",
"\n",
"Set the seed as 4321 when you collect your samples."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "824b27e5ad5febb24b04849b6d9e2ae6",
"grade": false,
"grade_id": "cell-417f5129afaf8a43",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"set.seed(4321) # DO NOT CHANGE THIS!\n",
"options(repr.plot.width = 8, repr.plot.height = 7)\n",
"# your code here\n",
"fail() # No Answer - remove if you provide an answer\n",
"sampling_distribution_100"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "fb8cc2a822c56e0471b7ae06e245f7de",
"grade": true,
"grade_id": "cell-5cc24d62254b5bc5",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"test_2.6()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "037d21afa792d88fc68a90353cf60f6c",
"grade": false,
"grade_id": "cell-afda78b801dfe776",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"# run this cell to change the sampling distribution plot created\n",
"# earlier in the notebook so that the x-axis is the same dimensions\n",
"# as the other two plots you just made, and so that the title is \"n = 40\"\n",
"sampling_distribution <- sampling_distribution + \n",
" xlim(c(65, 95))\n",
"sampling_distribution$labels$title <- \"Sampling Distribution (n=40)\""
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "f27ae5088f8e12c46855b809380df9ae",
"grade": false,
"grade_id": "cell-617a34d8b1bfc6fb",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"**Question 2.7** \n",
"
{points: 1}\n",
"\n",
"Fill in the blanks in the code below to use `plot_grid` to concatenate the three sampling distributions vertically. Order them from smallest sample size on the on the top, to largest sample size on the bottom. Name the final panel figure `sampling_distribution_panel`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "dbfbfed42621e0d64a0a2e5b0a9928df",
"grade": false,
"grade_id": "cell-a987790eea24ce45",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"options(repr.plot.width = 6)\n",
"# sampling_distribution_panel <- plot_grid(\n",
"# ...,\n",
"# ...,\n",
"# ...,\n",
"# ncol = 1\n",
"# )\n",
"\n",
"# your code here\n",
"fail() # No Answer - remove if you provide an answer"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "67ed268567034d0e2126d8d764eaf6fc",
"grade": true,
"grade_id": "cell-c76d50133fbfb9e4",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"test_2.7()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "455f5dd740158ff67a58c74bae558c97",
"grade": false,
"grade_id": "cell-05e3eccb230d6deb",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"**Question 2.8** Multiple Choice\n",
"
{points: 1}\n",
"\n",
"Considering the panel figure you created above in **question 2.7**, which of the following statements below **is not** correct:\n",
"\n",
"A. As the sample size increases, the sampling distribution of the point estimate becomes narrower.\n",
"\n",
"B. As the sample size increases, more sample point estimates are closer to the true population mean.\n",
"\n",
"C. As the sample size decreases, the sample point estimates become more variable (spread out).\n",
"\n",
"D. As the sample size increases, the sample point estimates become more variable (spread out).\n",
"\n",
"*Assign your answer to an object called `answer2.8`. Your answer should be a single character surrounded by quotes.*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "016ec1a1ff259afc665651735f7bdbed",
"grade": false,
"grade_id": "cell-8f9319a3b3411ed5",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# your code here\n",
"fail() # No Answer - remove if you provide an answer\n",
"answer2.8"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "cf26d56de56ef089c0649b1acdca5f6b",
"grade": true,
"grade_id": "cell-4bd59f7910e865dc",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"test_2.8()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "9bd9cb5c4bc51055cfe221d2f9578d5e",
"grade": false,
"grade_id": "cell-4622783ca69e105d",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"**Question 2.9** True/False\n",
"
{points: 1}\n",
"\n",
"Given what you observed above, and considering the real life scenario where you will only have one sample, answer the True/False question below:\n",
"\n",
"The smaller your random sample, the better your sample point estimate reflect the true population parameter you are trying to estimate. True or False?\n",
"\n",
"*Assign your answer to an object called `answer2.9`. Your answer should be either \"true\" or \"false\", surrounded by quotes.*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "b7b6d0e454406e26d2714f6e3137ab4f",
"grade": false,
"grade_id": "cell-3c587e58178a0614",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# your code here\n",
"fail() # No Answer - remove if you provide an answer\n",
"answer2.9"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "92bfeaa9cfd807840f01ceeba8f8c9e9",
"grade": true,
"grade_id": "cell-d346c550b654bff1",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"test_2.9()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "4e115acc35e74844a849f756e78b1544",
"grade": false,
"grade_id": "cell-b6d717903fcd154d",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"source('cleanup.R')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "R",
"language": "R",
"name": "ir"
},
"language_info": {
"codemirror_mode": "r",
"file_extension": ".r",
"mimetype": "text/x-r-source",
"name": "R",
"pygments_lexer": "r",
"version": "4.3.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}