GG274 Homework 8#
Logistics#
Due date: The homework is due 23:59 on Monday, March 9.
You will submit your work on MarkUs. To submit your work:
Download this file (
Homework_8.ipynb) from JupyterHub. (See our JupyterHub Guide for detailed instructions.)Submit this file to MarkUs under the hw8 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 be looking at data concerning the percentage of housing in each Toronto neighbourhood that is “unsuitable” (meaning that the housing is considered to be “overcrowded”, with too many people living in too few rooms).
Task 1 - Explore Neighbourhood Housing Suitability Data#
a) The data for this lab is stored in housing_dwellings_Toronto_2021_7.xlsx - a Microsoft Excel file format with file extension .xlsx.
Use the function pd.read_excel() to read the sheet named Housing Suitability from this spreadsheet into a DataFrame named housing_data.
Use the argument
sheet_nameto select the correct sheet, and set the argumentheaderequal to 9, to remove the explanatory text from the top.
For more information about the purpose of these arguments, refer to the Final Project Data Guide.
import pandas as pd
## Your code here
b) Create a new object named not_suitable that contains the values in the column "Private Households Not in Suitable Accommodation (%)"
## Your code here
c) Calculate the mean and standard deviation of the object not_suitable, using the formulas from last week:
Name these variables population_mean and population_sd
Note: Calculate these values manually, without using
.mean()or.std()
## Your code here
d) Plot a histogram showing the distribution of this variable. Use .axvline() to add a vertical red line marking the mean.
## Your code here
Task 2 – Sample Neighbourhood Data#
a) Take a single sample consisting of 30 Toronto neighbourhoods, setting the random_state argument as 123. Name the resulting dataset not_suitable_sample.
## Your code here
b) Calculate the sample mean and sample standard deviation for this sample, using the formulas below:
Name these variables sample_mean and sample_sd
Note: Calculate these values manually, without using
.mean()or.std()Note that the sample standard deviation divides by \((n - 1)\) instead of \(N\).
## Your code here
c) Plot a histogram showing the distribution of the variable within this sample. Use .axvline() to add a vertical red line marking the sample mean.
## Your code here
Task 3 - Simulating Sample Means#
a) Fill in the for loop in the code chunk below to obtain the sample mean from 1,000 different samples of 30 neighbourhoods.
For results consistent with the answer key, set
random_stateequal toiChoose a brand new name for your sample within the
forloop (do not choosenot_suitable_sample) to avoid overwriting your variable from the previous taskAfter running the
forloop, transformsample_meansinto a pandasSeries
sample_means = []
for i in range(1000):
## Your code here
b) Using a histogram, plot the distribution of the sample means
## Your code here
Task 4 – Sample Statistics#
Using the objects not_suitable_sample, sample_mean, and sample_sd created in Task 2 to complete the following steps:
a) Calculate the Standard Error using the formula below, and save this value in the object standard_error
## Your code here
b) Calculate a 90% Margin of Error using the formula and table provided below, and save this value in the object margin_of_error
Likelihood |
Standard Errors from Sample Mean (z) |
|---|---|
80% |
\(\bar{x}\ \pm\) 1.28 \(SE\) |
90% |
\(\bar{x}\ \pm\) 1.645 \(SE\) |
95% |
\(\bar{x}\ \pm\) 1.96 \(SE\) |
99% |
\(\bar{x}\ \pm\) 2.58 \(SE\) |
## Your code here
c) Calculate the confidence interval associated with the 90% margin of error, based on the formula below:
## Your code here
d) Provide an interpretation of the confidence interval calculated above. What statement can you make about the likelihood that your sample reflects the average share of unsuitable housing units across the entire city?
[Your answer here]
Task 5 – Hypothesis Testing#
Is your sample actually representative of the population mean? In this task, you will test the following Null and Alternative Hypotheses:
Null Hypothesis (\(H_0\)): There is no difference between the two means
Alternative Hypothesis (\(H_a\)): There is a difference between the two means
a) Calculate the difference between the sample mean and the population mean. Save this value as mean_difference.
## Your code here
b) Using the following formula, calculate the t-value associated with the difference between the sample mean and population mean:
## Your code here
c) Based on the t-value obtained above, can you reject the Null Hypothesis for each of the confidence levels below?
In other words, at each confidence level, can you say that the sample mean is different from the population mean?
Confidence Level |
t-value |
|---|---|
80% |
1.28 |
90% |
1.645 |
95% |
1.96 |
99% |
2.58 |
In the following markdown block, select “can” or “cannot” for each line, and explain why you can/cannot reject the null hypothesis at each confidence level.
Fill in the sentences below:
You [can/cannot] reject the null hypothesis at 80%, because ________.
You [can/cannot] reject the null hypothesis at 90%, because ________.
You [can/cannot] reject the null hypothesis at 95%, because ________.
You [can/cannot] reject the null hypothesis at 99%, because ________.