2014-09-26 86 views
0

我有一个像获取的不确定性值线性回归与蟒蛇

arr = [ 
    [30.0, 0.0257], 
    [30.0, 0.0261], 
    [30.0, 0.0261], 
    [30.0, 0.026], 
    [30.0, 0.026], 
    [35.0, 0.0387], 
    [35.0, 0.0388], 
    [35.0, 0.0387], 
    [35.0, 0.0388], 
    [35.0, 0.0388], 
    [40.0, 0.0502], 
    [40.0, 0.0503], 
    [40.0, 0.0502], 
    [40.0, 0.0498], 
    [40.0, 0.0502], 
    [45.0, 0.0582], 
    [45.0, 0.0574], 
    [45.0, 0.058], 
    [45.0, 0.058], 
    [45.0, 0.058], 
    [50.0, 0.0702], 
    [50.0, 0.0702], 
    [50.0, 0.0698], 
    [50.0, 0.0704], 
    [50.0, 0.0703], 
    [55.0, 0.0796], 
    [55.0, 0.0808], 
    [55.0, 0.0803], 
    [55.0, 0.0805], 
    [55.0, 0.0806], 
] 

一些数据绘制像

Google Charts API

我试图做这个线性回归,即试图找到趋势线的斜率和(y-)截距,以及斜率的不确定性截距的不确定性

Google Charts API已经找到绘制趋势线时的斜率和截距值,但我不知道如何找到不确定性。

我一直在使用LINEST函数Excel这样做,但我觉得这很麻烦,因为我所有的数据都在Python

所以我的问题是,我怎样才能找到LINEST使用Python得到的两个不确定性值?

我很抱歉提出这样的基本问题。

我在PythonJavascript相当不错,但我在回归分析方面很差,所以当我试图在文档中查找它们时,由于条件困难,我很困惑。

我希望能够使用一些着名的Python库,但如果我可以在Google Charts API之内完成这个工作,这将是理想选择。

+0

我认为这可能会帮助你http://stackoverflow.com/questions/11479064/multivariate-linear-regression-in-python/14971531#14971531 – Akavall 2014-09-26 01:58:03

+0

当谈到回归或任何统计方法时,我是一个绝对的新手。不幸的是,这个链接不起作用。抱歉。 – user2418202 2014-09-26 02:08:29

回答

0

它可以使用statsmodels这样进行:

import statsmodels.api as sm 
import numpy as np 


y=[];x=[] 
for item in arr: 
    x.append(item[0]) 
    y.append(item[1]) 

# include constant in ols models, which is not done by default 
x = sm.add_constant(x) 

model = sm.OLS(y,x) 
results = model.fit() 

然后,您可以访问你需要如下的值。截距和斜率由下式给出:

results.params # linear coefficients 
# array([-0.036924 , 0.0021368]) 

我想你指的是标准的错误时,你指的不确定性,他们可以这样访问:

results.bse # standard errors of the parameter estimates 
# array([ 1.03372221e-03, 2.38463106e-05]) 

概述可以通过运行来获得

>>> print results.summary() 
          OLS Regression Results 
============================================================================== 
Dep. Variable:      y R-squared:      0.997 
Model:       OLS Adj. R-squared:     0.996 
Method:     Least Squares F-statistic:      8029. 
Date:    Fri, 26 Sep 2014 Prob (F-statistic):   5.61e-36 
Time:      05:47:08 Log-Likelihood:     162.43 
No. Observations:     30 AIC:       -320.9 
Df Residuals:      28 BIC:       -318.0 
Df Model:       1 
Covariance Type:   nonrobust 
============================================================================== 
       coef std err   t  P>|t|  [95.0% Conf. Int.] 
------------------------------------------------------------------------------ 
const   -0.0369  0.001 -35.719  0.000  -0.039 -0.035 
x1    0.0021 2.38e-05  89.607  0.000   0.002  0.002 
============================================================================== 
Omnibus:      7.378 Durbin-Watson:     0.569 
Prob(Omnibus):     0.025 Jarque-Bera (JB):    2.079 
Skew:       0.048 Prob(JB):      0.354 
Kurtosis:      1.714 Cond. No.       220. 
============================================================================== 

Warnings: 
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified. 

This也可能是对生成的模型的属性的摘要的兴趣。

我没有在Excel中与LINEST进行比较。我也不知道这是否可能只使用Google Charts API。