numpy.polyfit系数的t值和Pr(> | t |)

时间:2014-03-07 03:43:35

标签: numpy statistics scipy regression rpy2

我想使用numpy.polyfit确定适合某些数据的多项式模型中系数的重要性。

This是我想用R实现的一个例子。基本上,我需要得到R的“摘要”函数输出scipy / numpy。是否有一种简单的方法可以使用scipy / numpy(一些内置辅助函数?)或者我应该使用rpy代替吗?

1 个答案:

答案 0 :(得分:1)

使用statsmodels:

import pandas as pd
import statsmodels.formula.api as smf
df = pd.DataFrame(
    {"year":[1959, 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969],
    "population":[4835, 4970, 5085, 5160, 5310, 5260, 5235, 5255, 5235, 5210, 5175]})
df.year -= 1964

results = smf.ols('population ~ year + I(year**2)', data=df).fit()
print results.summary()

这是输出:

                            OLS Regression Results                            
==============================================================================
Dep. Variable:             population   R-squared:                       0.941
Model:                            OLS   Adj. R-squared:                  0.926
Method:                 Least Squares   F-statistic:                     63.48
Date:                Fri, 07 Mar 2014   Prob (F-statistic):           1.23e-05
Time:                        14:53:22   Log-Likelihood:                -54.089
No. Observations:                  11   AIC:                             114.2
Df Residuals:                       8   BIC:                             115.4
Df Model:                           2                                         
================================================================================
                   coef    std err          t      P>|t|      [95.0% Conf. Int.]
--------------------------------------------------------------------------------
Intercept     5263.1585     17.655    298.110      0.000      5222.446  5303.871
year            29.3182      3.696      7.933      0.000        20.796    37.841
I(year ** 2)   -10.5886      1.323     -8.002      0.000       -13.640    -7.537
==============================================================================
Omnibus:                       10.349   Durbin-Watson:                   1.669
Prob(Omnibus):                  0.006   Jarque-Bera (JB):                4.954
Skew:                           1.379   Prob(JB):                       0.0840
Kurtosis:                       4.789   Cond. No.                         20.2
==============================================================================