Announcement

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

  • Plotting polynomial regression

    Hi there,

    I am trying to plot a polynomial regression between the variables hiscam and my third order polynomial time variable below:

    the intention is for the third order polynomial to fit the variation in the variable hiscam. How would I go about plotting this fitted relationship?

    Best wishes,

    James

    42.87512 -1000
    43.74237 -1000
    43.49274 -729
    43.85648 -729
    44.71942 -512
    45.68772 -512
    43.78705 -343
    44.99989 -343
    43.08729 -216
    44.15181 -216
    43.06003 -125
    43.11882 -125
    43.62434 -64
    44.06446 -64
    44.13048 -27
    45.65981 -27
    43.56574 -8
    45.83962 -8
    43.35312 -1
    44.99914 -1
    47.03517 0
    48.04344 0
    47.05454 1
    48.23038 1
    47.78739 8
    48.77758 8
    47.53606 27
    48.73902 27
    47.94943 64
    48.08339 64
    48.14172 125
    49.26078 125
    48.65994 216
    49.85739 216
    48.77392 343
    49.07422 343
    48.99594 512
    49.09679 512
    49.44331 729
    50.71514 729
    48.81572 1000
    49.97142 1000

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float hiscam int time3
    42.87512 -1000
    43.74237 -1000
    43.49274  -729
    43.85648  -729
    44.71942  -512
    45.68772  -512
    43.78705  -343
    44.99989  -343
    43.08729  -216
    44.15181  -216
    43.06003  -125
    43.11882  -125
    43.62434   -64
    44.06446   -64
    44.13048   -27
    45.65981   -27
    43.56574    -8
    45.83962    -8
    43.35312    -1
    44.99914    -1
    47.03517     0
    48.04344     0
    47.05454     1
    48.23038     1
    47.78739     8
    48.77758     8
    47.53606    27
    48.73902    27
    47.94943    64
    48.08339    64
    48.14172   125
    49.26078   125
    48.65994   216
    49.85739   216
    48.77392   343
    49.07422   343
    48.99594   512
    49.09679   512
    49.44331   729
    50.71514   729
    48.81572  1000
    49.97142  1000
    end
    
    
    graph twoway scatter hiscam time3 || lfit hiscam time3, name(cubic, replace)
    I could not help noticing, however, that, at least in your example data, this is a really badly fitting model. A simple linear model against time describes the data much better:

    Code:
    gen time = sign(time3)*abs(time3)^(1/3)
    graph twoway scatter hiscam time || lfit hiscam time, name(linear, replace)
    In the future, when showing data examples, please use the -dataex- command to do so, as I have here. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Hi Clyde,

      Thank you for your help.

      You are right! The linear model is a much better estimator.

      I just have a few further questions.

      I am trying to investigate the treatment effect of an intervention called the "crusade" that begins at the cutoff point. However I am struggling to graph what it is that I my regression results are showing.

      the dataset is below:

      Example generated by -dataex-. To install: ssc install dataex
      clear
      input float(hiscam YEAR crusade) byte summer
      42.87512 -10 0 1
      43.74237 -10 0 0
      43.49274 -9 0 1
      43.85648 -9 0 0
      44.71942 -8 0 1
      45.68772 -8 0 0
      43.78705 -7 0 1
      44.99989 -7 0 0
      43.08729 -6 0 1
      44.15181 -6 0 0
      43.06003 -5 0 1
      43.11882 -5 0 0
      43.62434 -4 0 1
      44.06446 -4 0 0
      44.13048 -3 0 1
      45.65981 -3 0 0
      43.56574 -2 0 1
      45.83962 -2 0 0
      43.35312 -1 0 1
      44.99914 -1 0 0
      47.03517 0 1 1
      48.04344 0 1 0
      47.05454 1 1 1
      48.23038 1 1 0
      47.78739 2 1 1
      48.77758 2 1 0
      47.53606 3 1 1
      48.73902 3 1 0
      47.94943 4 1 1
      48.08339 4 1 0
      48.14172 5 1 1
      49.26078 5 1 0
      48.65994 6 1 1
      49.85739 6 1 0
      48.77392 7 1 1
      49.07422 7 1 0
      48.99594 8 1 1
      49.09679 8 1 0
      49.44331 9 1 1
      50.71514 9 1 0
      48.81572 10 1 1
      49.97142 10 1 0
      end
      [/CODE]

      The years have been normalised so that they are equal to 0 at the cutoff point. The crusade intervention is a binary variable and there is a summer seasonal dummy as there are two observations in each year, a winter and summer one.

      My question is how can I graph this result showing a discontinuity at the cutoff whilst still maintaining the linear model?


      Many thanks

      Comment


      • #4
        Code:
        regress hiscam i.crusade##c.YEAR i.summer
        predict xb, xb
        by YEAR, sort: egen model = mean(xb)
        graph twoway scatter hiscam YEAR || line model YEAR, xline(0)

        Comment

        Working...
        X