Announcement

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

  • Graphing a cubic model in stata

    Hi I am trying to graph several cubic functions on one graph in stata (or even one at a time) based on my estimated regression. My regression is in the form y = x + x^2 + x^3 + z + z^2 + z^3 + v + v^2 + v^3 + w + w^2 + w^3 + binary variables.

    I have looked at examples such as: http://www.stata.com/statalist/archi.../msg00766.html, which is a question from the old help form. The example they use is:
    sysuse auto, clear reg mpg c.weight##c.weight##c.weight predict mhat sc mpg weight||line mhat weight, sort This works when estimating one cubic but I want to do 4 at a time and graph them individually or collectively with error bars. I don't need to plot the residuals as I have 20,000 + data points

    my data is in panel form.
    To summarize how do you graph cubics from a regression estimation against the y value.

    Any help is much appreciated. If you need any more specific information let me know as I tend to miss important information occasionally.

    Cheers,
    Daniel

  • #2
    You can use the margins command to plot each curve and its CI's in a separate graph. If all of your x, z, v and w variables are of the same scale you can, I guess, plot them in one graph, though, I think it would be too messy. If your variables are of different scales than you can rescale it to SD units or refer to SD units at the margins command and than plot it in the same graph.

    Comment


    • #3
      Originally posted by Daniel Hamill View Post
      Hi I am trying to graph several cubic functions on one graph in stata (or even one at a time) based on my estimated regression. My regression is in the form y = x + x^2 + x^3 + z + z^2 + z^3 + v + v^2 + v^3 + w + w^2 + w^3 + binary variables.

      I have looked at examples such as: http://www.stata.com/statalist/archi.../msg00766.html, which is a question from the old help form. The example they use is:
      sysuse auto, clear reg mpg c.weight##c.weight##c.weight predict mhat sc mpg weight||line mhat weight, sort This works when estimating one cubic but I want to do 4 at a time and graph them individually or collectively with error bars. I don't need to plot the residuals as I have 20,000 + data points

      my data is in panel form.
      To summarize how do you graph cubics from a regression estimation against the y value.

      Any help is much appreciated. If you need any more specific information let me know as I tend to miss important information occasionally.

      Cheers,
      Daniel
      The command
      Code:
      marginscontplot
      might help. (Please search "marginscontplot".)
      Ho-Chuan (River) Huang
      Stata 19.0, MP(4)

      Comment


      • #4
        before I can use marginsplot command I need to use margins first. Could you please provide an example of code using this function?:

        xtreg y c.x##c.x##c.x c.v##c.v##c.v c.z##c.z##c.z c.w##c.w##c.w

        where they are all continuous variables.

        Cheers,
        Daniel

        Comment


        • #5
          You have to select appropriate values of x, v, z, and w, values which will adequately cover the range of values you are interested in, and enough in between so that the resulting line-graph will look adequately curvilinear. Suppose you are interested in values of x between 0 and 10. Then you can do:

          Code:
          margins, at(x = (0(0.25)10))
          marginsplot
          and similarly for each of the others.

          If you really want to plot them all on one graph, then,:

          Code:
          margins, at(x = (0(0.25)10)) at(v = (v values)) at(z = (z values )) at (w = (w values)) saving(for_graphs, replace)
          use for_graphs, clear
          and then write your own -graph twoway- command using the results in for_graphs.dta

          Comment


          • #6
            Originally posted by Daniel Hamill View Post
            before I can use marginsplot command I need to use margins first. Could you please provide an example of code using this function?:

            xtreg y c.x##c.x##c.x c.v##c.v##c.v c.z##c.z##c.z c.w##c.w##c.w

            where they are all continuous variables.

            Cheers,
            Daniel
            Please try:
            Code:
            use "grunfeld.dta", clear
            
            egen id = group(firm)
            xtset id year
            
            xtreg invest c.value##c.value##c.value c.capital##c.capital##c.capital
            marginscontplot value
            marginscontplot capital
            Ho-Chuan (River) Huang
            Stata 19.0, MP(4)

            Comment


            • #7
              Thanks Clyde Schechter My graphs look fantastic thanks to the commands below.
              margins, at(x = (0(0.25)10)) marginsplot I ran the over code you noted:
              margins, at(x = (0(0.25)10)) at(v = (v values)) at(z = (z values )) at (w = (w values)) saving(for_graphs, replace) use for_graphs, clear But got the error "file for_graphs.dta could not be opened". Any idea what I would be doing wrong? is this creating a temporary file? which is then opened? Again thanks for the awesome help so far. Cheers, Daniel

              Comment


              • #8
                I don't know what's wrong there. I've used this construction myself in the past without difficulty. Which command is throwing the error message, the -margins- command or the -use- command?

                One possibility: the -saving()- option of -margins- is undocumented. If you are not running the latest version of Stata (14) it may not even be implemented, or not correctly implemented in your version of Stata.

                The code as written does not create a tempfile--it should create a permanent file named for_graphs.dta. If you look for it in your working directory, do you find it there? Do you have full access privileges on the directory you're working in? Is the code you showed in #7 exactly what you ran? Any chance that for_graphs is misspelled in either place? (And count change of upper and lower case as misspelling if you are on a Mac.)

                In the future, when showing code you ran, please put it between code delimiters as I have done. Code that is run together on one line is difficult to read. It also forces me to assume that your line breaks are where they should be, and not somewhere else causing errors.

                Comment


                • #9
                  Hi when I use the margins command below the margins are produced but I get the message at the end:

                  "(note: file for_graphs.dta not found), file for_graphs.dta could not be opened,r(603)"
                  "

                  Code:
                  margins, at(x = (5(5)180)) at(y = (5(5)180)) at(z = (5(5)180)) at (w = (5(5)180)) saving(for_graphs, replace)
                  I am using stata 13. I couldn't see it in my working directory.

                  Comment


                  • #10
                    I don't know what to tell you. Perhaps version 13 just doesn't support the -saving()- option with -margins-. Are you sure you have write privileges in your working directory? Do other -save- commands or -saving()- options produce saved files when you run them in this directory?

                    Comment


                    • #11
                      Clyde wrote in #10:
                      I don't know what to tell you. Perhaps version 13 just doesn't support the -saving()- option with -margins-.
                      I have version 13.1, so typed help margins, searched for sav in the Help window, and came up empty.

                      Cheers,
                      Bruce
                      --
                      Bruce Weaver
                      Email: [email protected]
                      Version: Stata/MP 18.5 (Windows)

                      Comment


                      • #12
                        An alternative to saving the predicted margins is through the user written command -marginscontplot- by Patrick Royston. I recently replied to a post here, so see #3 in the following for details

                        http://www.statalist.org/forums/foru...ine-mcp-graphs

                        Of course, upgrading to Stata 14 is always recommended

                        Comment


                        • #13
                          Saving under margins command in Stata 13 is an undocumented option but it is working. For more details type help margins_saving.

                          Comment


                          • #14
                            Hi Clyde, I tried running this same function on another computer with stata 14 and got the same message that "file for_graphs.dta not found". I should have access to my computers directory. Can I choose to save this not in my directory if I don't have access? I would usually save else where and am not familiar with the saving option on commands.

                            Comment


                            • #15
                              You can specify a full pathname in the -saving()- option.

                              But let's back up a second. When are you getting this error message? Are you getting it after -margins-, or when you try to -use for_graphs-? If the former, it's not a problem. Whenever you save a file with the -replace- option, Stata gives you a note indicating that the file was not found if that file did not already exist there. It's basically just checking in with you in case you were thinking the file already existed. But unless the file should have already existed, it's not a problem.

                              If, however, you're getting -file not found- when you try to -use- that data set, then there is a problem in that the file is missing when it should be there.

                              Comment

                              Working...
                              X