绘制Pandas OLS线性回归结果

5 投票
1 回答
13178 浏览
提问于 2025-04-18 00:17

我想知道如何把我用pandas做的线性回归结果画出来。

import pandas as pd
from pandas.stats.api import ols

df = pd.read_csv('Samples.csv', index_col=0)
control = ols(y=df['Control'], x=df['Day'])
one = ols(y=df['Sample1'], x=df['Day'])
two = ols(y=df['Sample2'], x=df['Day'])

我试过用plot(),但是没成功。我想把这三个样本都画在一个图上,有没有pandas或者matplotlib的代码可以处理这些总结格式的数据呢?

总之,结果看起来是这样的:

对照组

------------------------Summary of Regression Analysis-------------------------

Formula: Y ~ <x> + <intercept>

Number of Observations:         7
Number of Degrees of Freedom:   2

R-squared:         0.5642
Adj R-squared:     0.4770

Rmse:              4.6893

F-stat (1, 5):     6.4719, p-value:     0.0516

Degrees of Freedom: model 1, resid 5

-----------------------Summary of Estimated Coefficients------------------------
      Variable       Coef    Std Err     t-stat    p-value    CI 2.5%   CI 97.5%
--------------------------------------------------------------------------------
             x    -0.4777     0.1878      -2.54     0.0516    -0.8457    -0.1097
     intercept    41.4621     2.9518      14.05     0.0000    35.6766    47.2476
---------------------------------End of Summary---------------------------------

-------------------------Summary of Regression Analysis-------------------------

Formula: Y ~ <x> + <intercept>

Number of Observations:         6
Number of Degrees of Freedom:   2

R-squared:         0.8331
Adj R-squared:     0.7914

Rmse:              2.0540

F-stat (1, 4):    19.9712, p-value:     0.0111

Degrees of Freedom: model 1, resid 4

-----------------------Summary of Estimated Coefficients------------------------
      Variable       Coef    Std Err     t-stat    p-value    CI 2.5%   CI 97.5%
--------------------------------------------------------------------------------
             x    -0.4379     0.0980      -4.47     0.0111    -0.6300    -0.2459
     intercept    29.6731     1.6640      17.83     0.0001    26.4116    32.9345
---------------------------------End of Summary---------------------------------

-------------------------Summary of Regression Analysis-------------------------

Formula: Y ~ <x> + <intercept>

Number of Observations:         5
Number of Degrees of Freedom:   2

R-squared:         0.8788
Adj R-squared:     0.8384

Rmse:              1.0774

F-stat (1, 3):    21.7542, p-value:     0.0186

Degrees of Freedom: model 1, resid 3

-----------------------Summary of Estimated Coefficients------------------------
      Variable       Coef    Std Err     t-stat    p-value    CI 2.5%   CI 97.5%
--------------------------------------------------------------------------------
             x    -0.2399     0.0514      -4.66     0.0186    -0.3407    -0.1391
     intercept    24.0902     0.9009      26.74     0.0001    22.3246    25.8559
---------------------------------End of Summary---------------------------------

1 个回答

4

你可能会觉得我这个问题有帮助,如何从Pandas回归中绘制回归线

我试着找一些我用Pandas做的普通最小二乘法(ols)绘图的代码,但找不到。一般来说,使用Statsmodels会更好,因为它对Pandas的数据结构比较了解,所以转换起来不会太难。这样我的回答和提到的例子就更容易理解了。

另见:http://nbviewer.ipython.org/gist/dartdog/9008026

撰写回答