Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: RE: exact command for distance ?


From   Austin Nichols <austinnichols@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: RE: exact command for distance ?
Date   Fri, 11 Sep 2009 09:28:45 -0400

Laura--
You don't actually need to download anything to solve this kind of
problem, or much harder similar problems, as illustrated by e.g.
http://www.stata.com/statalist/archive/2009-07/msg00261.html
http://www.stata.com/statalist/archive/2007-01/msg00098.html
and similar posts.

I particularly doubt the final claim in the help file for -distmatch-
in the paragraph "Distance matching is computationally intensive.
Observations of 3,000 may take several minutes to complete. Other
methods typically take days if not weeks and requires extensive
user-involvement."

But are you using the centroids of the bodies of water? That seems
like a strange measure of the distance to water!

You could try:

use farms, clear
local nf=_N
g double mindist=.
merge using waterbodies
local R=6367.44
qui forv i=1/`nf' {
local x1=farm_Y[`i']
local y1=farm_X[`i']
local x2 wat_Y
local y2 wat_X
g double L=(`y2'-`y1')*_pi/180
replace L=(`y2'-`y1'-360)*_pi/180 if L<. & L>_pi
replace L=(`y2'-`y1'+360)*_pi/180 if L<-_pi
local t1 acos(sin(`x2'*_pi/180)*sin(`x1'*_pi/180)
g double d=`t1'+cos(`x2'*_pi/180)*cos(`x1'*_pi/180)*cos(L))*`R'
su d, meanonly
replace mindist=r(min) in `i'
drop L d
}
drop _m waterbody_ID wat_X wat_Y
la var mindist "Distance to center of nearest body of water"

or adapt as appropriate...

On Fri, Sep 11, 2009 at 3:23 AM, Laura Platchkov <LMP881@bham.ac.uk> wrote:
> Dear Roy,
>
> My question was a general advice and also the approriate command. From what I understand from your message, you wrote a command called "dismatch" that I have to add to my STATA that do exactly what I need? It can be applied without any transformation to my case?
>
> After adding the plugin, I need to append the table with the waterbodies to the farms and simply write "distmatch, id(id) near(1) long(long) lat(lat), km"
>
> To adapt to my case, I need to write:
>
> farm_id (waterbod_id) near (1) farm_X (waterb_X) farm_Y (waterbod_Y), km
>
> Is that correct?
>
> Thanks a lot!
>
> Laura
> ________________________________________
> From: Laura Platchkov
> Sent: 10 September 2009 20:27
> To: statalist@hsphsun2.harvard.edu
> Subject: exact command for distance ?
>
> Dear Statalist users,
>
> I was just wondering if someone would have an advice as to how to write a small program on STATA to compute distances between 2 datasets.
>
> I have 2 datasets (.dta files). The first, called farms.dta, contains 800 observations that correspond each of them to a farm,with, among others, the 3 variables: farm_ID, farm_X (longitude) and farm_Y (latitude). The second called waterbodies.dta contains information about the locations of the centroids of 135 waterbodies, more precisely 3 variables: waterbody_ID, wat_X, wat_Y.
>
> I want to calculate the distance of each of the farm to the nearest waterbody, in kilometers.
>
> Now, I need to write a small do file explaining to STATA to calculate the distance using the following Great Circle Formula:
>
> 3963*acos(sin(y/57.2958)*sin(y2/57.2958)+cos(y/57.2958)*cos(y2/57.2958)*cos((x2/57.2958)-(x/57.2958))), where x and y are the coordinates of the farms, and x2 and y2 are the coordinates of the bodies of water....
>
> ...for each observation to each of the waterbodies, but I only need the smallest distance.
>
> I thought a loop would be the best, but I have small doubts about how to write the command on stata. In particular, I don't exactly know how to tell STATA to use 2 different datasets at the keep in 1same time and how to tell STATA to give me only the smallest distance. I  want STATA to simply add the results as an additional variable in the first dataset (or create a 3d dataset with the farm_ID, farm_X, farm_Y and nearest distance if its easier).
>
> My idea is to do a write in the do-file, something like this:
>
> using farms
> forvalues j =1/800
> cross using waterbod
> 3963*acos(sin(y/57.2958)*sin(y2/57.2958)+cos(y/57.2958)*cos(y2/57.2958)*cos((x2/57.2958)-(x/57.2958))), name near_dist
> sort near_dist
> keep in 1
> label variable dist "nearest waterbody"
> list farm_ID farm_X farm_Y near_dist
> save ?????
>
> I guess there are some mistakes... Does anybody has perhaps a suggestion as how to improve the command?
>
> Thanks a lot!
> Laura
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index