估計差異的顯著性
帶有誤差棒的折線圖可以讓你大致看出趨勢,但逐年之間的差異是否在統計上顯著呢?在這個練習中,你將判定費城的房價中位數年際變化是否顯著。你會評估 2011 年到 2017 年之間逐年的差異。
兩樣本 Z 統計量的公式為:
$$Z = \frac{x_1 - x_2}{\sqrt{SE_{x_1}^2 + SE_{x_2}^2}}$$
提供的 DataFrame philly 含有 median_home_value、median_home_value_moe 與 year 欄位。
已將 pandas 以 pd 匯入,並從 numpy 模組匯入 sqrt 函式。
本練習屬於課程
使用 Python 分析美國 Census 資料
練習說明
- 將
x1設為當年度的房價中位數,將x2設為前一年度(當年度減 1)的房價中位數。 - 將
se_x1設為當年度房價中位數的 MOE 除以Z_CRIT,se_x2以相同方式計算前一年度的值。 - 使用 Python 的三元運算子(
result1 if condition else result2):若z的絕對值大於Z_CRIT,則回傳空字串,否則回傳"not "。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Set the critical Z score for 90% confidence, prepare message
Z_CRIT = 1.645
msg = "Philadelphia median home values in {} were {}significantly different from {}."
for year in range(2012, 2018):
# Assign current and prior year's median home value to variables
x1 = int(philly[philly["year"] == ____]["median_home_value"])
x2 = int(____)
# Calculate standard error as 90% MOE / critical Z score
se_x1 = float(____)
se_x2 = float(____)
# Calculate two-sample z-statistic, output message if greater than critical Z score
z = (x1 - x2) / sqrt(se_x1**2 + se_x2**2)
print(msg.format(year, ____, year - 1))