Announcement

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

  • Plot graph from linear regression in logs

    Hello everybody,

    I think I have a simple question but haven't found a solution yet.

    I estimate

    xtreg log_y=log_x +log_z, fe

    In this case I can interpret the coefficients as percent change, i.e. if the coefficient of log_x is 0.5, then a 1% increase in x will increase y by 1%.

    Now I estimate a model including a squared term of log_x then I don't know.

    xtreg log_y=log_x + log_x_sq+log_z, fe

    I intend to plot both figures on the relationship between x and y but: what is the range of log_y and log_x which I should show in the figure? I would think that the range should be between 0 and 1 but the true range of the values of log_x and log_y is of course different.

    Does somebody has an idea how to plot the estimated relations from a log-log regression?
    Last edited by Bobby Wood; 02 Aug 2017, 07:14.

  • #2
    When doing quadratic models in Stata, it is best to use factor variable notation. Read -help fvvarlist- to understand how it works.

    Code:
    summ log_x, meanonly
    local interesting_values `r(min)'
    local delta = (`r(max)'-`r(min)')/10
    forvalues i = 1/9 {
        local interesting_values `interesting_values' `=`r(min)' + `i'*`delta''
    }
    local interesting_values `interesting_values' `r(max)'
    
    
    xtreg log_y c.log_x##c.log_x log_z, fe
    margins, at(log_x = (`interesting_values'))
    marginsplot
    The first part of this code generates a list of 11 equally spaced values spanning the range of observed values of log_x. That's just a suggestion, of course, and you might prefer some other set of interesting values. Really nobody can answer that question for you: you alone know what the purpose and meaning of your research is, so only you can decide which values of log_x are important to look at and which are not.

    The second part of this code does the regression, using factor variable notation. That sets you up for the -margins- command. -marginsplot- makes the actual graph. Note that -marginsplot- accepts most options available for -graph twoway-, so you can customize the appearance of the graph however you like.

    Note: Code not tested. Beware of typos.

    Comment


    • #3
      Thanks very much. But the problem I have is not that I can't plot it, sorry for being not sufficiently precise on this. The problem I have is that I don't know the range of values I should take.
      As one can interpret the coefficients of a log-log regression as percent changes, I thought the plot should have the range 0 to 100 and not the values of the log variable.

      In the plot you suggested, I'm not sure about the interpretation as the range is not from 0 to 100 but (in my case) just from 0.5 to 3.3. Does, this percent change interpretation doesn't work for the plot or am I missing something?
      Last edited by Bobby Wood; 02 Aug 2017, 10:11.

      Comment


      • #4
        I think we're not talking about the same thing. What kind of plot do you want to make? What would be on the horizontal axis, and what on the vertical? Whatever those are, you should be able to get it out of -marginsplot-; it's a question of getting the -margins- command right.

        I will say, however, that your question is perplexing from another perspective. All of Stata's graph commands will automatically select an appropriate range of values based on the data. You do not need to specify a range yourself--though you can do so if you want to override Stata's choice. But advance knowledge of the data is not required to get an appropriate range--Stata will handle that automatically if you just specify nothing about it.

        Comment


        • #5
          Unfortunately I use xtivreg2 and margins doesn't work and I have to do it by hand and also decide about the range.

          Maybe I'm totally confused, but if I estimate a simple linear regression model like:

          reg log_y log_x

          and get 0.5 as the coefficient for log_x. Then the interpretation is that a 1% increase in x will cause a 0.5% increase in y.

          Plotting this is easy (and probably unnecessary). The x axis will have the range 0 to 100 and the y axis will have the range 0 to 100*0.5=50.
          This enables me to visualize the effect from a 1% increase of x on y to a 100% increase from x on y, right?

          Now I want to do the same, but for a model which also includes a square term:

          reg log_y log_x log_x_sq

          How should I proceed regarding the plot in this case? Or am totally nonsense what I want to do and(or how I want to do it?


          Comment


          • #6
            I'm sorry, but I still don't understand. What variable is plotted on the x-axis and what variable is on the y-axis? When you describe, a 1% increase in x will be associated with a 0.5% increase in y, you are describing a single point--not a graph. So I still do not get what you are trying to plot.

            If you are calculating, without benefit of -margins-, marginal effects in a quadratic model, be sure you do it correctly:

            Code:
            marginal effect of log_x at log_x = L  is  _b[log_x] + 2*_b[log_x_sq]*L
            Note: Above formula assumes that the full regression equation does not include any other terms involving log_x.

            Comment


            • #7
              x is plotted on the x-axis, y is plotted on the y-axis. What I want to do is to plot the relation between x and y and - as the relation is potentially non-linear - I also want to plot the relation between y and a function of x estimated by

              reg log_y log_x lox_x_sq

              Comment


              • #8
                So after -reg log_y log_x log_x_sq- just do

                Code:
                predict y_hat, xb
                replace y_hat = exp(y_hat)
                graph twoway line y_hat x || scatter y x
                You don't need to know the range of the data in advance: Stata will figure it out and choose appropriate ends for the axes. If you don't like Stata's choices, you can always override them using -xlabel()- or -ylabel()- options.

                Comment

                Working...
                X