如何基于statsmodel OLS使用回归线创建散点图?

时间:2018-07-19 03:17:24

标签: python linear-regression seaborn statsmodels

我很难在散点图上添加回归线(statsmodel OLS所基于的那条线)。请注意,使用seaborn的lmplot,我可以得到一条线(请参见示例),但是我想使用来自statsmodel OLS的确切行来实现总体一致性。

如何调整下面的代码以将回归线添加到第一个散点图中?

import statsmodels.regression.linear_model as sm
import seaborn as sns
import pandas as pd
import numpy as np
np.random.seed(0)

data = {'Xvalue': range(20, 30), 'Yvalue': np.random.randint(low=10, high=100, size=10)}

data = pd.DataFrame(data)

X = data[['Xvalue']]
Y = data['Yvalue']
model2 = sm.OLS(Y,sm.add_constant(X), data=data)
model_fit = model2.fit()
print(model_fit.summary())

#Plot
data.plot(kind='scatter', x='Xvalue', y='Yvalue')

#Seaborn
sns.lmplot(x='Xvalue', y='Yvalue', data=data)

散点图(试图找出如何在statsmodel OLS回归行中添加

Scatter plot (trying to work out how to add in the statsmodel OLS regression line

季节性残灯及其回归线(试图模仿)

seaborn lmplot with its regression line (trying to mimic this)

1 个答案:

答案 0 :(得分:0)

感谢@busybear的链接,它现在可以正常工作!

import statsmodels.regression.linear_model as sm
import seaborn as sns
import pandas as pd
import numpy as np
np.random.seed(0)

data = {'Xvalue': range(20, 30), 'Yvalue': np.random.randint(low=10, high=100, size=10)}

data = pd.DataFrame(data)

X = data[['Xvalue']]
Y = data['Yvalue']
model = sm.OLS(data['Yvalue'], sm.add_constant(data['Xvalue']))
model_fit = model.fit()
p = model_fit.params
print(model_fit.summary())


#Plot
p
x = np.arange(0,40)
ax = data.plot(kind='scatter', x='Xvalue', y='Yvalue')
ax.plot(x, p.const + p.Xvalue * x)
ax.set_xlim([0,30])

#Seaborn
sns.lmplot(x='Xvalue', y='Yvalue', data=data)

enter image description here