Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: need mata or perhaps egen help


From   "Scott Merryman" <scott.merryman@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: need mata or perhaps egen help
Date   Thu, 22 May 2008 16:02:26 -0500

You can use -st_data()- and -st_store()- to move data between Stata
and mata (see -help m4_stata- )

Below is one way:

clear
//nearest neighbor matrix
set obs 20
set seed 12345
forv i = 1/20 {
	qui gen v`i' = uniform()<.2 in  `i'/l
	qui replace v`i' = 0 in `i'
}
mata: dist = makesymmetric(st_data(.,.))
mata: dist
drop v*

//spending
set obs 20
gen spending = uniform()
gen total =.

//total spending of neighbors
mata
spend = st_data(.,"spending")
spend
X = dist*spend
st_store(.,"total", X)
end
l


Scott

On Thu, May 22, 2008 at 3:06 PM, David Merriman <dmerrim@gmail.com> wrote:
> I have a set of 584 communities and a square (584x584) matrix D that
> measures the
> distance from each community to each community.
>
> For each community I also have (584x1) vector (EDI) containing a
> variable that measures (essentially) the total amount spent on
> economic development incentives.
>
> I have this data for a number of years but I do not think that is
> important to my programming issues.
>
> For each of my communities I want a single variable that measures the
> total amount spent within a given distance (say 15 miles) on economic
> development incentives.
>
> I can convert my D matrix into a zero/1 matrix
> (assign zero to home community and and any community more than 15
> miles distant, 1 otherwise) and Call  this DT
>
> Seems I should be then be able to use mata to do the matrix multiplication
>
> R=DT*EDI
>
> R should be a 584x1 vector with the variable I need.
>
>
> The problem (I think) is that I have never used mata and am having a
> hard time figuring out how to feed my stata data into mata, do the
> multiplication and get the result back  to stata.
>
> Can anyone tell me how to do this?
> Maybe there is an easier way to do it with egen or some other stata commands.
>
> Thanks
> Dave Merriman
>
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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