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?
/* 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?
Comment