Announcement

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

  • Marginal Effects

    Hello,

    I am trying to get marginal effects after I conduct probit regression analysis. I used the following code, but I am getting errors. Can someone help me?

    HTML Code:
    margins, dydx (indexfin indexdec i.religion womage husbandage i.womedu_new i.husedu_new i.womjob i.husjob i.wealth residencearea i.states needmoney needpermission distanceissue wifebeating tvmedia i.antenatalvisitsfacilities indexfin##religion indexdec##religion)
    )
    
    I even tried margins, dydx (*)
    Error Messages
    HTML Code:
    Initially I received this message 
    invalid dydx() option;
    variable indexfin may not be present in model as factor and continuous predictor
    
    I removed the few initial variables and then I started getting the following message
    " factor 'womage' not found in list of covariates "
    I appreciate help.

    Aamna

  • #2
    variable indexfin may not be present in model as factor and continuous predictor
    By default, when no i. or c. prefix is specified, variables listed in isolation are assumed to be continuous, and variables that are included in an interaction term (# or ## operator) are assumed to be discrete. You have indexfin listed by itself (hence continuous) at the beginning of the list, and as a term in two interactions (hence discrete) near the end. So your code contradicts itself and Stata cannot discern what to do. You will need to rerun the regression fixing this inconsistency. You will also need to fix the same inconsistency with regard to indexdec. To be safe, don't rely on the defaults; specify i. or c. in every occurrence of every variable in your regression model. And, to also eliminate even the possibility of this kind of contradiction, since you are using the ## operator for your interaction, don't specify the separate variables in the interaction by themselves. The ## operator will automatically do it for you and it will not get the type of variable wrong.

    You will also need to remove the indexfin##religion and indexdec##religion at the end of the dydx() option. There is no such thing as the marginal effect of an interaction term, and Stata does not allow you to specify them in the -dydx()- option.

    factor 'womage' not found in list of covariates
    It seems that this variable does not appear in your regression.

    If these suggestions do not completely solve the problem, when posting back, be sure to show the exact and complete regression command and the output that Stata gives from it, in addition to the exact and complete command and output from -margins-.
    Last edited by Clyde Schechter; 21 Dec 2022, 16:40.

    Comment


    • #3
      Hello,
      Thank you for the feedback. My issue did not resolve as I was getting the same messages even when I made changes. Here are my commands for my work

      HTML Code:
      svy: probit lbw i.religion womage husbandage i.womedu_new i.husedu_new i.womjob i.husjob i.wealth residencearea i.states needmoney needpermission distanceissue wifebeating tvmedia i.antenatalvisitsfacilities i.finindex#i.religion i.decindex#i.religion
      I have no issue with the above code and get the results. Now comes the issue with the margins command.

      HTML Code:
      margins, dydx (i.finindex i.decindex i.religion womage husbandage i.womedu_new i.husedu_new i.womjob i.husjob i.wealth residencearea i.states needmoney needpermission distanceissue wifebeating tvmedia i.antenatalvisitsfacilities decindex##religion finindex##religion)
      
      invalid dydx() option;
      levels of interactions not allowed
      HTML Code:
      margins, dydx (i.religion c.womage c.husbandage i.womedu_new i.husedu_new i.womjob i.husjob i.wealth residencearea i.states needmoney needpermission distanceissue wifebeating tvmedia i.antenatalvisitsfacilities decindex##religion finindex##religion)
      
      invalid dydx() option;
      levels of interactions not allowed
      r(198);
      HTML Code:
      . margins, dydx (c.womage c.husbandage i.womedu_new i.husedu_new i.womjob i.husjob i.wealth residencearea i.states needmoney needpermission distanceissue wifebeating tvmedia i.antenatalvisitsfacilities finindex##religion)
      
      invalid dydx() option;
      levels of interactions not allowed
      r(198);
      margins c.womage c.husbandage i.womedu_new i.husedu_new i.womjob i.husjob i.wealth residencearea i.states needmoney needpermission distanceissue wifebeating tvmedia i.antenatalvisitsfacilities, dydx (finindex) at (religion=(1 2 3 4))

      only factor variables and their interactions are allowed
      r(198);

      HTML Code:
      .margins c.womage c.husbandage i.womedu_new i.husedu_new i.womjob i.husjob i.wealth residencearea i.states needmoney needpermission distanceissue wifebeating tvmedia i.antenatalvisitsfacilities, dydx (decindex finindex) at (religion=(1 2 3 4))
      
      only factor variables and their interactions are allowed
      r(198);

      .
      HTML Code:
       margins c.womage c.husbandage i.womedu_new i.husedu_new i.womjob i.husjob i.wealth residencearea i.states needmoney needpermission distanceissue wifebeating tvmedia i.antenatalvisitsfacilities finindex#religion decindex#religion
      
      only factor variables and their interactions are allowed
      r(198);

      HTML Code:
      margins c.womage c.husbandage i.womedu_new i.husedu_new i.womjob i.husjob i.wealth residencearea i.states needmoney needpermission distanceissue wifebeating tvmedia i.antenatalvisitsfacilities, dydx (finindex#religion decindex#religion)
      invalid dydx() option;
      levels of interactions not allowed
      r(198);

      needmoney needpermission distanceissue wifebeating tvmedia - Binary variables


      I have run different codes, but I am getting error messages. I desperately need help to resolve this issue as my research is on hold due to this issue. I really appreciate your help.

      Aamna






      Comment


      • #4
        Hi Aamna
        Perhaps you can simply try
        margins, dydx(*)
        Also, you cannot directly estimate marginal effects of "interactions" using margins. that requires extra work.
        HTH

        Comment


        • #5
          Remove all the i. and c. prefixes from the -margins- command. And eliminate interaction terms from the dydx() option, as there is no such thing as the marginal effect of an interaction.

          Comment


          • #6
            Hello

            Thank you for your reply. I removed all of the i and c from the code, plus the interaction terms. I am still getting an error.

            HTML Code:
            margins finindex decindex religion womage husbandage womedu_new husedu_new womjob husjob wealth residencearea states needmoney needpermission distanceissue wifebeating tvmedia antenatalvisitsfacilities
            
            factor 'womage' not found in list of covariates
            r(322);
            My PI sent me the following link to understand the calculation of interaction terms. This has confused me more, and I am becoming more frustrated about my ability to get the result. What am I missing or not understanding at all? I am sorry for any inconvenience.

            https://www.statalist.org/forums/for...t-allowed-mean

            Aamna

            Comment


            • #7
              The command you are showing is not for marginal effects, it is for predictive margins. This kind of predictive margins command is only applicable to discrete variables that were entered with the i. prefix in the preceding regression. womage is a continuous variable. So it cannot be used in this way. The same is true, I suspect, of husbandage, and perhaps some other variables in your list. Any variable in your list that was not run with i. in the regression must be removed from this list.

              For these continuous variables, to get predictive margins you must choose particular values of those variables at which to get the predictive margins. For example if you want the predictive margins of womage when womage = 30, 50, and 65, the code would be -margins, at(womage = (30 50 65))-.

              As for the linked material about interaction terms, it confirms what I have already said before: there is no such thing as the marginal effect of an interaction term. It does not exist. Let me explain why. For purposes of explanation, I'll assume that indexfin and religion are both dichotomous variables. This makes the discussion simpler, but the same reasoning applies (even more strongly, in fact) if the variables have more than 2 levels. The marginal effect of a dichotomous (0/1) variable X is defined as E(Y | X = 1) - E(Y | X = 0). If X itself is an interaction term, for example the interaction1.indexfin#1.religion, then there is only one way that this term can be 1: we must have indexfin = 1 and religion = 1. But there are three different ways it can be 0. We can have a) indexfin = 0 with religion = 1, b) indexfin = 1 with religion = 0, or c) indexfin = 0 and religion = 0. Now suppose that the regression coefficients of indexfin, religion, and indexfin#religion are called b1, b2, and b3. Then E(Y | X = 1) - E(Y | X = 0) will be either b1 + b3, b2 + b3, or b1 + b2 + b3 depending on which of the three possibilities for indexfin and religion themselves is in place. So unless b3 = 0 and b1 = b21, there are multiple possible values, hence the expression is undefined because there is no way of deciding which of the three is correct.

              What is proposed in #3 of the linked thread is a mixed second partial derivative of the outcome with respect to the two interacted variables. This is not a marginal effect of an interaction term, but it could be considered as being vaguely analogous to one. If this is what your PI has in mind, you can do the same calculation shown there, just substituting the names of the variables in your interaction terms.

              Comment


              • #8
                Hello,

                Thank you for explaining the marginal effects. I have a better understanding of it since I started, but I am still far from comprehending all the logic behind it. I still have a few more questions based on your reply.

                1) You mentioned that I am not doing marginal effect (my codes). So I wonder what is the correct code to get the marginal effects? I have searched, and I got a similar margins code in every search. Am I not understanding the code, or what am I missing?
                2) Is there a code that I can use to get marginal effects for continuous, binary, and categorical variables together? If not, do I need to do separate commands for continuous variables and a separate one for categorical and binary?
                3) For my research, I am interested in looking at ages 15-49 (reproductive age). Can I use ' at (womage=(15-49))?
                4) Is there something else I could do, similar to marginal effects, to help me get my estimation for all variables present in my research?

                I appreciate your help and time in explaining and teaching me.

                Aamna

                Comment


                • #9
                  1) You mentioned that I am not doing marginal effect (my codes). So I wonder what is the correct code to get the marginal effects? I have searched, and I got a similar margins code in every search. Am I not understanding the code, or what am I missing?
                  I think what you are misunderstanding is the difference between predictive margins and marginal effects. Predictive margins give you the expected values of the outcome variable at the designated values of the specified variable. So, if you have a discrete variable like womedu_new,
                  Code:
                  margins womedu_new
                  will produce a table showing, for each value of womedu_new that exists in the estimation sample, the corresponding mean value of the probability of lbw, adjusted for all other model variables, corresponding to that value of womedu_new. This syntax is applicable only to variables that were entered into the regression with an i. prefix.

                  If you have a continuous variable, like womage, then you must yourself specify the values of womage for which you want to see the corresponding mean values of the probability of lbw. The syntax would be something like
                  Code:
                  margins, at(womage = (20 30 40 50))
                  The marginal effect of womedu_new means the expected change in the probability of lbw when womedu_new changes by one unit. The code for that is:
                  Code:
                  margins, dydx(womedu_new)
                  This syntax applies to both discrete (i.) and continuous (c.) variables. The actual calculation is done differently, however. For discrete variables it is calculated as the difference between the mean values of probability of lbw when the variable = 1 and when it = 0. For continuous variables a numerical first derivative d prob_lbw/d variable is calculated.

                  It gets a bit more complicated when the model contains interactions. If you have an interaction between i.decindex and i.religion, then your model presumes that the marginal effect of religion depends on the value of decindex and vice versa. Consequently if you specify -margins, dydx(decindex)- what you get will be a sort of average of the different marginal effects of decindex in each religion, the weighting of the average depending on the distribution of religion in your data. More precise is to calculate the decindex marginal effects separately for each religion with
                  Code:
                  margins religion, dydx(decindex)
                  You can also do it the other way around if you are interested in the marginal effects of religion depending on the value of decindex. -margins decindex, dydx(religion)-.

                  If you had an interaction between a discrete variable D and a continuous variable C, you would look at:
                  Code:
                  margins D, dydx(C)
                  margins, dydx(D) at (C = (list_of_values_of_C))
                  If you had an interaction between two continuous variables, C1, and C2, you would look at:
                  Code:
                  margins, dydx(C1) at(C2 = (list_of_values_of_C))
                  // AND VICE VERSA
                  2) Is there a code that I can use to get marginal effects for continuous, binary, and categorical variables together? If not, do I need to do separate commands for continuous variables and a separate one for categorical and binary?
                  As noted in the answer to your first question, for most things you need to use different code for discrete and categorical variables. And interactions require yet a different approach. I have illustrated the commonest situations in response to 1).

                  3) For my research, I am interested in looking at ages 15-49 (reproductive age). Can I use ' at (womage=(15-49))?
                  Almost. It would be -at(womanage = (15/49))-

                  [quote]4) Is there something else I could do, similar to marginal effects, to help me get my estimation for all variables present in my research?
                  [quote]
                  The examples shown in response to 1) show how you can get the marginal effects of each variable in your model, including dealing with the interactions. For the interactions you would be calculating the marginal effect of each of the constituent variables at values of the other constituent variable. The interaction term itself has no marginal effect.




                  Comment


                  • #10
                    Hello

                    Thank you for explaining this further. It has helped me understand interaction terms in more detail. I have just a minor question, which might be a little silly to ask.

                    I have seen people doing something like this (an example) finindex*religion (both are categorical variables) to create interaction terms. After reading your posts, I feel this could not produce interaction terms. I want to know what does this create or show?

                    I appreciate your time and patience in explaining and teaching me in depth.

                    Aamna

                    Comment


                    • #11
                      You can create your own interaction terms in that way if you want to. But if you do that, you cannot use margins for the finindex or religion variables. That's because -margins- has no way to know that you have an interaction term involving those variables and it will give incorrect results for finindex and religion (and any other variables that might be interacted with them.)

                      Comment


                      • #12
                        Hello,

                        If I use this interaction term, does margin recognize religion and finindex when present as independent variables (at the beginning of command) or does it not recognize them at all?

                        HTML Code:
                        svy: probit lbw i.finindex i.decindex i.religion womage husbandage i.womedu_new i.husedu_new i.womjob i.husjob i.wealth int1
                        margins, dydx (*)
                        
                        int1 is interactive term
                        Aamna

                        Comment


                        • #13
                          In this situation -margins- gives incorrect results for finindex and decindex. It recognizes they are present in the model, but it does not know that int1 is their interaction, consequently it misinterprets finindex and decindex and produces wrong answers.

                          Added: Look, this is a pretty transparent, but unsuccessful, attempt to "trick" Stata into calculating a marginal effect for an interaction term. There is no such thing as the marginal effect of an interaction term, and I have explained in #7 why no such thing can exist. StataCorp has designed its -margins- command to respect that fact and to avoid putting out any answer when asked to provide a marginal effect for an interaction term. You are not going to succeed in this endeavor, no matter how hard you try.

                          I'm inferring that you are under pressure from a supervisor to produce this non-existent marginal effect of the interaction term, apparently being unaware of its non-existence. While I don't know who your supervisor is and can't advise you how to manage your relationship with him/her, I can at least ask you (rhetorically) whether you have tried to explain that you are being asked to do the impossible. Have you shown this thread to your supervisor? If, after reading this thread, your supervisor insists that there is such a thing, have you asked him/her to explain what it is and how it is to be calculated?

                          Perhaps you can ask your supervisor to refer you to a paper or textbook in which such a thing is actually calculated. Then you can review that reference, dissect the methods that were used, and apply them to your data. Whatever those methods are, I guarantee you that what they produce is not actually a marginal effect of an interaction term (since no such thing exists), but it is something else that has been mislabeled. Nevertheless, it may well be a useful statistic, and producing it will probably satisfy your supervisor. Ideally you will also be able to call it by its proper name (whatever that turns out to be) and your supervisor will go along with that. If your supervisor is stubborn and insists on continuing to call it the marginal effect of the interaction term, I suppose, in the short term, you have no choice but to go along with that. But, if that happens, at least know inside your own brain that it is wrong and that your supervisor is now being obstinate and ignorant. And when the opportunity presents itself to move on to a better supervisor, let that experience be part of your decision making process.
                          Last edited by Clyde Schechter; 30 Dec 2022, 14:14.

                          Comment


                          • #14
                            Hello,

                            I study the association between air pollution levels and the weekly number of cases of a respiratory disease controlling for various socioeconomic characteristics at the census block level.

                            I want to explore the heterogeneity of the effect of pollution on the respiratory disease incidence across census blocks. One way of doing this is by implementing the partial (sorted) effects method and classification analysis using the SortedEffects R package, I guess.

                            I was wondering if there was a STATA analogue of the SortedEffects R package.

                            My dependent variable and main variable of interest are continuous. I do not attempt to study the causal effect of pollution - just the association between air pollution and the respiratory disease.

                            Thank you.

                            Comment

                            Working...
                            X