GG274 Homework 9#

Logistics#

Due date: The homework is due 23:59 on Monday, March 16.

You will submit your work on MarkUs. To submit your work:

  1. Download this file (Homework_9.ipynb) from JupyterHub. (See our JupyterHub Guide for detailed instructions.)

  2. Submit this file to MarkUs under the hw9 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 using Toronto neighbourhood data to examine the following question:

What is the relationship between the share of neighbourhood residents with asthma vs. the share of neighbourhood residents who are immigrants?

Task 1: Prepare Data For Analysis#

Task 1a: Load Data#

The data for this lab is stored in two files: population_characteristics_Toronto_2021_7.xlsx and AHD_Asthma_FY2022.xlsx. Use the function pd.read_excel() to read the correct sheets from each file:

  • Read the sheet named Immigrant Status from population_characteristics_Toronto_2021_7.xlsx, setting the argument header as 9. Save this Data Frame as immigration

  • Read the sheet named Asthma_NHsTOR from AHD_Asthma_FY2022.xlsx, setting the argument header as [2, 3] (there are multiple headers). Save this DataFrame as asthma.

import pandas as pd

## Your code below

Task 1b: Prepare immigrant data#

Create a new list named imm_headers, containing the header names Neighbourhood Name, and Population who are Immigrants (%)

Then, create a new DataFrame named imm_subset, which only contains the column names from imm_headers.

## Your code here

Task 1c: Start preparing asthma data#

The asthma dataset has multiple headers, so it will require a bit more processing

  • First, create a new list named asthma_top_headers, containing the header names Neighbourhood and Age-Standardized rate (/100) of Asthma (2021/22), All Ages 0+

  • Second, create a new DataFrame named asthma_top_subset, containing only the columns from asthma with the top-level header names from asthma_top_headers

## Your code here

Once you have created asthma_top_subset, you can run the following line of code to drop the top-level header names ("Neighbourhood" and "Age-Standardized rate (/100) of Asthma (2021/22), All Ages 0+"). This will leave only the names of the columns within those categories.

asthma_top_subset.columns = asthma_top_subset.columns.droplevel(0) ## DO NOT DELETE!

Task 1d: Finish preparing asthma data#

Once you have successfully completed Task 1c:

  • Create a new list named asthma_top_subset_headers, containing "Neighbourhood Name", and "Total"

  • Finally, create a new DataFrame named asthma_subset, containing only the columns from asthma_top_subset with the header names from asthma_top_subset_headers

## Your code here

Task 1e: Merging DataFrames#

Merge imm_subset and asthma_subset, joining on the column Neighbourhood Name from both datasets. Name this merged DataFrame df.

## Your code here

Task 1f: Rename columns#

Create a dictionary called new_column_names, with the following contents:

  • "Neighbourhood Name": "neighbourhood"

  • "Population who are Immigrants (%)": "share_immigrant"

  • "Total": "asthma_rate"

Then, use .rename() with new_column_names to rename the columns in df

## Your code here

Task 2: Two-Sample Difference of Means#

In this task, you will ask the question: are asthma rates in neighbourhoods with a majority (>50%) immigrants statistically different from asthma rates in other neighbourhoods?

Task 2a: Generate Categories#

Create a new boolean column named "majority_immigrant" that is True if share_immigrant is greater than 50, and False otherwise.

## Your code here

Task 2b: Subset Data#

Create two lists:

  • majority_imm_asthma, containing the values of the asthma_rate column from df where majority_immigrant is True

  • minority_imm_asthma, containing the values of the asthma_rate column from df where majority_immigrant is False

## Your code here

Task 2c: Conduct t-test#

Run stats.ttest_ind() to compare majority_imm_asthma and minority_imm_asthma. Include the argument equal_var = False. Save the output of the t-test to asthma_test.

from scipy import stats

## Your code below

Task 2d: Interpret results#

Answer the following question below:

  • Based on the t-statistic and p-value, what is the highest level of confidence (90%, 95%, or 99%) at which you can say that there is a statistically significant difference between asthma rates in majority-immigrant neighbourhoods compared with other neighbourhoods?

Your answer here

Task 3: Correlation#

Task 3a: Plot Relationship#

Create a scatterplot showing the relationship between the columns share_immigrant and asthma_rate from df.

## Your code here

Task 3b: Correlation#

Using statistics.correlation(), calculate the correlation between the columns "share_immigrant" and "asthma_rate" from df. Assign this output to asthma_imm_corr.

import statistics

## Your code below

Task 3c: Interpret Results#

Answer the following question below: Based on this correlation coefficient, what is the relationship between the share of immigrants and asthma rates? Are these variables strongly or weakly correlated?

Your answer here

Task 4: Linear Regression#

Task 4a: Conduct regression#

Using ols() and fit(), run a regression model where asthma_rate is the dependent variable (y) and share_immigrant is the independent variable (x). Save this output as reg_model

from statsmodels.formula.api import ols

## Your code below

Task 4b: Interpret results#

Use .summary().tables[1] to pull out the coefficients, and answer the questions below:

  • What does the value of the Intercept coefficient signify in this context?

    Your Answer Here

  • What does the value of the share_immigrant coefficient signify in this context?

    Your Answer Here

## Your code here

Task 4c: Interpret R-squared#

Obtain the r-squared value from reg_model, and answer the question below:

  • What does this value of r-squared signify? Does the share of immigrants explain a substantial share of asthma rates, or not?

    Your Answer Here

## Your code here