Announcement

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

  • Problem with a loop

    Hello, when I run the below loop it says:

    invalid dydx() option;
    age_hh_head not found in list of covariates

    I'm not able to figure it out. Please help.


    Code:
    local rhsvars "age_hh_head c.age_hh_head#c.age_hh_head i.headship hhsize dependency_ratio i.caste i.religion i.agri_land_ownership i.house_ownership i.employment i.employment_husband i.dist"
    
    foreach var in decision_making freedom_mobility attitude_violence access_financial_resources {
        eststo raw_`var': quietly fracreg logit c_vector `var' `rhsvars' if area==0 & total_int_women1==1, vce(cl dist)
        eststo margins_`var': margins, dydx(`var' age_hh_head headship hhsize dependency_ratio caste religion agri_land_ownership house_ownership employment employment_husband)
    }

  • #2
    In local rhsvars, c.age_hh_head#c.age_hh_head needs to be c.age_hh_head##c.age_hh_head. In Stata's factor variable notation (see -help fvvarlist- for details), -a##b- expands to -a b a#b-. But a#b just gives you a#b. So the way you had it, age_hh_head never appears as a regressor in the model, so you can't include it in the -dydx()- option.

    Comment


    • #3
      Hello Clyde, I understood what you are saying, but I'm already including age_hh_head as a separate variable in addition to c.age_hh_head#c.age_hh_head. Then, why is the problem?

      Comment


      • #4
        Hmm! I could have sworn that age_hh_head did not appear separately in local macro rhsvars when I read it yesterday--but clearly it is there, and the post has not been edited, so it must have been there then, too.

        This is quite puzzling. I suggest you post the full output from both -fracreg- and -margins-. Include everything, including messages, iterations, etc. Probably it will suffice to do this for just the first dependent variable in your loop, assuming the same problem occurs with all of them. Perhaps there will be some hint of trouble that will shed light on this.

        Added: I notice that you are doing the regression -quietly-. I suspect that the problem here is arising because of some problem in the regression, so before posting back, re-run everything without quietly so that we can see the complete output from the regression.
        Last edited by Clyde Schechter; 25 Nov 2023, 10:24.

        Comment


        • #5
          Hello, Clyde. When I run the regression without quietly, it works fine (I have removed the estimates for districts due to the maximum characters limit).

          Code:
           local rhsvars "age_hh_head c.age_hh_head#c.age_hh_head i.headship hhsize dependency_ratio i.caste i.religion i.agri_la
          > nd_ownership i.house_ownership i.employment i.employment_husband i.dist"
          
          . 
          . foreach var in decision_making freedom_mobility attitude_violence access_financial_resources {
            2.         eststo raw_`var': fracreg logit c_vector `var' `rhsvars' if area==0 & total_int_women1==1, vce(cl dist)
            3.         eststo margins_`var': margins, dydx(`var' age_hh_head headship hhsize dependency_ratio caste religion agri_
          > land_ownership house_ownership employment employment_husband)
            4. }
          
          Iteration 0:   log pseudolikelihood = -36335.448  
          Iteration 1:   log pseudolikelihood = -24392.322  
          Iteration 2:   log pseudolikelihood = -24289.482  
          Iteration 3:   log pseudolikelihood = -24288.108  
          Iteration 4:   log pseudolikelihood =  -24288.06  
          Iteration 5:   log pseudolikelihood = -24288.058  
          
          Fractional logistic regression                  Number of obs     =     48,202
                                                          Wald chi2(49)     =   1.70e+13
                                                          Prob > chi2       =     0.0000
          Log pseudolikelihood = -24288.058               Pseudo R2         =     0.0452
          
                                                        (Std. Err. adjusted for 707 clusters in district)
          -----------------------------------------------------------------------------------------------
                                        |               Robust
                               c_vector |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
          ------------------------------+----------------------------------------------------------------
                        decision_making |  -.0130431   .0038392    -3.40   0.001    -.0205677   -.0055184
                            age_hh_head |  -.0031874   .0021474    -1.48   0.138    -.0073961    .0010214
                                        |
            c.age_hh_head#c.age_hh_head |  -.0000393   .0000211    -1.86   0.063    -.0000808    2.07e-06
                                        |
                               headship |
                         female-headed  |   .0715121   .0141789     5.04   0.000      .043722    .0993022
                                 hhsize |   .0489771   .0029611    16.54   0.000     .0431734    .0547808
                       dependency_ratio |   .1505537   .0068284    22.05   0.000     .1371703    .1639371
                                        |
                               caste_hh |
                       Scheduled Tribe  |   .1892346   .0246487     7.68   0.000     .1409241    .2375451
                  Other Backward Class  |  -.2029608    .014636   -13.87   0.000    -.2316468   -.1742747
                                 Other  |  -.3772465   .0202913   -18.59   0.000    -.4170167   -.3374763
                                        |
                            religion_hh |
                                Muslim  |   .2328735   .0241241     9.65   0.000     .1855911     .280156
                             Christian  |  -.1219737   .0464922    -2.62   0.009    -.2130968   -.0308506
                                 Other  |   -.091021   .0328364    -2.77   0.006    -.1553792   -.0266627
                                        |
                    agri_land_ownership |
                                   yes  |  -.1461235   .0116395   -12.55   0.000    -.1689366   -.1233105
                                        |
                        house_ownership |
                                   yes  |   -.015126   .0116904    -1.29   0.196    -.0380388    .0077867
                                        |
                             employment |
                                   Yes  |   .1294699   .0105494    12.27   0.000     .1087935    .1501463
                                        |
                     employment_husband |
                                   Yes  |   .0139119   .0126824     1.10   0.273    -.0109452    .0387689
                                        |
                                  _cons |  -2.236268   .0606813   -36.85   0.000    -2.355201   -2.117335
          -----------------------------------------------------------------------------------------------
          
          Average marginal effects                        Number of obs     =     48,202
          Model VCE    : Robust
          
          Expression   : Conditional mean of c_vector, predict()
          dy/dx w.r.t. : decision_making age_hh_head 2.headship hhsize dependency_ratio 1.caste_hh 2.caste_hh 3.caste_hh
                         1.religion_hh 2.religion_hh 3.religion_hh 1.agri_land_ownership 1.house_ownership 1.employment
                         1.employment_husband
          
          ---------------------------------------------------------------------------------------
                                |            Delta-method
                                |      dy/dx   Std. Err.      z    P>|z|     [95% Conf. Interval]
          ----------------------+----------------------------------------------------------------
                decision_making |  -.0021364   .0006289    -3.40   0.001     -.003369   -.0009039
                    age_hh_head |  -.0010961   .0000707   -15.50   0.000    -.0012347   -.0009575
                                |
                       headship |
                 female-headed  |   .0118753   .0023862     4.98   0.000     .0071984    .0165523
                         hhsize |   .0080223   .0004841    16.57   0.000     .0070735    .0089711
               dependency_ratio |   .0246604    .001114    22.14   0.000      .022477    .0268437
                                |
                       caste_hh |
               Scheduled Tribe  |   .0342231   .0045276     7.56   0.000     .0253492     .043097
          Other Backward Class  |   -.033394   .0024289   -13.75   0.000    -.0381545   -.0286335
                         Other  |  -.0592709   .0030833   -19.22   0.000    -.0653142   -.0532277
                                |
                    religion_hh |
                        Muslim  |   .0401608   .0043314     9.27   0.000     .0316715    .0486501
                     Christian  |   -.019243   .0071347    -2.70   0.007    -.0332267   -.0052593
                         Other  |   -.014478   .0051156    -2.83   0.005    -.0245044   -.0044515
                                |
            agri_land_ownership |
                           yes  |  -.0240585   .0019249   -12.50   0.000    -.0278312   -.0202858
                                |
                house_ownership |
                           yes  |  -.0024827   .0019226    -1.29   0.197    -.0062508    .0012855
                                |
                     employment |
                           Yes  |   .0213653   .0017514    12.20   0.000     .0179327    .0247979
                                |
             employment_husband |
                           Yes  |   .0022732   .0020673     1.10   0.271    -.0017786    .0063251
          ---------------------------------------------------------------------------------------
          Note: dy/dx for factor levels is the discrete change from the base level.

          Comment


          • #6
            Hello Clyde,
            Now, it's running with quietly as well. I have used the same codes I posted here yesterday, but I don't know what went wrong yesterday. I'm really sorry about this.

            Comment


            • #7
              I have a theory about why it is running now but didn't run yesterday. The code begins with the definition of local macro rhvars. If you ran this command by itself and then tried running the rest of the code, by the definition of local, local macro rhvars would have disappeared before you used it in the regression. So your regression would have included `var' as the dependent variable, but none of the right hand variables. Any code that contains local macros needs to be run without interruption from beginning to end.

              I can't be sure that's what actually happened, but it's consistent with what you observed.

              Comment


              • #8
                Thanks for pointing it out, Clyde.

                Comment

                Working...
                X