4

I have 10 climate stations data about precipitation and it's DEM.

I had done a linear regression follow:

DEM = [200, 300, 400, 500, 600, 300, 200, 100, 50, 200]
Prep = [50, 95, 50, 59, 99, 50, 23, 10, 10, 60]
X = DEM   #independent variable
Y = Prep  #dependent variable
slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)

But now I want to add weight to those stations like:

Weight = [0.3, 0.1, 0.1, 0.1, 0.2, 0.05, 0.05, 0.05, 0.05, 0.05]

The diagram is like http://ppt.cc/XXrEv

I found Weighted Least Squares to do it, but I want to know how and why it work or if it is wrong.

import numpy as np
import statsmodels.api as sm

Y = [1, 3, 4, 5, 2, 3, 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
Mauro Baraldi
  • 6,346
  • 2
  • 32
  • 43
楊宇恩
  • 99
  • 2
  • 11
  • If you want to know how (and why) weighted least squares work, then it's probably better to do a statistics course: SO is not really the place for these kind of questions. –  Jun 10 '15 at 02:41
  • 1
    The one thing you will need to consider is what the weights represent: if they are errors (standard deviations), the weights should be the inverse of the variances (1 / errors**2). The example you give here, with `weights=range(1, 8)`, appears rather unrealistic, as it means that the errors decrease with increasing `X`. –  Jun 10 '15 at 02:46
  • Thanks for your replay,that is helpful. – 楊宇恩 Jun 10 '15 at 06:21

1 Answers1

1

Answer:

import numpy as np
import statsmodels.api as sm
start_time = time.time()
alist=[2,4,6]
DEM=[200,300,400,500,300,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,X, weights=W).fit()

print wls_model.params[0] #  intercept
print wls_model.params[1] #  slope
print wls_model.rsquared  #rsquared
print wls_model.summary()

And I found the WLS will auto normalize.So you can add weight direct.

楊宇恩
  • 99
  • 2
  • 11