0

I would like to create a distance based spatial weights matrix in pysal that is later fed into the spatial regression methods of the "spreg" submodule of pysal. The following is a small reproducible code example:

import numpy as np 
import pysal
position=np.random.standard_normal((1000,2))
y_=np.random.standard_normal((1000,1))
X_=np.random.standard_normal((1000,10))   
kd = pysal.lib.cg.KDTree(position)
spweights = pysal.lib.weights.Kernel(kd, bandwidth=None, fixed=False, k=4,
                     function='triangular', eps=1.000000, ids=None,
                     diagonal=False, distance_metric='euclidean', radius=None)
models_GM_Error_Het=pysal.model.spreg.GM_Error_Het(y=y_,x=X_,w=spweights,max_iter=2,epsilon=1e-8)

The error python returns is "Exception: All entries on diagonal must equal 0." On the other hand, using only k-nearest neighbour weights with binary weights only,

import numpy as np 
import pysal
position=np.random.standard_normal((1000,2))
y_=np.random.standard_normal((1000,1))
X_=np.random.standard_normal((1000,10))   
kd = pysal.lib.cg.KDTree(position)
spweights2 = pysal.lib.weights.KNN(kd, 4)
models_GM_Error_Het=pysal.model.spreg.GM_Error_Het(y=y_,x=X_,w=spweights2,max_iter=2,epsilon=1e-8)

works finely. How do I modify the above code?

user823
  • 265
  • 2
  • 14

1 Answers1

0

In the insertion of the following in the penultimate line helped:

for k in range(position.shape[0]):
    spweights2.weights[k][0]=0.0
spweights2.transform = 'r'

The diagonal elements must be zero. Furthermore, the weights must be row-standardized. The program runs now finely.

user823
  • 265
  • 2
  • 14