始める無料で始める

推定値の差の有意性

誤差バー付きの折れ線グラフは傾向のおおよその様子を示しますが、年ごとの差は統計的に有意でしょうか?この演習では、Philadelphia における住宅価格の中央値の変化の有意性を判定します。2011年から2017年までの各年の差を評価します。

2標本Z統計量の式は次のとおりです。

$$Z = \frac{x_1 - x_2}{\sqrt{SE_{x_1}^2 + SE_{x_2}^2}}$$

philly という DataFrame が用意されており、median_home_valuemedian_home_value_moeyear の列が含まれます。

pandaspd として、numpy モジュールからは sqrt 関数がインポート済みです。

この演習はコースの一部です

Pythonで学ぶ米国センサスデータ分析

コースを見る

演習の手順

  • 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))
コードを編集して実行