【Python】绘制R中线性回归诊断图
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【Python】绘制R中线性回归诊断图,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2567字,纯文字阅读大概需要4分钟。
内容图文
![【Python】绘制R中线性回归诊断图](/upload/InfoBanner/zyjiaocheng/831/df1d95cb2aaa440a92191c20034c189d.jpg)
【参考】
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')
内容总结
以上是互联网集市为您收集整理的【Python】绘制R中线性回归诊断图全部内容,希望文章能够帮你解决【Python】绘制R中线性回归诊断图所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。