我在熊猫数据框中有一些不错的数据。我想对其进行简单的线性回归:
使用统计模型,我执行回归。现在,我如何获得我的情节?我试过statsmodels的
plot_fit
方法,但是剧情有点funky:
我希望得到一条代表回归实际结果的水平线。
Statsmodels 有多种绘制回归的方法(这里有一些关于它们的更多细节)但它们似乎都不是超级简单的“只需在数据之上绘制回归线”——
plot_fit
似乎是最接近的东西。
matplotlib.axes._subplots.AxesSubplot
。我可以轻松地将回归线叠加到该图上吗?两个相关问题:
似乎都没有一个好的答案。
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
我试过这个,但它似乎不起作用......不知道为什么:
正如我在评论中提到的,
seaborn
是统计数据可视化的绝佳选择。
import seaborn as sns
sns.regplot(x='motifScore', y='expression', data=motif)
或者,您可以使用
statsmodels.regression.linear_model.OLS
并手动绘制回归线。
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
,它从上述方法中删除了一些样板。
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)
我同意@Igor Rauch 的观点,即在绘制简单的拟合回归线时,seaborn 非常容易使用(特别是因为 OLS 拟合是在引擎盖下完成的)。
使用seaborn,你可以关闭
ci
,分别传递线和散点的kwargs。
import pandas as pd
import seaborn as sns
df = pd.DataFrame({
'motifScore': [1.401123, 1.188554, 1.476361, 1.805736, 1.110953, 1.744645, 1.098253, 1.098253],
'expression': [0.55, -1.58, -1.75, 0.13, 2.3, -0.49, -1.59, -2.04]})
sns.regplot(x='motifScore', y='expression', data=df, ci=False, line_kws={'color': 'red'}, scatter_kws={'s': 20, 'alpha': 0.7});
相关的
statsmodels
方法是abline_plot()
。它使用 matplotlib.lines.Line2D
构造引擎盖下的拟合线;因此,如果轴限制设置不当,则该线可能不会显示。例如,对于 ((0,1), (0,1)) 的默认限制,样本数据根本不会显示拟合线。
import statsmodels.api as sm
X = sm.add_constant(df['motifScore'])
y = df['expression']
results = sm.OLS(y, X).fit()
fig = sm.graphics.abline_plot(model_results=results, color='red')
fig.axes[0].set(ylim=(-1,0), xlim=(1,2))
没有绘制原始数据,所以必须单独绘制。由于 abline 是一条拟合线,它可能无论如何都会穿过分散的标记,因此无需调整轴限制。请注意,最好在
abline_plot()
之前绘制散点图以获得更明确定义的轴限制。
import matplotlib.pyplot as plt
plt.scatter(df['motifScore'], df['expression'])
fig = sm.graphics.abline_plot(model_results=results, color='red', ax=plt.gca())
如果您想坚持
statsmodels.graphics
,还有另一个值得一试的绘图仪:plot_ccpr()
。因为这绘制了 CCPR,它的主要功能是查看特定回归变量对因变量的影响(为模型x against b*x
绘制y=a+b*x
),它将被常数项关闭。如果 y-ticks 不重要,它很有用。
fig = sm.graphics.plot_ccpr(results, 'motifScore')
# the above is the same as the following (uncomment to see it drawn)
# notice that results.params.const is missing from y
# fig.axes[0].plot(range(1,3), [results.params['motifScore']*i for i in range(1,3)]);