Announcement

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

  • save regression results for margins ploting

    My question is straightforward. Could Stata save regression results so that I do not have to re-run the model when trying to use margins and marginsplot commands?

  • #2
    Yes. If you want to save them in memory during a Stata working session, read -help estimates store- and -help estimates restore- for details. If you want to save the regression results to a file on disk, read -help estimates save- and -help estimates use-.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Yes. If you want to save them in memory during a Stata working session, read -help estimates store- and -help estimates restore- for details. If you want to save the regression results to a file on disk, read -help estimates save- and -help estimates use-.
      Yes. This is what I am looking for. Thanks. They save a lot of time.

      Comment


      • #4
        Originally posted by Clyde Schechter View Post
        Yes. If you want to save them in memory during a Stata working session, read -help estimates store- and -help estimates restore- for details. If you want to save the regression results to a file on disk, read -help estimates save- and -help estimates use-.
        A follow-up question. I saved the regression results, but when I try -margins- and -marginsplot- after using the saved results, I got an error "e(sample) does not identify the estimation sample". Did I miss some steps? My codes are as follow:

        Code:
        use https://www.stata-press.com/data/r17/nhanes2, clear
        reg bpsystol agegrp##sex
        estimates save "$results/nhanes2_model_results", replace
        
        estimates use "$results/nhanes2_model_results"
        reg
        margins agegrp
        marginsplot

        Comment


        • #5
          Yes, in the help file for -estimates- you will see:
          After estimates use filename, e(sample) is set to contain 0, meaning that none of the observations currently
          in memory was used in obtaining the estimates.
          So, you have to use the -estimates esample:- command to re-define the estimation sample so that -margins- can use it. In -help estimates save- about halfway down you will find the links for setting and resetting e(sample). There you will find the syntactic details.

          Comment


          • #6
            Originally posted by Clyde Schechter View Post
            Yes, in the help file for -estimates- you will see:


            So, you have to use the -estimates esample:- command to re-define the estimation sample so that -margins- can use it. In -help estimates save- about halfway down you will find the links for setting and resetting e(sample). There you will find the syntactic details.
            I checked the help file and other discussions, but still have not figured out how to perform marginsplot from stored regression results. The background is that the regression takes a long time to finish, if I can store the regression results on disk and restore them, then perfrom marginsplot, that will save a lot of time. Could you please make some edits to my previous code to make it work? Thanks.

            Comment


            • #7
              Code:
              use https://www.stata-press.com/data/r17/nhanes2, clear
              reg bpsystol agegrp##sex
              estimates save "nhanes2_model_results", replace
              
              estimates use "nhanes2_model_results"
              estimates esample: bpsystol agegrp sex
              margins agegrp
              marginsplot
              Note: I removed the $results from your pathname because you never defined it in the code you posted. So I just saved the estimates in the current working directory instead. But you can put $results back in--that change had nothing to do with the problem you were having.

              Comment


              • #8
                Originally posted by Clyde Schechter View Post
                Code:
                use https://www.stata-press.com/data/r17/nhanes2, clear
                reg bpsystol agegrp##sex
                estimates save "nhanes2_model_results", replace
                
                estimates use "nhanes2_model_results"
                estimates esample: bpsystol agegrp sex
                margins agegrp
                marginsplot
                Note: I removed the $results from your pathname because you never defined it in the code you posted. So I just saved the estimates in the current working directory instead. But you can put $results back in--that change had nothing to do with the problem you were having.
                Yes, it worked. Thanks.

                Comment

                Working...
                X