如何清晰地绘制statsmodels线性回归(OLS) [英] How to plot statsmodels linear regression (OLS) cleanly

查看:427
本文介绍了如何清晰地绘制statsmodels线性回归(OLS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在pandas数据框中有一些不错的数据.我想对其进行简单的线性回归:

I have some nice data in a pandas dataframe. I'd like to run simple linear regression on it:

我使用statsmodels执行回归.现在,我该如何获取情节?我已经尝试过statsmodels的plot_fit方法,但是情节有点时髦:

Using statsmodels, I perform my regression. Now, how do I get my plot? I've tried statsmodels' plot_fit method, but the plot is a little funky:

我希望获得一条表示回归实际结果的水平线.

I was hoping to get a horizontal line which represents the actual result of the regression.

Statsmodels具有各种用于绘制回归的方法(此处有更多详细信息),但它们似乎都不是超级简单的正义图"数据上方的回归线"-plot_fit似乎是最接近的东西.

Statsmodels has a variety of methods for plotting regression (a few more details about them here) but none of them seem to be the super simple "just plot the regression line on top of your data" -- plot_fit seems to be the closest thing.

  • 上面的第一张图片来自pandas的plot函数,该函数返回matplotlib.axes._subplots.AxesSubplot.我可以轻松地在该图上叠加一条回归线吗?
  • 我忽略的statsmodels中有一个函数吗?
  • 是否有更好的方法来汇总这个数字?
  • The first picture above is from pandas' plot function, which returns a matplotlib.axes._subplots.AxesSubplot. Can I overlay a regression line easily onto that plot?
  • Is there a function in statsmodels I've overlooked?
  • Is there a better way to put together this figure?

两个相关问题:

  • Plotting Pandas OLS linear regression results
  • Getting the regression line to plot from a Pandas regression

似乎都没有一个好的答案.

Neither seems to have a good answer.

按照@IgorRaush的要求

As requested by @IgorRaush

        motifScore  expression
6870    1.401123    0.55
10456   1.188554    -1.58
12455   1.476361    -1.75
18052   1.805736    0.13
19725   1.110953    2.30
30401   1.744645    -0.49
30716   1.098253    -1.59
30771   1.098253    -2.04

abline_plot

我已经尝试过了,但是它似乎不起作用...不知道为什么:

abline_plot

I had tried this, but it doesn't seem to work... not sure why:

推荐答案

正如我在评论中提到的,seaborn是统计数据可视化的绝佳选择.

As I mentioned in the comments, seaborn is a great choice for statistical data visualization.

import seaborn as sns

sns.regplot(x='motifScore', y='expression', data=motif)

或者,您可以使用statsmodels.regression.linear_model.OLS并手动绘制回归线.

Alternatively, you can use statsmodels.regression.linear_model.OLS and manually plot a regression line.

import statsmodels.api as sm

# regress "expression" onto "motifScore" (plus an intercept)
model = sm.OLS(motif.expression, sm.add_constant(motif.motifScore))
p = model.fit().params

# generate x-values for your regression line (two is sufficient)
x = np.arange(1, 3)

# scatter-plot data
ax = motif.plot(x='motifScore', y='expression', kind='scatter')

# plot regression line on the same axes, set x-axis limits
ax.plot(x, p.const + p.motifScore * x)
ax.set_xlim([1, 2])

另一种解决方案是statsmodels.graphics.regressionplots.abline_plot,它消除了上述方法的一些样板.

Yet another solution is statsmodels.graphics.regressionplots.abline_plot which takes away some of the boilerplate from the above approach.

import statsmodels.api as sm
from statsmodels.graphics.regressionplots import abline_plot

# regress "expression" onto "motifScore" (plus an intercept)
model = sm.OLS(motif.expression, sm.add_constant(motif.motifScore))

# scatter-plot data
ax = motif.plot(x='motifScore', y='expression', kind='scatter')

# plot regression line
abline_plot(model_results=model.fit(), ax=ax)

这篇关于如何清晰地绘制statsmodels线性回归(OLS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆