How to plot statsmodels linear regression (OLS) cleanly

前端 未结 1 1647
星月不相逢
星月不相逢 2020-12-13 20:43

Problem Statement:

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

Using statsmodels, I perform my r

相关标签:
1条回答
  • 2020-12-13 21:05

    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)
    


    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])
    


    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)
    

    0 讨论(0)
提交回复
热议问题