GG274 Homework 7: Statistical Distributions#
Logistics#
Due date: The homework is due 23:59 on Monday, March 2.
You will submit your work on MarkUs. To submit your work:
Download this file (
Homework_7.ipynb) from JupyterHub. (See our JupyterHub Guide for detailed instructions.)Submit this file to MarkUs under the hw7 assignment. (See our MarkUs Guide for detailed instructions.) All homeworks will take place in a Jupyter notebook (like this one). When you are done, you will download this notebook and submit it to MarkUs.
Introduction#
For this week’s homework, you will investigate statistical distributions based on real-life data. In this case, you’ll be investigating a new data source: the General Social Survey.
This survey asks Canadians a bunch of questions about their daily life! For this homework, you’ll be focusing on two aspects of how people spend their time:
Total number of minutes sleeping per day (
SLEEPDUR)Number of minutes per day in transit (by car, public transit, biking, walking, etc.) (
TRANSDUR)
Step 1: Read Data#
Create the following two pandas DataFrames:
gss_raw: theDataFramecreated by reading thegss_2022.csvfile.gss: a version ofgss_rawcontaining only the following columns:SLEEPDUR,TRANSDUR, and PRV
Note: PRV is needed for step 8 and 9 and was omitted from the original version of the homework which is why it is shown in red.
import pandas as pd
# Write your code here
Step 2: Rename Columns#
Create a dictionary called new_column_names, and then use this dictionary to rename the columns in gss as follows:
"SLEEPDUR":"Sleep (Minutes)""TRANSDUR":"Transport (Minutes)"”PRV”: “Province Code”
Note: The name change for PRV is needed for Step 8 and 9 and was omitted from the original version of this homework, which is why it is in red.
# Write your code here
Step 3: Transform Data#
These variables are represented in minutes, but it would be much more convenient to interpret them in hours. Create new columns in gss named "Sleep (Hours)" and "Transport (Hours)", containing the values of Sleep (Minutes) and Transport (Minutes) converted into hours.
Also save the values of these columns in new objects: sleep_hours and transport_hours.
# Write your code here
Step 4: Calculate Means#
a) Calculate the mean (average) number of hours spent sleeping. Assign this to a new object named sleep_mean
b) Calculate the mean (average) number of hours spent in transport. Assign this to a new object named transport_mean
Calculate these manually, without using
.mean()
# Write your code here
Step 5: Calculate Standard Deviations#
Using the formula, calculate the standard deviations of these columns
Calculate these values manually, following the formula above and the example from lecture!
Remember that square root is the same as the exponent 0.5
a) Calculate the standard deviation of gss["Sleep (Hours)"]. Name the result sleep_sd.
# Write your code here
b) Calculate the standard deviation of gss["Transport (Hours)"]. Name the result transport_sd.
# Write your code here
Step 6: Compare Distributions#
For this question, you will plot the distribution of the two main variables.
a) Produce a plot showing the distribution of total number of hours spent sleeping each day.
# Write your code here
b) Produce a plot showing the distribution of total number of hours spent in transit each day.
# Write your code here
c) Describe and compare these distributions in 3-4 sentences, answering the following questions:
Where is the mode (the category/bin with the largest number of observations) for each distribution?
Is the mean of each distribution located near the mode, or far away from the mode?
For each distribution, is the distribution evenly distributed, or is it “left-skewed” (with a long “tail” to the left) or “right-skewed” (with a long “tail” to the right)?
Conceptually, why might one of these variables be more skewed than the other one?
[Your Answer Here]
Step 7: Customizing Plots#
In this final section, you will customize your plots!
Include the following arguments within your plot function, and experiment with different colour choices:
bins = ____– Insert a list (e.g.[0, 2, 4, etc.]) or a range(e.g.range(0, 24, 1)), with bins of your choiceedgecolor = "___"– Choose your own colourcolor = "___"– Choose your own colour
Below the line for creating the plot, include the following commands from
matplotlib.pyplot(each appearing on a separate line):plt.xlabel("___"), with a descriptive label for the x-axisplt.ylabel("___"), with a descriptive label for the y-axisplt.title("___"), with a descriptive plot title
import matplotlib.pyplot as plt
# Write your code here
Step 8: Hours spent in transport by province#
First let’s map the province codes to their names. The line that actually does the mapping is a bit more complicated because of how numbers are stored.
.astype(str)says to interpret the cells in the column as strings.str.strip()removes any whitespace.map(province_codes)does the work of matching the province code given as a key in theprovince_codesdictionary to the name of each province
# run this cell
province_codes = {
"10" : "Newfoundland and Labroador",
"11" : "Prince Edward Island",
"12" : "Nova Scotia",
"13" : "New Brunswick",
"24" : "Quebec",
"35" : "Ontario",
"46" : "Manitoba",
"47" : "Saskatchewan",
"48" : "Alberta",
"59" : "British Columbia"
}
gss["Province"] = gss["Province Code"].astype(str).str.strip().map(province_codes)
gss.head()
Question: Of the people who spent time travelling to and/or from an activty, what is the mean time they spend travelling?
First, select the rows where “Transport (Hours)” is greater than 0, save this data frame to a new variable called gss_transport_nonzero. (Use .loc[] and a boolean exporession to achieve this part.)
Then, compute the mean and save it to a variable gss_transport_nonzero_mean
# write your code here
# check your work
print(gss_transport_nonzero.head())
print(gss_transport_nonzero_mean)
Step 9: How do Transport time averages compare across provinces?#
To answer this question, use .groupby(). Using the pattern from this week’s lecture fill in the missing pieces.
df.groupby("category")["column of interest"].aggregator()
dfisgss_transport_nonzero“category” is ??
“column of interest” is ??
“.aggregator()” is ??
Save the result in a variable called province_transport_means
# write your code here
# check your work
print(province_transport_means)
Marking Rubric#
Section |
0 |
1 |
2 |
3 |
|---|---|---|---|---|
Computational questions including visualizations (for each part) |
auto test fails |
auto test passes |
NA |
NA |
Qualitative questions (for each part) |
No answer |
The question is answered but no explanation is given |
The question is answered but the explanation is irrelevant or not supported |
The question is answered and the explanation is supported |