Weight Matrices from Distance Matrix
Posted: Tue Feb 24, 2015 3:47 am
Dear All,
Please see some code to create 5 commonly used spatial weighting matrices based on distances (k-nearest, radial distances, power distances, exponential distances and double-power distances). The idea is to use this in conjuction with a well specified distance matrix (in the code below, called distance_haversine):
If there are any questions or requests, please don't hesitate to ask.
Charlie
Please see some code to create 5 commonly used spatial weighting matrices based on distances (k-nearest, radial distances, power distances, exponential distances and double-power distances). The idea is to use this in conjuction with a well specified distance matrix (in the code below, called distance_haversine):
Code: Select all
!rwd_max = 0.5
!pdw_weight=1
!epw_weight=2
!k_nearest=100
!dpd_weight=2
!dpd_max=0.5
matrix(@rows(distance_haversine),@rows(distance_haversine)) knearest_matrix = 0 ' k-Nearest Neighbor Weights.
vector(@rows(distance_haversine)) rankvect
for !p = 1 to @rows(distance_haversine)
rankvect(!p) = !p
next
for !a = 1 to (@rows(distance_haversine))
vector(@rows(distance_haversine)) sorted_ranks_{!a}
sorted_ranks_{!a}=@capplyranks(rankvect,@ranks(@rowextract(distance_haversine,!a) ,"a,","r"))
for !k = 2 to !k_nearest+1
knearest_matrix(!a,sorted_ranks_{!a}(!k))=1
next
d sorted_ranks_{!a}
next
matrix(@rows(distance_haversine),@rows(distance_haversine)) rwd_matrix = 0 'Radial Distance Weights.
matrix(@rows(distance_haversine),@rows(distance_haversine)) dpdw_matrix = 0 ' Double-Power Distance Weights.
matrix(@rows(distance_haversine),@rows(distance_haversine)) pwd_matrix = 0 'Power Distance Weights.
matrix(@rows(distance_haversine),@rows(distance_haversine)) ewd_matrix = 0 'Exponential Distance Weights
for !a = 1 to @rows(distance_haversine)
for !b = !a to @rows(distance_haversine)
if !a<>!b then
if distance_haversine(!a,!b)<!dpd_max then
dpdw_matrix(!a,!b)=(1-(distance_haversine(!a,!b)/!dpd_max)^{!dpd_weight})^{!dpd_weight}
dpdw_matrix(!b,!a)=dpdw_matrix(!a,!b)
endif
if distance_haversine(!a,!b)<!rwd_max then
rwd_matrix(!a,!b) = 1
rwd_matrix(!b,!a) = 1
endif
if distance_haversine(!a,!b)<>0 then
pwd_matrix(!a,!b) = distance_haversine(!a,!b)^(-{!pdw_weight})
pwd_matrix(!b,!a) = pwd_matrix(!a,!b)
ewd_matrix(!a,!b) = @exp(-{!epw_weight}*distance_haversine(!a,!b))
ewd_matrix(!b,!a) = ewd_matrix(!a,!b)
endif
endif
next
next
If there are any questions or requests, please don't hesitate to ask.
Charlie