import wooldridge as wooimport numpy as npimport pandas as pdimport statsmodels.formula.api as smfimport matplotlib.pyplot as plthprice2 = woo.dataWoo('hprice2')# repeating the regression from Example 6.2:reg = smf.ols(formula='np.log(price) ~ np.log(nox)+np.log(dist)+rooms+I(rooms**2)+stratio',data=hprice2)results = reg.fit()# predictions with rooms = 4-8, all others at the sample mean:nox_mean = np.mean(hprice2['nox'])dist_mean = np.mean(hprice2['dist'])stratio_mean = np.mean(hprice2['stratio'])X = pd.DataFrame({'rooms': np.linspace(4, 8, num=5),'nox': nox_mean,'dist': dist_mean,'stratio': stratio_mean})print(f'X: \n{X}\n')# calculate 95% confidence interval:lpr_PICI = results.get_prediction(X).summary_frame(alpha=0.05)lpr_CI = lpr_PICI[['mean', 'mean_ci_lower', 'mean_ci_upper']]print(f'lpr_CI: \n{lpr_CI}\n')# plot:plt.plot(X['rooms'], lpr_CI['mean'], color='black',linestyle='-', label='')plt.plot(X['rooms'], lpr_CI['mean_ci_upper'], color='lightgrey',linestyle='--', label='upper CI')plt.plot(X['rooms'], lpr_CI['mean_ci_lower'], color='darkgrey',linestyle='--', label='lower CI')plt.ylabel('lprice')plt.xlabel('rooms')plt.legend()plt.savefig('PyGraphs/Effects-Manual.pdf')
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。