Announcement

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

  • Graph the X–Y relationship with holding other variables

    Dear honorable predecessors,

    I meet with an issue about graphing by STATA, that is how to graph the X-Y relationship, while holding other variables at the sample means, medians, or other meaningful values ?

    All I know to graph is using the code "graph twoway (qfit Y X)", I don't know how to fit curves during controling other variables.

    Please give me some suggestions. Thank you for your time and help.

    Wei

  • #2
    You could use margins and marginsplot. Try something like the following as an example of the approach. Both commands have options that allow the ultimate graph to be be tweaked to your liking.
    Code:
    sysuse auto
    
    regress price c.weight c.displacement##c.foreign
    
    summarize weight, meanonly
    local numlist `=r(min)'(`=(r(max)-r(min))/50')`=r(max)'
    quietly margins, at(weight=(`numlist'))
    
    marginsplot , plotopts(msymbol(none))

    Comment


    • #3
      Originally posted by Joseph Coveney View Post
      You could use margins and marginsplot. Try something like the following as an example of the approach. Both commands have options that allow the ultimate graph to be be tweaked to your liking.
      Code:
      sysuse auto
      
      regress price c.weight c.displacement##c.foreign
      
      summarize weight, meanonly
      local numlist `=r(min)'(`=(r(max)-r(min))/50')`=r(max)'
      quietly margins, at(weight=(`numlist'))
      
      marginsplot , plotopts(msymbol(none))
      Thank you for your warm help and timely reply. I will learn the usage of margins and marginsplot by your example.

      However, my regression model is a nonlinear one, like Y=X1+X1^2+X2+X3.....

      X1 is the independent variable, X2, X3 et al. are control variables. And I want to grap the X1-Y relationship with holding X2, X3, et al. at the sample means.

      At this case, what should I do? Please give me some codes, sir.

      Comment


      • #4
        It's exactly the same. That's the beauty of these two commands.

        The only difference from what I show above is in the regression model, itself, where you would fit to the polynomial in the predictor-of-interest. There, do use factor variable syntax, because margins relies upon the use of factor variables in the regression command:
        Code:
        sysuse auto
        
        regress price c.weight##c.weight c.displacement##c.foreign
        
        summarize weight, meanonly
        local numlist `=r(min)'(`=(r(max)-r(min))/50')`=r(max)'
        quietly margins, at(weight=(`numlist'))
        
        marginsplot , plotopts(msymbol(none))

        Comment


        • #5
          Originally posted by Joseph Coveney View Post
          It's exactly the same. That's the beauty of these two commands.

          The only difference from what I show above is in the regression model, itself, where you would fit to the polynomial in the predictor-of-interest. There, do use factor variable syntax, because margins relies upon the use of factor variables in the regression command:
          Code:
          sysuse auto
          
          regress price c.weight##c.weight c.displacement##c.foreign
          
          summarize weight, meanonly
          local numlist `=r(min)'(`=(r(max)-r(min))/50')`=r(max)'
          quietly margins, at(weight=(`numlist'))
          
          marginsplot , plotopts(msymbol(none))
          Dear sir,

          I obtain the required curve by your codes, thank you very much! I really appreciate it.

          Now, I encouter another problem, that is how to fit two curves in one graph by marginsplot or marginscontplot. For example, the regression model above,
          Y=X1+X1^2+X2+X3.....

          If X2 is a dummy variable, which has two values, 0 and 1. How to fit two curves for X2=1 and X2=0 in one gaph? By the command of graph twoway, I know the code is

          graph twoway (qfit Y X1 if X2 == 1)
          (qfit Y X1 if X2 == 0)

          While, what should I do to achieve the goals by the command of marginsplot or marginscontplot? After reading help, I haven't found solutions.

          Please give me some suggestions, thank you again for your warm help and time reply.

          Wei

          Comment

          Working...
          X