What is the fastest way to calculate the column sums by panels (IDs) in Mata? I use this in a panel maximum likelihood estimation algorithm, and I currently do it as shown below. While it does the job, it is slow especially since the data is huge and the calculation loop runs over and over again for finding the maximum likelihood. Is there a faster way to do this?
This is a simplified single-run example of the algorithm:
This is a simplified single-run example of the algorithm:
Code:
clear
set obs 1000000
gen id=_n
expand 10
sort id
gen var1 = runiform(0,1)
gen var2 = runiform(0,1)
gen var3 = runiform(0,1)
timer clear 1
timer on 1
mata {
id = st_data(.,"id")
big = (st_data(.,"var1"), st_data(.,"var2"), st_data(.,"var3"))
V = panelsetup(id, 1)
bigtot = J(rows(id),3,.)
// this takes too much time -->
for (i=1; i<=rows(V); i++) {
X1 = panelsubmatrix(big, i, V)
bigtot[V[i,1]::V[i,2],.]=J(rows(X1),1,colsum(X1))
} //<--
}
timer off 1
timer list 1

)
Comment