Dear All,
I am using Stata 15 to carry out my spatial analysis. I generate an inverse distance function using spmatrix idistance.
Specifically I type:
spmatrix create idistance dW, replace
spmatrix export dW using dW.txt, replace
Then, I import the file in Stata:
insheet using "dW.txt", delim(" ") clear
drop in 1
drop v1
save "dW.dta", replace
For some specific reasons, I need to use mata to create my own Matrix. Nonetheless, to be sure that I proceed correctly, I firstly try to replicate the inverse distance I have done previously. I am using an example that I found into the sp manual (p. 128). So i type:
mata:
id = st_data(., "_ID")
location = st_data(., ("_CX", "_CY"))
N = st_nobs()
W = J(N, N, 0)
for (i=1; i<=N; i++) {
for (j=1; j<i; j++) {
delta = location[i,.] - location[j,.]
W[i,j] = W[j,i] = 1/sqrt(delta*delta')
}
}
end
spmatrix spfrommata dW2= W id,
spmatrix export dW2 using dW2.txt, replace
insheet using "dW2.txt", delim(" ") clear
drop in 1
drop v1
save "dW2.dta", replace
Now if everything works, I should observe the same entries in each ij position. I have been struggling all the day, but in fact I obtain different results. Why this is the case?
If you may want to have a look at the files, this is the link to the dataset I am using (already spset): https://dl.dropboxusercontent.com/u/27015126/data.dta
Here, instead is the linked shapefile: https://dl.dropboxusercontent.com/u/...M_2013_shp.dta
Thanks in advance.
Dario
I am using Stata 15 to carry out my spatial analysis. I generate an inverse distance function using spmatrix idistance.
Specifically I type:
spmatrix create idistance dW, replace
spmatrix export dW using dW.txt, replace
Then, I import the file in Stata:
insheet using "dW.txt", delim(" ") clear
drop in 1
drop v1
save "dW.dta", replace
For some specific reasons, I need to use mata to create my own Matrix. Nonetheless, to be sure that I proceed correctly, I firstly try to replicate the inverse distance I have done previously. I am using an example that I found into the sp manual (p. 128). So i type:
mata:
id = st_data(., "_ID")
location = st_data(., ("_CX", "_CY"))
N = st_nobs()
W = J(N, N, 0)
for (i=1; i<=N; i++) {
for (j=1; j<i; j++) {
delta = location[i,.] - location[j,.]
W[i,j] = W[j,i] = 1/sqrt(delta*delta')
}
}
end
spmatrix spfrommata dW2= W id,
spmatrix export dW2 using dW2.txt, replace
insheet using "dW2.txt", delim(" ") clear
drop in 1
drop v1
save "dW2.dta", replace
Now if everything works, I should observe the same entries in each ij position. I have been struggling all the day, but in fact I obtain different results. Why this is the case?
If you may want to have a look at the files, this is the link to the dataset I am using (already spset): https://dl.dropboxusercontent.com/u/27015126/data.dta
Here, instead is the linked shapefile: https://dl.dropboxusercontent.com/u/...M_2013_shp.dta
Thanks in advance.
Dario
Comment