Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Saving coefficients in a column

    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

  • #2
    Actually I solved it. I wrote:

    Code:
    gen coefficients = .
    local id "876 921 966 994"
    foreach s of local id{
    estat impact if _ID==`s'
    mat r`s' = r(b_indirect)
    replace coefficients = r`s'[1,1] if _ID==`s'
    }

    Comment


    • #3
      Thank you for closing the thread by sharing your solution. It will be helpful to others with similar problems in the future who search the Forum.

      Comment

      Working...
      X