檢定相關性
你想了解拉斯維加斯與休士頓的房租價格是否具有相關性。當你看到拉斯維加斯的房租上漲時,是否可以合理推測休士頓也在上漲?已為你載入了拉斯維加斯(lasvegas_rents)與休士頓(houston_rents)的房租 NumPy 陣列,以及每次量測所對應的 dates。
pandas 作為 pd、NumPy 作為 np、Matplotlib 作為 plt,以及 SciPy 的 stats 套件都已為你載入。
本練習屬於課程
Python 推論基礎
練習說明
- 建立折線圖,x 軸使用
dates,並畫出兩條線:一條對應houston_rents,另一條對應lasvegas_rents。 - 計算 Pearson 相關係數與其對應的 p 值。
- 判斷並列印一個布林值,表示 p 值在 5% 顯著水準下是否達到顯著。
- 列印 R-squared。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# 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
____