How to build statsmodels linear regression (OLS) correctly

Problem Statement:

I have good data in the pandas framework. I would like to run a simple linear regression on it:

enter image description here

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

enter image description here

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

Statsmodels has many methods for constructing a regression ( a few details about them here ), but none of them seem to be super simple "just draw a regression line on top of your data" - plot_fitit seems the closest.

Questions:

  • pandas ', a matplotlib.axes._subplots.AxesSubplot. ?
  • statsmodels, ?
  • ?

:

.

@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

, , , ... , :

enter image description here

+6
1

, seaborn - .

import seaborn as sns

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

sns.regplot


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 = df.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])

manual


- 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 = df.plot(x='motifScore', y='expression', kind='scatter')

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

abline_plot

+7

Source: https://habr.com/ru/post/1015011/


All Articles