我有一些像
这样的数据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
中的两个不确定性值?
我为这样一个基本问题而道歉。
我很擅长Python
和Javascript
,但我在回归分析方面很差,所以当我试图在文档中查找它们时,由于条款的困难,我很困惑
我希望使用一些着名的Python
库,但如果我能在Google Charts API
内完成此操作,那将是理想的。
答案 0 :(得分: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。