Hi there,
I am trying to run at least 10,000 replications of my code with a Mata function inside using the postfile command. The code should replicate the results of xtreg y x1 x2, fe r and collect 10,000 values of robust standard errors in a dataset. However, when I attempt to run the code I get the following error message:
unknown function ()
post: above message corresponds to expression 1, variable se1
I might have made some mistake in either recalling the arguments of the function or in defining the function in Mata, or both. Unfortunately, I cannot see/understand where I made such mistake. Can someone help me identifying the problem, please? Thank you.
Just to be clear, when I run a simplified code with Mata code inside Stata code and without the postfile command, everything runs smoothly and I get the desired results.
Here, a simplified version of the code:
_________________________________
clear all
***Mata function
mata:
mata clear
function robest(real matrix X,
real matrix XtX,
real vector res,
real matrix info,
real scalar k,
real scalar N,
real scalar nt)
{
V = J(k,k,.)
for(i=1;i<=N; i++) {
Xi = panelsubmatrix(X,i,info)
res_i = panelsubmatrix(res,i,info)
V = V + Xi'*(res_i*res_i')*Xi
}
Vhat = invsym(XtX)*V*invsym(XtX)*((nt-1)/(nt-k))*(N/(N-1))
se = sqrt(diagonal(Vhat))
results = se'
return(results)
}
end
***Simulation program
program mysim
version 14
tempname sim
postfile `sim' se1 se2 se3 using reg_stimates, replace
quietly {
forvalues i = 1/10000 {
drop _all
set obs 50
gen id = _n
expand 5
bysort id: generate time = _n
gen y = rnormal()
gen x1 = rnormal()
gen x2 = rnormal()
gen cons = 1
foreach j in y x1 x2{
bys id: egen m_`j' = mean(`j')
gen dm_`j' = `j' - m_`j'
sum `j'
replace dm_`j' = dm_`j' + r(mean)
}
putmata y = dm_y X = (dm_x1 dm_x2 cons), replace
m id = st_data(., "id")
m info = panelsetup(id, 1)
m N = rows(info)
m nt = rows(y)
m k = cols(X)
m XtX = cross(X,X)
m Xty = cross(X,y)
m b = cholsolve(XtX,Xty)
m res = y - X*b
m robest(X,XtX,res,info,k,N,nt)
post `sim' (se1)(se2)(se3)
}
}
postclose `sim'
end
***Run simulation
set seed 12345
mysim
0 Response to Help to recall Mata function inside postfile command
Post a Comment