Quantcast
Channel: Statalist
Viewing all articles
Browse latest Browse all 73261

Mata function by group

$
0
0
I calculate a real-valued scalar by using the following program:

/* myproc.ado */
capture program drop myproc
program define myproc
mata: myindexfunc()
gen myindex=MYINDEX
end

mata:
void myindexfunc() {
xvar = st_data(.,"xvar")
yvar = st_data(.,"yvar")
lat = st_data(.,"lat")
lon = st_data(.,"lon")
n = length(xvar)
d = J(n, n, .)
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++) {
d[i,j]=exp(-sqrt((lon[i]-lon[j])^2+(lat[i]-lat[j])^2))
}
}
myindex = cross(cross(xvar,d)',yvar)/sum(xvar)/sum(yvar)
st_numscalar("MYINDEX",myindex)
}
end

The program above creates the "myindex" variable that contains a single value of "MYINDEX". The problem here is that my data file has a group variable (say, "group") along with xvar, yvar, lat, lon. My goal is to apply the mata function "myindexfunc()" by group. How do I have to modify the program to do the operation by group?

Viewing all articles
Browse latest Browse all 73261

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>