Good afternoon,
I found the following code, supposed to do what -egen, mean()- does but in Mata, by William Gould on this thread here: https://www.stata.com/statalist/arch.../msg00582.html
1. Can you make the code work? (It currently does not work.)
2. Can you explain what the lines that I have marked and numbered do in 1-2 sentences?
How the code does not work:
Firstly, around my comment 1) when the declaration is
transmoprhic info
I get the following error:
When I change the declaration to
real matrix info
the code goes through, but
Second, the code does not seem to be doing anything. If I understand correctly the Mata code is supposed to generate a variable y2 which is identical to the variable x1 generated by -egen, mean()-.
I found the following code, supposed to do what -egen, mean()- does but in Mata, by William Gould on this thread here: https://www.stata.com/statalist/arch.../msg00582.html
1. Can you make the code work? (It currently does not work.)
2. Can you explain what the lines that I have marked and numbered do in 1-2 sentences?
Code:
clear mata
mata:
void function mean_by2(string scalar var, string scalar groupid)
{
real scalar i, j, j0, j1, sum, mean
real colvector id, y, p, y2
//transmoprhic info // 1) What is this and why does it generate an error? I changed it to:
real matrix info
st_view(id, ., groupid)
st_view(y, ., var)
p = order(id, 1) // 2) Is this something like "sort id" in Stata?
info=panelsetup(my_y, 1) // 3) Where did this my_y popped up from? It is not defined anywhere before?
st_view(y2, ., st_addvar("double", "_y2"))
for (i=1; i<=rows(info); i++) {
j0 = info[i, 1]
j1 = info[i, 2]
sum = 0
mean = mean(my_y[|j0\j1|]) // 4) What does |j0\j1| do?
for (j=j0; j<=j1; j++) y2[p[j]] = mean // 5) What does this thing do?
}
}
end
clear
set obs 1000000
set more off
egen id=seq(), block(100)
gen y=rnormal()
timer on 1
egen double x1=mean(y), by(id)
timer off 1
timer on 2
mata: mean_by2("y","id")
timer off 2
timer list
Firstly, around my comment 1) when the declaration is
transmoprhic info
I get the following error:
Code:
. mata:
------------------------------------------------- mata (type end to exit) --------------------------------
: void function mean_by2(string scalar var, string scalar groupid)
> {
> real scalar i, j, j0, j1, sum, mean
> real colvector id, y, p, y2
> transmoprhic info // 1) What is this and why does it generate an error? I
> changed it to:
invalid expression
(17 lines skipped)
----------------------------------------------------------------------------------------------------------
r(3000);
end of do-file
r(3000);
real matrix info
the code goes through, but
Second, the code does not seem to be doing anything. If I understand correctly the Mata code is supposed to generate a variable y2 which is identical to the variable x1 generated by -egen, mean()-.

Comment