Announcement

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

  • Margins command, quadratic variable, and IV: How to calculate marginal effect for quadratic term without factor variable notation

    Dear Stata Forum,

    I would like to estimate the following equation simplified for illustration purposes:
    Code:
    xtivreg2  logrhw   (c.wellbeing c.wellbeing2=c.depress c.depress_2), fe
    Then I would like to determine the marginal effect for wellbeing evaluated at the mean.
    However, IV regression in Stata does not allow for something like the following, which is needed for the margins command:
    Code:
    c.wellbeing##c.wellbeing=c.depress##c.depress
    Question:
    How can I evaluate the marginal effect of wellbeing?
    I would appreciate your help and suggestions.
    Thanks,
    Nico
    Here is a data example:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(logrhw wellbeing wellbeing2 depress depress_2)
            .  .    . .  .
            .  .    . .  .
            .  .    . .  .
            .  .    . .  .
            .  .    . .  .
            . 19  361 2  4
    1.8524344  .    . .  .
       1.6607  .    . .  .
    1.9543204 30  900 5 25
     1.487605 27  729 5 25
     2.181253 30  900 5 25
     1.304421 30  900 5 25
     1.652883 28  784 5 25
    1.2391038 30  900 5 25
            .  2    4 1  1
            . 15  225 1  1
            .  5   25 1  1
            . 12  144 2  4
            .  6   36 2  4
            .  2    4 1  1
            .  .    . .  .
            .  .    . .  .
            .  .    . .  .
            .  .    . .  .
            .  .    . .  .
            .  .    . .  .
            .  .    . .  .
            .  .    . .  .
            .  .    . .  .
            .  .    . .  .
            .  .    . .  .
     2.129325 23  529 3  9
     2.300225 27  729 3  9
    2.1090636 27  729 4 16
     2.749251 30  900 5 25
            . 22  484 2  4
     2.392943 29  841 4 16
            .  .    . .  .
     2.244474 11  121 3  9
    2.0936341 17  289 2  4
     2.760113 30  900 5 25
    2.0467763 25  625 3  9
            . 15  225 3  9
    2.4479344 26  676 3  9
            .  .    . .  .
            .  .    . .  .
            .  .    . .  .
     .9266297 34 1156 4 16
            . 29  841 5 25
    2.0941942 24  576 5 25
    2.0135913 26  676 5 25
    1.9088397 29  841 5 25
    1.9175254 25  625 5 25
            .  .    . .  .
            . 22  484 4 16
     2.423204 31  961 5 25
            .  .    . .  .
            .  .    . .  .
            .  .    . .  .
            .  .    . .  .
            .  .    . .  .
            .  .    . .  .
            .  .    . .  .
            . 26  676 5 25
            . 26  676 4 16
            . 26  676 4 16
            .  .    . .  .
            .  .    . .  .
            .  .    . .  .
            . 17  289 3  9
            . 25  625 2  4
            .  .    . .  .
            .  .    . .  .
            . 29  841 4 16
     2.610395 27  729 5 25
    2.6780224 25  625 4 16
      2.86767 33 1089 5 25
    2.0818703 26  676 5 25
            . 26  676 4 16
            . 26  676 5 25
            .  .    . .  .
            .  .    . .  .
            .  .    . .  .
            .  .    . .  .
            .  .    . .  .
            .  .    . .  .
     2.514734 25  625 5 25
            .  .    . .  .
    2.8272696  9   81 3  9
     2.831384 17  289 3  9
     3.073519  8   64 2  4
    3.0846524 28  784 4 16
     3.109363 15  225 3  9
    3.1362605  2    4 2  4
    3.1790135 16  256 4 16
     3.225305 24  576 4 16
    3.3197315 26  676 4 16
    3.2494724 23  529 3  9
    2.6127515 30  900 5 25
     2.630744 31  961 5 25
    end












  • #2
    Look into f_able on ssc

    Comment


    • #3
      Hi Fernando,
      Thanks for your reply. I coded the following:
      Code:
      xtivreg2  logrhw (wellbeing wellbeing2 =depress depress_2), first fe 
      f_able, nlvar(wellbeing2)
      margins, dydx(wellbeing) nochain
      It works fine with simple IV regression, no problems. However, with individual fixed effects IV I received the following error message:
      [CODEwarning: cannot perform check for estimable functions.
      variable __marg_pvar_1 not found
      ][/CODE]
      If you have a further suggestion, I would appreciate that.
      Thanks,
      Nico

      Comment


      • #4
        Nico: You can center the variables wellbeing and depress by hand before squaring them.

        Code:
        sum wellbeing
        gen wellbeing_dm = wellbeing - r(mean)
        gen wellbeing_dm_sq = wellbeing_dm^2
        sum depress
        gen depress_dm = depress - r(mean)
        gen depress_dm_sq = depress_dm^2
        xtivreg2 logrhw (wellbeing wellbeing_dm_sq = depress depress_dm_sq), first fe cluster(id)

        Comment


        • #5
          You could also use ivreghdfe from https://github.com/sergiocorreia/ivreghdfe which allows interactions in dependent variable and absorb the fixed effects.

          Code:
          webuse nlswork, clear
          ivreghdfe ln_w c.age##c.age not_smsa (c.tenure##c.tenure = union south), absorb(idcode) cluster(idcode)
          Res.:

          Code:
          . ivreghdfe ln_w c.age##c.age not_smsa (c.tenure##c.tenure = union south), absorb(idcode) cluster(idcode)
          (dropped 673 singleton observations)
          (MWFE estimator converged in 1 iterations)
          
          IV (2SLS) estimation
          --------------------
          
          Estimates efficient for homoskedasticity only
          Statistics robust to heteroskedasticity and clustering on idcode
          
          Number of clusters (idcode) =     3461                Number of obs =    18334
                                                                F(  5,  3460) =    33.65
                                                                Prob > F      =   0.0000
          Total (centered) SS     =  1119.473248                Centered R2   =  -2.6811
          Total (uncentered) SS   =  1119.473248                Uncentered R2 =  -2.6811
          Residual SS             =  4120.915828                Root MSE      =    .4742
          
          -----------------------------------------------------------------------------------
                            |               Robust
                    ln_wage | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
          ------------------+----------------------------------------------------------------
                     tenure |   .3261022   .0875849     3.72   0.000     .1543788    .4978256
                            |
          c.tenure#c.tenure |  -.0104835   .0099359    -1.06   0.291    -.0299643    .0089973
                            |
                        age |   -.050879   .0601464    -0.85   0.398     -.168805     .067047
                            |
                c.age#c.age |   .0001792   .0013514     0.13   0.894    -.0024703    .0028288
                            |
                   not_smsa |  -.0427857   .0454809    -0.94   0.347    -.1319579    .0463864
          -----------------------------------------------------------------------------------
          Underidentification test (Kleibergen-Paap rk LM statistic):              3.944
                                                             Chi-sq(1) P-val =    0.0470
          ------------------------------------------------------------------------------
          Weak identification test (Cragg-Donald Wald F statistic):                4.493
                                   (Kleibergen-Paap rk Wald F statistic):          2.006
          Stock-Yogo weak ID test critical values: 10% maximal IV size              7.03
                                                   15% maximal IV size              4.58
                                                   20% maximal IV size              3.95
                                                   25% maximal IV size              3.63
          Source: Stock-Yogo (2005).  Reproduced by permission.
          NB: Critical values are for Cragg-Donald F statistic and i.i.d. errors.
          ------------------------------------------------------------------------------
          Hansen J statistic (overidentification test of all instruments):         0.000
                                                           (equation exactly identified)
          ------------------------------------------------------------------------------
          Instrumented:         tenure c.tenure#c.tenure
          Included instruments: age c.age#c.age not_smsa
          Excluded instruments: union south
          Partialled-out:       _cons
                                nb: total SS, model F and R2s are after partialling-out;
                                    any small-sample adjustments include partialled-out
                                    variables in regressor count K
          ------------------------------------------------------------------------------
          
          Absorbed degrees of freedom:
          -----------------------------------------------------+
           Absorbed FE | Categories  - Redundant  = Num. Coefs |
          -------------+---------------------------------------|
                idcode |      3461        3461           0    *|
          -----------------------------------------------------+
          * = FE nested within cluster; treated as redundant for DoF computation
          Last edited by Andrew Musau; 20 Jan 2025, 15:48.

          Comment


          • #6
            Dear both,
            I really appreciate your help.
            I will give your suggestions a shot.
            Have a great day,
            Best,
            Nico

            Comment

            Working...
            X