2014-09-26 88 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。