Dear All,

I estimate a spatial regression model and then I calculate the marginal impact of the regressor on the dependent variable. Since I use a spatial lag model, I have a direct, an indirect and a total impact. Now, for each county in my dataset, I would like to estimate the indirect impact and to save it in a separate column.

Specifically, I use the datasets homicide1990 and homicide1990_shp available through the example datasets in Stata. Then, I use:

Code:
spmatrix create idistance " /* I create an inverse distance matrix */
spregress hrate unemployment, dvarlag(W) het gs2sls /* I estimate a spatial lag model */
Using

Code:
estat impact
I can estimate the (average) direct, indirect and total effect. Now, I would like to calculate them for
each county
in the data and save them in a new column. So I type:

Code:
gen coefficients = .
local i = 1

local id "876 921 966 994" /* This numbers identify the first four counties in the dataset */
foreach s of local id{
estat impact if _ID==`s'
mat r`s' = r(b_indirect)
replace coefficients = r`s'[1,1] in `i'
}
The example above is for only four country. The direct, indirect and total impacts are estimated for each country, but every loop replaces the indirect impact in the new column coefficient. So only the last one is reported. How can I get all the four coefficients in the new column?

Thanks in advance