如何在python中加权站到最小二乘?
发布时间:2020-12-20 13:50:18 所属栏目:Python 来源:网络整理
导读:我有10个关于降水的气候站数据,它是DEM. 我做了一个线性回归跟随: DEM = [200,300,400,500,600,200,100,50,200]Prep = [50,95,59,99,23,10,60]X = DEM #independent variableY = Prep #dependent variableslope,intercept,r_value,p_value,std_err = stats.
我有10个关于降水的气候站数据,它是DEM.
我做了一个线性回归跟随: DEM = [200,300,400,500,600,200,100,50,200] Prep = [50,95,59,99,23,10,60] X = DEM #independent variable Y = Prep #dependent variable slope,intercept,r_value,p_value,std_err = stats.linregress(x,y) 但现在我想为这些电台增加重量,例如: Weight = [0.3,0.1,0.2,0.05,0.05] 该图表就像http://ppt.cc/XXrEv 我找到了加权最小二乘法,但我想知道它是如何工作的,为什么它是错误的. import numpy as np import statsmodels.api as sm Y = [1,3,4,5,2,4] X = range(1,8) X = sm.add_constant(X) wls_model = sm.WLS(Y,X,weights=range(1,8)) results = wls_model.fit() results.params 解决方法
回答:
import numpy as np import statsmodels.api as sm start_time = time.time() alist=[2,6] DEM=[200,600] PRE=[20,19,18,20,21,22,30,23] A_DEM=[] A_PRE=[] W=[] for a in alist: A_DEM.append(DEM[a-1]) A_PRE.append(PRE[a-1]) W.append(1) X = sm.add_constant(A_DEM) Y = A_PRE wls_model = sm.WLS(Y,weights=W).fit() print wls_model.params[0] # intercept print wls_model.params[1] # slope print wls_model.rsquared #rsquared print wls_model.summary() 我发现WLS会自动标准化.所以你可以直接增加重量. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |