标签:plt Python lm results set resids 线性 import 绘制
【参考】
2. 6 ways to run a "simple" regression(使用6种工具)
(1)原文:https://underthecurve.github.io/jekyll/update/2016/07/01/one-regression-six-ways.html#Python
(2)脚本:https://github.com/OpenNewsLabs/one-regression-six-ways/blob/master/Python/statsmodels_method.py
【代码】
# import modules
import os
import math
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import statsmodels.api as sm
import statsmodels.formula.api as smf
# set working directory - modify as necessary
# os.chdir('/Users/christinezhang/Projects/regression/Python')
# read data
d = pd.read_csv('data.csv')
# log transformation of income
d['log_income'] = np.log(d['income'])
# run regression
lm = smf.ols(formula = 'health ~ log_income', data = d).fit()
print(lm.summary())
# assess the regression model
## put residuals (raw & standardized) plus fitted values into a data frame
results = pd.DataFrame({'country': d.country,
'resids': lm.resid,
'std_resids': lm.resid_pearson,
'fitted': lm.predict()})
print(results.head())
## raw residuals vs. fitted
residsvfitted = plt.plot(results['fitted'], results['resids'], 'o')
l = plt.axhline(y = 0, color = 'grey', linestyle = 'dashed')
plt.xlabel('Fitted values')
plt.ylabel('Residuals')
plt.title('Residuals vs Fitted')
plt.show(residsvfitted)
## q-q plot
qqplot = sm.qqplot(results['std_resids'], line='s')
plt.show(qqplot)
## scale-location
scalelocplot = plt.plot(results['fitted'], abs(results['std_resids'])**.5, 'o')
plt.xlabel('Fitted values')
plt.ylabel('Square Root of |standardized residuals|')
plt.title('Scale-Location')
plt.show(scalelocplot)
## residuals vs. leverage
residsvlevplot = sm.graphics.influence_plot(lm, criterion = 'Cooks', size = 2)
plt.show(residsvlevplot)
# 4 plots in one window
fig = plt.figure(figsize = (8, 8), dpi = 100)
ax1 = fig.add_subplot(2, 2, 1)
ax1.plot(results['fitted'], results['resids'], 'o')
l = plt.axhline(y = 0, color = 'grey', linestyle = 'dashed')
ax1.set_xlabel('Fitted values')
ax1.set_ylabel('Residuals')
ax1.set_title('Residuals vs Fitted')
ax2 = fig.add_subplot(2, 2, 2)
sm.qqplot(results['std_resids'], line='s', ax = ax2)
ax2.set_title('Normal Q-Q')
ax3 = fig.add_subplot(2, 2, 3)
ax3.plot(results['fitted'], abs(results['std_resids'])**.5, 'o')
ax3.set_xlabel('Fitted values')
ax3.set_ylabel('Sqrt(|standardized residuals|)')
ax3.set_title('Scale-Location')
ax4 = fig.add_subplot(2, 2, 4)
sm.graphics.influence_plot(lm, criterion = 'Cooks', size = 2, ax = ax4)
plt.tight_layout()
fig.savefig('regplots.png')
标签:plt,Python,lm,results,set,resids,线性,import,绘制 来源: https://blog.csdn.net/qq_34105362/article/details/89553002
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。