Course Description
Statistics is the study of how to collect, analyze, and draw conclusions from data. It’s a hugely valuable tool that you can use to bring the future into focus and infer the answer to tons of questions. For example, what is the likelihood of someone purchasing your product, how many calls will your support team receive, and how many jeans sizes should you manufacture to fit 95% of the population? In this course, you’ll use sales data to discover how to answer questions like these as you grow your statistical skills and learn how to calculate averages, use scatterplots to show the relationship between numeric values, and calculate correlation. You’ll also tackle probability, the backbone of statistical reasoning, and learn how to conduct a well-designed study to draw your own conclusions from data.
Summary statistics gives you the tools you need to boil down massive datasets to reveal the highlights. In this chapter, you’ll explore summary statistics including mean, median, and standard deviation, and learn how to accurately interpret them. You’ll also develop your critical thinking skills, allowing you to choose the best summary statistics for your data.
Statistics can be used to answer lots of different types of questions, but being able to identify which type of statistics is needed is essential to drawing accurate conclusions. In this exercise, you’ll sharpen your skills by identifying which type is needed to answer each question.
In the video, you learned about two main types of data: numeric and categorical. Numeric variables can be classified as either discrete or continuous, and categorical variables can be classified as either nominal or ordinal. These characteristics of a variable determine which ways of summarizing your data will work best.
In this chapter, you’ll be working with the 2018
Food Carbon Footprint Index from nu3. The
food_consumption
dataset contains information about the
kilograms of food consumed per person per year in each country in each
food category (consumption
) as well as information about
the carbon footprint of that food category (co2_emissions
)
measured in kilograms of carbon dioxide, or CO\_2\, per person per year
in each country.
In this exercise, you’ll compute measures of center to compare food
consumption in the US and Belgium using your dplyr
skills.
dplyr
is loaded for you and
food_consumption
is available.
food_consumption
for "Belgium"
and the another
that holds rows for "USA"
. Call these
belgium_consumption
and usa_consumption
.food_consumption
for rows with data about
Belgium and the USA.country
.mean_consumption
and median_consumption
.# edited/added
library(tidyverse)
food_consumption <- readRDS('food_consumption.rds')
# Filter for Belgium
belgium_consumption <- food_consumption %>%
filter(country == "Belgium")
# Filter for USA
usa_consumption <- food_consumption %>%
filter(country == "USA")
# Calculate mean and median consumption in Belgium
mean(belgium_consumption$consumption)
median(belgium_consumption$consumption)
# Calculate mean and median consumption in USA
mean(usa_consumption$consumption)
median(usa_consumption$consumption)
food_consumption %>%
# Filter for Belgium and USA
filter(country %in% c("Belgium", "USA")) %>%
# Group by country
group_by(country) %>%
# Get mean_consumption and median_consumption
summarize(mean_consumption = mean(consumption),
median_consumption = median(consumption))
In the video, you learned that the mean is the sum of all the data points divided by the total number of data points, and the median is the middle value of the dataset where 50% of the data is less than the median, and 50% of the data is greater than the median. In this exercise, you’ll compare these two measures of center.
dplyr
and ggplot2
are loaded and
food_consumption
is available.
food_consumption
to get the rows where
food_category
is "rice"
.ggplot2
of
co2_emission
for rice.Take a look at the histogram of the CO2 emissions for rice you just plotted. Which of the following terms best describes the shape of the data?
No skew
Left-skewed
Right-skewed
Filter food_consumption
to get the rows where
food_category
is "rice"
.
Summarize the data to get the mean and median of
co2_emission
, calling them mean_co2
and
median_co2.
Given the skew of this data, what measure of central tendency best summarizes the kilograms of CO2 emissions per person per year for rice?
food_consumption %>%
# Filter for rice food category
filter(food_category == "rice") %>%
# Create histogram of co2_emission
ggplot(aes(co2_emission)) +
geom_histogram()
food_consumption %>%
# Filter for rice food category
filter(food_category == "rice") %>%
# Create histogram of co2_emission
ggplot(aes(co2_emission)) +
geom_histogram()
food_consumption %>%
# Filter for rice food category
filter(food_category == "rice") %>%
# Get mean_co2 and median_co2
summarize(mean_co2 = mean(co2_emission),
median_co2 = median(co2_emission))
Quantiles are a great way of summarizing numerical data since they can be used to measure center and spread, as well as to get a sense of where a data point stands in relation to the rest of the dataset. For example, you might want to give a discount to the 10% most active users on a website.
In this exercise, you’ll calculate quartiles, quintiles, and deciles, which split up a dataset into 4, 5, and 10 pieces, respectively.
The dplyr
package is loaded and
food_consumption
is available.
co2_emission
column of
food_consumption
.co2_emission
column of
food_consumption
.co2_emission
that
split up the data into ten pieces (deciles).# Calculate the quartiles of co2_emission
quantile(food_consumption$co2_emission)
# Calculate the quintiles of co2_emission
quantile(food_consumption$co2_emission, probs = c(0, 0.2, 0.4, 0.6, 0.8, 1))
# Calculate the deciles of co2_emission
quantile(food_consumption$co2_emission, probs = seq(0, 1, 0.1))
Variance and standard deviation are two of the most common ways to measure the spread of a variable, and you’ll practice calculating these in this exercise. Spread is important since it can help inform expectations. For example, if a salesperson sells a mean of 20 products a day, but has a standard deviation of 10 products, there will probably be days where they sell 40 products, but also days where they only sell one or two. Information like this is important, especially when making predictions.
Both dplyr
and ggplot2
are loaded, and
food_consumption
is available.
co2_emission
for each food_category
by
grouping by and summarizing variance as var_co2
and
standard deviation as sd_co2
.co2_emission
for each
food_category
using facet_wrap()
.# Calculate variance and sd of co2_emission for each food_category
food_consumption %>%
group_by(food_category) %>%
summarize(var_co2 = var(co2_emission),
sd_co2 = sd(co2_emission))
# Create subgraphs for each food_category: histogram of co2_emission
ggplot(food_consumption, aes(co2_emission)) +
# Create a histogram
geom_histogram() +
# Create a separate sub-graph for each food_category
facet_wrap(~ food_category)
Outliers can have big effects on statistics like mean, as well as
statistics that rely on the mean, such as variance and standard
deviation. Interquartile range, or IQR, is another way of measuring
spread that’s less influenced by outliers. IQR is also often used to
find outliers. If a value is less than \ - 1.5 \ or greater than \ + 1.5
\, it’s considered an outlier. In fact, this is how the lengths of the
whiskers in a ggplot2
box plot are calculated.
Diagram of a box plot showing median, quartiles, and outliers
In this exercise, you’ll calculate IQR and use it to find some
outliers. Both dplyr
and ggplot2
are loaded
and food_consumption
is available.
co2_emission
per country by
grouping by country and taking the sum of co2_emission
.
Call the sum total_emission
and store the resulting data
frame as emissions_by_country
.total_emission
and store these as q1
and q3
.total_emission
and
store it as iqr
.total_emission
, and store these as lower
and
upper
.filter()
to get countries with a
total_emission
greater than the upper
cutoff
or a total_emission
less than the lower
cutoff.# Calculate total co2_emission per country: emissions_by_country
emissions_by_country <- food_consumption %>%
group_by(country) %>%
summarize(total_emission = sum(co2_emission))
emissions_by_country
# Calculate total co2_emission per country: emissions_by_country
emissions_by_country <- food_consumption %>%
group_by(country) %>%
summarize(total_emission = sum(co2_emission))
# Compute the first and third quartiles and IQR of total_emission
q1 <- quantile(emissions_by_country$total_emission, 0.25)
q3 <- quantile(emissions_by_country$total_emission, 0.75)
iqr <- q3 - q1
# Calculate total co2_emission per country: emissions_by_country
emissions_by_country <- food_consumption %>%
group_by(country) %>%
summarize(total_emission = sum(co2_emission))
# Compute the first and third quartiles and IQR of total_emission
q1 <- quantile(emissions_by_country$total_emission, 0.25)
q3 <- quantile(emissions_by_country$total_emission, 0.75)
iqr <- q3 - q1
# Calculate the lower and upper cutoffs for outliers
lower <- q1 - 1.5 * iqr
upper <- q3 + 1.5 * iqr
# Calculate total co2_emission per country: emissions_by_country
emissions_by_country <- food_consumption %>%
group_by(country) %>%
summarize(total_emission = sum(co2_emission))
# Compute the first and third quartiles and IQR of total_emission
q1 <- quantile(emissions_by_country$total_emission, 0.25)
q3 <- quantile(emissions_by_country$total_emission, 0.75)
iqr <- q3 - q1
# Calculate the lower and upper cutoffs for outliers
lower <- q1 - 1.5 * iqr
upper <- q3 + 1.5 * iqr
In this chapter, you’ll learn how to generate random samples and measure chance using probability. You’ll work with real-world sales data to calculate the probability of a salesperson being successful. Finally, you’ll use the binomial distribution to model events with binary outcomes.
In the video, you learned about two different ways of taking samples: with replacement and without replacement. Although it isn’t always easy to tell which best fits various situations, it’s important to correctly identify this so that any probabilities you report are accurate. In this exercise, you’ll put your new knowledge to the test and practice figuring this out.
You’re in charge of the sales team, and it’s time for performance reviews, starting with Amir. As part of the review, you want to randomly select a few of the deals that he’s worked on over the past year so that you can look at them more deeply. Before you start selecting deals, you’ll first figure out what the chances are of selecting certain deals.
Recall that the probability of an event can be calculated by \[ P(\text{event}) = \frac{\text{# ways event can happen}}{\text{total \# of possible outcomes}} \]
dplyr
is loaded and amir_deals
is
available.
product
type.prob
by dividing
n
by the total number of deals Amir worked on.If you randomly select one of Amir’s deals, what’s the probability
that the deal will involve Product C
?
# edited/added
amir_deals <- readRDS('seller_1.rds') %>%
select(product, client, status, amount, num_users)
# Count the deals for each product
amir_deals %>%
count(product)
# Calculate probability of picking a deal with each product
amir_deals %>%
count(product) %>%
mutate(prob = n/sum(n))
In the previous exercise, you counted the deals Amir worked on. Now it’s time to randomly pick five deals so that you can reach out to each customer and ask if they were satisfied with the service they received. You’ll try doing this both with and without replacement.
Additionally, you want to make sure this is done randomly and that it can be reproduced in case you get asked how you chose the deals, so you’ll need to set the random seed before sampling from the deals.
dplyr
is loaded and amir_deals
is
available.
31
.What type of sampling is better to use for this situation?
# Set random seed to 31
set.seed(31)
# Sample 5 deals without replacement
amir_deals %>%
sample_n(5)
# Set random seed to 31
set.seed(31)
# Sample 5 deals with replacement
amir_deals %>%
sample_n(5, replace = TRUE)
A new restaurant opened a few months ago, and the restaurant’s
management wants to optimize its seating space based on the size of the
groups that come most often. On one night, there are 10 groups of people
waiting to be seated at the restaurant, but instead of being called in
the order they arrived, they will be called randomly. In this exercise,
you’ll investigate the probability of groups of different sizes getting
picked first. Data on each of the ten groups is contained in the
restaurant_groups
data frame.
Remember that expected value can be calculated by multiplying each
possible outcome with its corresponding probability and taking the sum.
The restaurant_groups
data is available and
dplyr
and ggplot2
are loaded.
group_size
column of
restaurant_groups
, setting the number of bins to
5
.group_size
in
restaurant_groups
, then add a column called
probability
that contains the probability of randomly
selecting a group of each size. Store this in a new data frame called
size_distribution
.size_distribution
,
which represents the expected group size.# edited/added
restaurant_groups <- read.csv('restaurant_groups.csv')
# Create a histogram of restaurant_groups
ggplot(restaurant_groups, aes(group_size)) +
geom_histogram(bins = 5)
# Create probability distribution
size_distribution <- restaurant_groups %>%
# Count number of each group size
count(group_size) %>%
# Calculate probability
mutate(probability = n / sum(n))
size_distribution
# Create probability distribution
size_distribution <- restaurant_groups %>%
count(group_size) %>%
mutate(probability = n / sum(n))
# Calculate expected group size
expected_val <- sum(size_distribution$group_size *
size_distribution$probability)
expected_val
# Create probability distribution
size_distribution <- restaurant_groups %>%
count(group_size) %>%
mutate(probability = n / sum(n))
# Calculate probability of picking group of 4 or more
size_distribution %>%
# Filter for groups of 4 or larger
filter(group_size >= 4) %>%
# Calculate prob_4_or_more by taking sum of probabilities
summarize(prob_4_or_more = sum(probability))
Which sample is most likely to have been taken from a uniform distribution?
A: bell-shaped distribution, B: relatively flat distribution, C: lots of lower values, fewer high values
The app to the right will take a sample from a discrete uniform distribution, which includes the numbers 1 through 9, and calculate the sample’s mean. You can adjust the size of the sample using the slider. Note that the expected value of this distribution is 5.
A sample is taken, and you win twenty dollars if the sample’s mean is less than 4. There’s a catch: you get to pick the sample’s size.
Which sample size is most likely to win you the twenty dollars?
At this point, you’ve learned about the two different variants of the uniform distribution: the discrete uniform distribution, and the continuous uniform distribution. In this exercise, you’ll decide which situations follow which distribution.
Illustration of discrete and continuous uniform distributions
The sales software used at your company is set to automatically back itself up, but no one knows exactly what time the back-ups happen. It is known, however, that back-ups happen exactly every 30 minutes. Amir comes back from sales meetings at random times to update the data on the client he just met with. He wants to know how long he’ll have to wait for his newly-entered data to get backed up. Use your new knowledge of continuous uniform distributions to model this situation and answer Amir’s questions.
min
and his longest possible wait time as max
.
Remember that back-ups happen every 30 minutes.prob_less_than_5
.prob_greater_than_5
.prob_between_10_and_20
.# Min and max wait times for back-up that happens every 30 min
min <- 0
max <- 30
# Min and max wait times for back-up that happens every 30 min
min <- 0
max <- 30
# Calculate probability of waiting less than 5 mins
prob_less_than_5 <- punif(5, min, max)
prob_less_than_5
# Min and max wait times for back-up that happens every 30 min
min <- 0
max <- 30
# Calculate probability of waiting more than 5 mins
prob_greater_than_5 <- punif(5, min, max, lower.tail = FALSE)
prob_greater_than_5
# Min and max wait times for back-up that happens every 30 min
min <- 0
max <- 30
# Calculate probability of waiting 10-20 mins
prob_between_10_and_20 <- punif(20, min, max) - punif(10, min, max)
prob_between_10_and_20
To give Amir a better idea of how long he’ll have to wait, you’ll simulate Amir waiting 1000 times and create a histogram to show him what he should expect. Recall from the last exercise that his minimum wait time is 0 minutes and his maximum wait time is 30 minutes.
A data frame called wait_times
is available and
dplyr
and ggplot2
are loaded.
334
.time
in the wait_times
data frame.# edited/added
wait_times <- data.frame(simulation_nb = 1:1000)
# Set random seed to 334
set.seed(334)
# Set random seed to 334
set.seed(334)
# Generate 1000 wait times between 0 and 30 mins, save in time column
wait_times %>%
mutate(time = runif(1000, min = 0, max = 30))
# Set random seed to 334
set.seed(334)
# Generate 1000 wait times between 0 and 30 mins, save in time column
wait_times %>%
mutate(time = runif(1000, min = 0, max = 30)) %>%
# Create a histogram of simulated times
ggplot(aes(time)) +
geom_histogram(bins = 30)
Assume that Amir usually works on 3 deals per week, and overall, he wins 30% of deals he works on. Each deal has a binary outcome: it’s either lost, or won, so you can model his sales deals with a binomial distribution. In this exercise, you’ll help Amir simulate a year’s worth of his deals so he can better understand his performance.
deals
.# Set random seed to 10
set.seed(10)
# Simulate a single deal
rbinom(1, 1, 0.3)
# Set random seed to 10
set.seed(10)
# Simulate 1 week of 3 deals
rbinom(1, 3, 0.3)
# Set random seed to 10
set.seed(10)
# Simulate 52 weeks of 3 deals
deals <- rbinom(52, 3, 0.3)
# Calculate mean deals won per week
mean(deals)
Just as in the last exercise, assume that Amir wins 30% of deals. He wants to get an idea of how likely he is to close a certain number of deals each week. In this exercise, you’ll calculate what the chances are of him closing different numbers of deals using the binomial distribution.
# Probability of closing 3 out of 3 deals
dbinom(3, 3, 0.3)
# Probability of closing <= 1 deal out of 3 deals
pbinom(1, 3, 0.3)
# Probability of closing > 1 deal out of 3 deals
pbinom(1, 3, 0.3, lower.tail = FALSE)
Now Amir wants to know how many deals he can expect to close each week if his win rate changes. Luckily, you can use your binomial distribution knowledge to help him calculate the expected value in different situations. Recall from the video that the expected value of a binomial distribution can be calculated by \n p\.
# Expected number won with 30% win rate
won_30pct <- 3 * 0.3
won_30pct
# Expected number won with 25% win rate
won_25pct <- 3 * 0.25
won_25pct
# Expected number won with 35% win rate
won_35pct <- 3 * 0.35
won_35pct
It’s time to explore one of the most important probability distributions in statistics, normal distribution. You’ll create histograms to plot normal distributions and gain an understanding of the central limit theorem, before expanding your knowledge of statistical functions by adding the Poisson, exponential, and t-distributions to your repertoire.
Since each deal Amir worked on (both won and lost) was different,
each was worth a different amount of money. These values are stored in
the amount
column of amir_deals
As part of
Amir’s performance review, you want to be able to estimate the
probability of him selling different amounts, but before you can do
this, you’ll need to determine what kind of distribution the
amount
variable follows.
Both dplyr
and ggplot2
are loaded and
amir_deals
is available.
amount
.Which probability distribution do the sales amounts
most
closely follow?
# Histogram of amount with 10 bins
ggplot(amir_deals, aes(amount)) +
geom_histogram(bins = 10)
Since each deal Amir worked on (both won and lost) was different,
each was worth a different amount of money. These values are stored in
the amount
column of amir_deals
and follow a
normal distribution with a mean of 5000 dollars and a standard deviation
of 2000 dollars. As part of his performance metrics, you want to
calculate the probability of Amir closing a deal worth various
amounts.
# Probability of deal < 7500
pnorm(7500, mean = 5000, sd = 2000)
# Probability of deal > 1000
pnorm(1000, mean = 5000, sd = 2000, lower.tail = FALSE)
# Probability of deal between 3000 and 7000
pnorm(7000, mean = 5000, sd = 2000) - pnorm(3000, mean = 5000, sd = 2000)
# Calculate amount that 75% of deals will be more than
qnorm(0.75, mean = 5000, sd = 2000, lower.tail = FALSE)
The company’s financial analyst is predicting that next quarter, the
worth of each sale will increase by 20% and the volatility, or standard
deviation, of each sale’s worth will increase by 30%. To see what Amir’s
sales might look like next quarter under these new market conditions,
you’ll simulate new sales amounts using the normal distribution and
store these in the new_sales
data frame, which has already
been created for you.
In addition, dplyr
and ggplot2
are
loaded.
new_mean
.new_sd
.amount
to the data frame
new_sales
, which contains 36 simulated amounts from a
normal distribution with a mean of new_mean
and a standard
deviation of new_sd
.new_sales
amount
s using a histogram with 10 bins.# edited/added
new_sales <- data.frame(sale_num = 1:36)
# Calculate new average amount
new_mean <- 5000 * 1.2
# Calculate new standard deviation
new_sd <- 2000 * 1.3
# Simulate 36 sales
new_sales <- new_sales %>%
mutate(amount = rnorm(36, mean = new_mean, sd = new_sd))
# Create histogram with 10 bins
ggplot(new_sales, aes(amount)) +
geom_histogram(bins = 10)
The key metric that the company uses to evaluate salespeople is the percent of sales they make over $1000 since the time put into each sale is usually worth a bit more than that, so the higher this metric, the better the salesperson is performing.
Recall that Amir’s current sales amounts have a mean of $5000 and a standard deviation of $2000, and Amir’s predicted amounts in next quarter’s market have a mean of $6000 and a standard deviation of $2600.
Based only on the metric of percent of sales over $1000, does Amir perform better in the current market or the predicted market?
On the right, try creating sampling distributions of different summary statistics from samples of different distributions. Which distribution does the central limit theorem not apply to?
The central limit theorem states that a sampling distribution of a sample statistic approaches the normal distribution as you take more samples, no matter the original distribution being sampled from.
In this exercise, you’ll focus on the sample mean and see the central
limit theorem in action while examining the num_users
column of amir_deals
more closely, which contains the
number of people who intend to use the product Amir is selling.
Both dplyr
and ggplot2
are loaded and
amir_deals
is available.
num_users
column of
amir_deals
. Use 10 bins.104
.20
with replacement from the
num_users
column of amir_deals
, and take the
mean.sample_means
. This
will take 100 different samples and calculate the mean of each.samples
has been created for you
with a column mean
, which contains the values from
sample_means
. Create a histogram of the mean
column with 10 bins.# Create a histogram of num_users
ggplot(amir_deals, aes(num_users)) +
geom_histogram(bins = 10)
# Set seed to 104
set.seed(104)
# Sample 20 num_users with replacement from amir_deals
sample(amir_deals$num_users, size = 20, replace = TRUE) %>%
# Take mean
mean()
# Set seed to 104
set.seed(104)
# Sample 20 num_users from amir_deals and take mean
sample(amir_deals$num_users, size = 20, replace = TRUE) %>%
mean()
# Repeat the above 100 times
sample_means <- replicate(100, sample(amir_deals$num_users, size = 20, replace = TRUE) %>% mean())
# Set seed to 104
set.seed(104)
# Sample 20 num_users from amir_deals and take mean
sample(amir_deals$num_users, size = 20, replace = TRUE) %>%
mean()
# Repeat the above 100 times
sample_means <- replicate(100, sample(amir_deals$num_users, size = 20, replace = TRUE) %>% mean())
# Create data frame for plotting
samples <- data.frame(mean = sample_means)
# Histogram of sample means
ggplot(samples, aes(mean)) +
geom_histogram(bins = 10)
You want to know what the average number of users
(num_users
) is per deal, but you want to know this number
for the entire company so that you can see if Amir’s deals have more or
fewer users than the company’s average deal. The problem is that over
the past year, the company has worked on more than ten thousand deals,
so it’s not realistic to compile all the data. Instead, you’ll estimate
the mean by taking several random samples of deals, since this is much
easier than collecting data from everyone in the company.
The user data for all the company’s deals is available in
all_deals
.
321
.all_deals$num_users
and
take the mean of each sample. Store the sample means in
sample_means
.sample_means
.num_users
column of
amir_deals
.# edited/added
all_deals <- read.csv('all_deals.csv')
# Set seed to 321
set.seed(321)
# Take 30 samples of 20 values of num_users, take mean of each sample
sample_means <- replicate(30, sample(all_deals$num_users, 20) %>% mean())
# Calculate mean of sample_means
mean(sample_means)
# Calculate mean of num_users in amir_deals
mean(amir_deals$num_users)
Now that you’ve learned about the Poisson distribution, you know that its shape is described by a value called lambda. In this exercise, you’ll match histograms to lambda values.
highest probabilities at 0 and 1
highest probabilities at 3 and 4
highest probabilities at 7 and 8
Your company uses sales software to keep track of new sales leads. It organizes them into a queue so that anyone can follow up on one when they have a bit of free time. Since the number of lead responses is a countable outcome over a period of time, this scenario corresponds to a Poisson distribution. On average, Amir responds to 4 leads each day. In this exercise, you’ll calculate probabilities of Amir responding to different numbers of leads.
# Probability of 5 responses
dpois(5, lambda = 4)
# Probability of 5 responses from coworker
dpois(5, lambda = 5.5)
# Probability of 2 or fewer responses
ppois(2, lambda = 4)
# Probability of > 10 responses
ppois(10, lambda = 4, lower.tail = FALSE)
By this point, you’ve learned about so many different probability distributions that it can be difficult to remember which is which. In this exercise, you’ll practice distinguishing between distributions and identifying the distribution that best matches different scenarios.
To further evaluate Amir’s performance, you want to know how much time it takes him to respond to a lead after he opens it. On average, it takes 2.5 hours for him to respond. In this exercise, you’ll calculate probabilities of different amounts of time passing between Amir receiving a lead and sending a response.
# Probability response takes < 1 hour
pexp(1, rate = 1/2.5)
# Probability response takes > 4 hours
pexp(4, rate = 1/2.5, lower.tail = FALSE)
# Probability response takes 3-4 hours
pexp(4, rate = 1/2.5) - pexp(3, rate = 1/2.5)
Which statement is not true regarding the t-distribution?
In this chapter, you’ll learn how to quantify the strength of a linear relationship between two variables, and explore how confounding variables can affect the relationship between two other variables. You’ll also see how a study’s design can influence its results, change how the data should be analyzed, and potentially affect the reliability of your conclusions.
On the right, use the scatterplot to estimate what the correlation is
between the variables x
and y
. Once you’ve
guessed it correctly, use the New Plot button to try
out a few more scatterplots. When you’re ready, answer the question
below to continue to the next exercise.
Which of the following statements is NOT true about correlation?
In this chapter, you’ll be working with a dataset
world_happiness
containing results from the 2019 World Happiness
Report. The report scores various countries based on how happy
people in that country are. It also ranks each country on various
societal aspects such as social support, freedom, corruption, and
others. The dataset also includes the GDP per capita and life expectancy
for each country.
In this exercise, you’ll examine the relationship between a country’s
life expectancy (life_exp
) and happiness score
(happiness_score
) both visually and quantitatively. Both
dplyr
and ggplot2
are loaded and
world_happiness
is available.
happiness_score
vs. life_exp
using ggplot2
.se
to FALSE
.Based on the scatterplot, which is most likely the correlation
between life_exp
and happiness_score
?
0.3
-0.3
0.8
-0.8
Calculate the correlation between life_exp
and
happiness_score
.
# edited/added
world_happiness <- readRDS('world_happiness_sugar.rds')
# Create a scatterplot of happiness_score vs. life_exp
ggplot(world_happiness, aes(life_exp, happiness_score)) +
geom_point()
# Add a linear trendline to scatterplot
ggplot(world_happiness, aes(life_exp, happiness_score)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE)
# Add a linear trendline to scatterplot
ggplot(world_happiness, aes(life_exp, happiness_score)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE)
# Correlation between life_exp and happiness_score
cor(world_happiness$life_exp, world_happiness$happiness_score)
While the correlation coefficient is a convenient way to quantify the
strength of a relationship between two variables, it’s far from perfect.
In this exercise, you’ll explore one of the caveats of the correlation
coefficient by examining the relationship between a country’s GDP per
capita (gdp_per_cap
) and happiness score.
Both dplyr
and ggplot2
are loaded and
world_happiness
is available.
gdp_per_cap
(on the x-axis) and life_exp
(on
the y-axis).gdp_per_cap
and
life_exp
.The correlation between GDP per capita and life expectancy is 0.7. Why is correlation not the best way to measure the relationship between the two variables?
# Scatterplot of gdp_per_cap and life_exp
ggplot(world_happiness, aes(gdp_per_cap, life_exp)) +
geom_point()
# Scatterplot of gdp_per_cap and life_exp
ggplot(world_happiness, aes(gdp_per_cap, life_exp)) +
geom_point()
# Correlation between gdp_per_cap and life_exp
cor(world_happiness$gdp_per_cap, world_happiness$life_exp)
When variables have skewed distributions, they often require a transformation in order to form a linear relationship with another variable so that correlation can be computed. In this exercise, you’ll perform a transformation yourself.
Both dplyr
and ggplot2
are loaded and
world_happiness
is available.
happiness_score
versus
gdp_per_cap
.happiness_score
and
gdp_per_cap
.world_happiness
called
log_gdp_per_cap
that contains the log of
gdp_per_cap
.happiness_score
versus
log_gdp_per_cap
.happiness_score
and
log_gdp_per_cap
.# Scatterplot of happiness_score vs. gdp_per_cap
ggplot(world_happiness, aes(gdp_per_cap, happiness_score)) +
geom_point()
# Calculate correlation
cor(world_happiness$gdp_per_cap, world_happiness$happiness_score)
# Create log_gdp_per_cap column
world_happiness <- world_happiness %>%
mutate(log_gdp_per_cap = log(gdp_per_cap))
# Scatterplot of happiness_score vs. log_gdp_per_cap
ggplot(world_happiness, aes(log_gdp_per_cap, happiness_score)) +
geom_point()
# Calculate correlation
cor(world_happiness$log_gdp_per_cap, world_happiness$happiness_score)
A new column has been added to world_happiness
called
grams_sugar_per_day
, which contains the average amount of
sugar eaten per person per day in each country. In this exercise, you’ll
examine the effect of a country’s average sugar consumption on its
happiness score.
Both dplyr
and ggplot2
are loaded and
world_happiness
is available.
grams_sugar_per_day
(on the x-axis) and
happiness_score
(on the y-axis).grams_sugar_per_day
and happiness_score
.Based on this data, which statement about sugar consumption and happiness scores is true?
# Scatterplot of grams_sugar_per_day and happiness_score
ggplot(world_happiness, aes(grams_sugar_per_day, happiness_score)) +
geom_point()
# Correlation between grams_sugar_per_day and happiness_score
cor(world_happiness$grams_sugar_per_day, world_happiness$happiness_score)
A study is investigating the relationship between neighborhood residence and lung capacity. Researchers measure the lung capacity of thirty people from neighborhood A, which is located near a highway, and thirty people from neighborhood B, which is not near a highway. Both groups have similar smoking habits and a similar gender breakdown.
Which of the following could be a confounder in this study?
While controlled experiments are ideal, many situations and research questions are not conducive to a controlled experiment. In a controlled experiment, causation can likely be inferred if the control and test groups have similar characteristics and don’t have any systematic difference between them. On the other hand, causation cannot usually be inferred from observational studies, whose results are often misinterpreted as a result.
In this exercise, you’ll practice distinguishing controlled experiments from observational studies.
A company manufactures thermometers, and they want to study the relationship between a thermometer’s age and its accuracy. To do this, they take a sample of 100 different thermometers of different ages and test how accurate they are. Is this data longitudinal or cross-sectional?
Congratulations on completing the course! You now have foundational statistics skills that you can use in your analyses and build upon further.
In the first chapter of the course, you learned about what statistics can do, as well as summary statistics to measure the center and spread of a distribution. In the second chapter, you learned how to measure chance and how to use and interpret probability distributions. You also learned about the binomial distribution. In chapter three, you learned about the normal distribution and the central limit theorem, one of the most important ideas in statistics. You also saw how the Poisson distribution can be used to model countable outcomes. In the final chapter, you saw how to quantify relationships between two variables using correlation. You also learned about controlled experiments and observational studies and the conclusions that can and cannot be drawn from them.
There’s still much more that you can do with statistics and much more to learn. Your new skills will set you up for success in this course on the foundations of regression.
Thanks for accompanying me on this statistical journey. Congratulations again!