相关性检验
您想判断拉斯维加斯和休斯敦的房租价格是否存在相关性。如果拉斯维加斯的价格在上涨,是否可以合理地假设休斯敦也在上涨?已为您加载了表示拉斯维加斯(lasvegas_rents)和休斯敦(houston_rents)房租的 NumPy 数组,以及与每次测量对应的 dates。
已经为您加载了 pandas(别名 pd)、NumPy(别名 np)、Matplotlib(别名 plt),以及 SciPy 中的 stats 包。
本练习是课程的一部分
Python 推断基础
练习说明
- 使用以
dates为 x 轴创建一张折线图,包含两条线: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
____