相関の検定
ラスベガスとヒューストンの家賃が相関しているかどうかを確認したいとします。ラスベガスで家賃が上がっているとき、ヒューストンでも上がっていると考えるのは妥当でしょうか? ラスベガス(lasvegas_rents)とヒューストン(houston_rents)の家賃を格納した NumPy 配列と、各測定に対応する dates が読み込まれています。
pandas は pd、NumPy は np、Matplotlib は plt、そして SciPy の stats パッケージが読み込まれています。
この演習はコースの一部です
Pythonで学ぶ推測の基礎
演習の手順
- x軸に
datesを使い、houston_rentsとlasvegas_rentsをそれぞれ1本ずつの線で表す折れ線グラフを作成します。 - ピアソンの相関係数とその p値を計算します。
- p値が5%水準で有意かどうかを示すブール値を判定して表示します。
- R二乗を表示します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Create a line graph showing the rents for both San Francisco and Las Vegas
____(____, ____, label='Houston')
____(____, ____, label='Las Vegas')
plt.show()
# Compute the Pearson correlation coefficient R, as well as the p-value
r, p_value = ____(____, ____)
# Print if the p-value is less than alpha = 5%
____
# Print out R-squared
____