Announcement

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

  • Plot regression

    Hi all,

    After performing this regression:

    Code:
    reg loglprice c.yearquarter
    I would like to visualize the results of the regression using "margins" to obtain a graph with the log of Price in the Y-axis, and the yearquarter (i.e.2001q3) in the X-axis:

    Intuitively, it should be something like this:

    Code:
    margins, at(yearquarter==(1999q1(1)2011q4)) atmean
    marginsplot
    I know that the format of yearquarter is not correct, but what do I need to change so that I can get the code to work?
    I know it works with the variable months (1-12) instead of yearquarter.

    Also, is it necessary to generate a dummy variable for yearquarter to perform the regression?

    Thanks in advance!

  • #2
    This seems a complicated way to get something simple. Consider

    Code:
    scatter log1price yearquarter || lfit log1price yearquarter
    or that matter
    Code:
    aaplot
    from SSC.

    If your data run from 1999q1-2011q4 your first time point will be a more convenient origin than 1960q1 = 0.

    Comment


    • #3
      Hi Nick,

      I tried it and it does not give the desired outcome.
      In order to use the results of the regression, the code "margins" and "marginsplot" need to be used.

      For the variable 'months' it works if I enter the following after regression:

      Code:
      margins, at(hhmovm==(1 (1) 12)) atmean
      marginsplot
      I believe that the 'q' in the variable yearquarter is what is causing the issue here.

      Comment


      • #4
        Not so. It is just not true that you need to do it that way. Give example data and I will show you.

        Also, you have to show exactly what you tried and what was wrong from your point of view. The FAQ Advice explains that problem reports with the flavour of “it didn’t work” are usually unanswerable.

        However, you are naming quite different variables now. What do they have to do with your question in #1? If you now have a different question, the answer may be very different.
        Last edited by Nick Cox; 05 May 2019, 11:16.

        Comment


        • #5
          The data of the variable yearquarter looks like:

          yearquarter
          2001q3
          1999q1
          1999q3
          1999q1
          1999q3
          2000q3
          1999q2
          2000q1
          2000q1
          1999q2
          2000q3
          2001q2
          2000q2
          1999q3
          ...

          After performing the following regression:
          Code:
           reg loglprice c.yearquarter
          I entered the code you suggested:
          Code:
           scatter log1price yearquarter || lfit log1price yearquarter
          And it showed the attached graph 'yearquarter'.

          Hhmovm was just to show that the marginsplot code does work for the monthly variable. See attachment 'hhmovm' for my outcome.
          I just want to achieve the same for the variable yearquarter.

          I apologize if it was not clear the first time.
          Attached Files

          Comment


          • #6
            I can’t work without prices too.

            Comment


            • #7
              This only part of the data:

              loglprice
              11.83501
              11.58058
              11.89819
              11.77529
              12.24047
              11.8494
              11.58989
              11.71178
              12.04355
              11.86358
              11.11095
              11.63426
              11.49272
              12.3371
              11.17043
              11.77529
              11.61729
              11.91839
              11.76757
              12.72189
              ...

              Comment


              • #8
                This is moving very slowly. Just please sort on your date variable and show dates and prices together. As I understand it you have just 52 observations, which are not too many to show here. Use dataex as explained in the FAQ Advice.

                I am now away from a computer but others may well be able to respond more quickly.

                Comment


                • #9
                  This is the code, however, I cannot understand why yearquarter suddenly show numbers 156 and so on, while I can see in the data editor view the format '2001q3' etc.

                  Code:
                  * Example generated by -dataex-. To install: ssc install dataex
                  clear
                  input float(loglprice yearquarter)
                  12.577636 156
                  11.277204 156
                   12.20357 156
                  10.757903 156
                  10.668956 156
                  11.184422 156
                  11.634257 156
                  11.407565 156
                  11.225244 156
                  10.491274 156
                  11.860055 156
                  11.433883 156
                  12.733755 156
                  12.404923 156
                   12.69158 156
                  12.001506 156
                   12.07254 156
                   11.15625 156
                   11.25156 156
                  12.429216 156
                   11.97035 156
                    11.8699 156
                  11.000432 156
                  11.711777 156
                  10.491274 156
                  12.043553 156
                  11.141862 156
                   11.91839 156
                   11.98293 156
                  13.214084 156
                   11.91839 156
                  11.127263 156
                  11.066638 156
                  11.429543 156
                   11.95118 156
                    11.8706 156
                  11.461632 156
                   8.517193 156
                  10.545341 156
                  13.214084 156
                   12.07254 156
                  12.043553 156
                  11.813852 156
                   12.05525 156
                  11.407565 156
                  12.310432 156
                   10.85418 156
                   11.05089 156
                  11.695247 156
                  12.429216 156
                  12.094515 156
                   12.76569 156
                   12.76569 156
                  12.043553 156
                  12.254863 156
                   12.06681 156
                  13.214084 156
                  11.767568 156
                  11.277204 156
                   11.67844 156
                   11.73607 156
                  11.289782 156
                  11.695247 156
                  12.170445 156
                  11.400876 156
                  12.542545 156
                  12.706848 156
                  11.635143 156
                  10.668956 156
                   9.952278 156
                  11.289782 156
                  10.518673 156
                  13.214084 156
                  11.805595 156
                   12.12811 156
                  11.532728 156
                  11.561716 156
                  12.452932 156
                   11.77529 156
                  11.396392 156
                  12.206073 156
                  11.695247 156
                  11.418614 156
                    12.0137 156
                  11.599103 156
                   12.29225 156
                  11.350407 156
                  12.301383 156
                  12.388394 156
                  11.198215 156
                   8.294049 156
                  10.645425 156
                  11.133128 156
                   11.63071 156
                  11.362103 156
                  10.621327 156
                   10.12663 156
                  13.214084 156
                   11.68688 156
                   9.472705 156
                  end
                  format %tq yearquarter

                  Comment


                  • #10
                    This is the code, however, I cannot understand why yearquarter suddenly show numbers 156 and so on, while I can see in the data editor view the format '2001q3' etc.
                    I think you need to step back from your project and learn more about Stata. You shouldn't be trying to work with date time variables, one of Stata's most advanced and difficult features, when you don't know how they work. Stata quarterly date variables are numbers. Each quarter is represented by the number of quarters since first quarter of 1960. So, for example, the first quarter of 1960 is 0, and the last quarter of 1960 is 3. The first quarter of 1961 is 4. Now, the folks at StataCorp know that this kind of representation, though optimal for doing computations, is not very helpful to humans trying to look at data. So, notice the command at the end of your -dataex- output: -format %tq yearquarter-. That command tells Stata, when displaying this variable in output, or in the editor/browser window, to show not the raw number 156, but rather the corresponding year and quarter in an easily human-readable form: 1999q1. So you have the best of both worlds: a display format that humans can read, linked to a numerical representation that is suitable for date computations.

                    There are other contexts besides date time variables where Stata enables you to provide a convenient human-readable display for a numerical variable whose underlying meaning is opaque, but where numerical representation is needed for purposes of computation. (I'm referring here to value labeled integer variables.)

                    You have started a number of different threads in the short time you have been a Forum member. I think that you will make much better progress on your project if you interrupt your work on it and take the time to learn the basics. Then you won't have to post a question, wait for a response (which, you may notice, typically requests more or different information) and then reply, and then get another response, and perhaps a long series of posts and responses before you arrive at a solution.

                    I think the best way to learn the Stata basics is to read certain sections of the PDF manuals that are installed with your Stata. The "curriculum" I recommend is the one that William Lisowski has outlined in post #5 at https://www.statalist.org/forums/for...uplicate-dates. Read that post and follow the advice there. The time you invest in learning the fundamentals will be amply repaid, and in pretty short order.

                    In addition to that advice, I urge you to read the Forum FAQ. You will get timely and helpful responses with fewer back-and-forth rounds if you follow the advice there about how to pose clear questions, what information to provide, and how to provide it in the most useful ways.

                    It may be that after you do the reading I have suggested here, you will be able to resolve this particular question on your own. If not, do feel free to post back for help, showing what you've tried and what you got as a result. If you do post back in this way, please post a different example from your data. The one you posted has the same value of yearquarter in every observation. Consequently it is not possible to fit a regression to the data, nor do you get anything useful from any graph commands. Pick a different subset of your data, one that is more representative of the data you are working with, to show.

                    Comment


                    • #11
                      Clyde Schechter gave excellent advice.

                      The request in #1 was (paraphrasing slightly, as i.e. does not mean e.g.) for a graph with the log of Price on the Y-axis and the yearquarter (e.g. 2001q3) on the X-axis

                      The code in #2 does provide that as the attachment to #5 shows when opened with Stata. (Trivial personal, but temporarily crucial, detail in terms of my replies: my phone does not understand .gph file formats! That, extended to what is common with phones and tablets, is one of the reasons we ask for .png. See FAQ Advice #12.)

                      It wasn't clear early on in the thread that you have several measurements for each quarterly date. That doesn't affect the principle. Note that confidence intervals if you want them too don't take account of any serial dependence not captured by a linear trend.

                      The data example in #9 is not useful to show technique as only one quarterly date is sampled. In terms of margins and marginsplot technique it seems simplest to use the underlying numeric dates, which summarize will give you. Here is a silly example.

                      Code:
                      webuse turksales, clear
                      expand 10
                      set seed 2803
                      replace sales = sales + rnormal(0, 1)
                      regress sales c.t
                      su t 
                      margins, at(t=(120(1)159))
                      marginsplot

                      Comment

                      Working...
                      X