<jupyter_start><jupyter_text>Question 81 - Relationship between fitness and smokingGiven the data table below, determine if there is a relationship between fitness level and smoking habits:| |Low fitness level|Medium-low fitness level|Medium-high fitness level|High fitness level||---|---|---|---|---||Never smoked |113 |113 |110 |159||Former smokers |119 |135 |172 |190||1 to 9 cigarettes daily |77 |91 |86 |65||>=10 cigarettes daily |181 |152 |124 |73|You don't have to fully solve for the number here (that would be pretty time-intensive for an interview setting), but lay out the steps you would take to solve such a problem.<jupyter_code>import numpy as np
from scipy import stats

never = np.array([113,113,110,159])
heavy = np.array([181,152,124,73])
print(stats.chisquare(never,heavy))


# do we need to correct for different N?
# answer: seems like no
print(sum(never), sum(heavy))
heavy_adj = heavy/sum(heavy)*sum(never)
stats.chisquare(never,heavy_adj)
# include all columns at once
former = np.array([119,135,172,190])
casual = np.array([77,91,86,65])
data = np.array([never,former,casual,heavy])
stats.chi2_contingency(data)
# how much precision would we lose if we binned the 4x4 table into 2x2?
# answer: not much

nosmoke_low = 113+119+113+135
smokes_low = 77+181+91+152
nosmoke_hi = 110+172+159+190
smokes_hi = 86+65+124+73

# chi-square test: are smokers exercising as much as non-smokers?
expected_fitness = [nosmoke_low, nosmoke_hi]
observed_fitness = [smokes_low, smokes_hi]
stats.chisquare(observed_fitness, expected_fitness)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1192136968928345, 'fraction_numerical': 0.08877615726062144, 'mean_word_length': 5.982300884955753, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '168130', 'license': 'Unlicense', 'n_tokens_mistral': 628, 'n_tokens_neox': 507, 'n_words': 170, 'path': 'interviewq_exercises/q081_stats_chi2_fitness_smoking.ipynb', 'repo_name': 'gentimouton/dives', 'size': '1577'}