Announcement

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

  • creating a loop including postestimation command and saving results

    Hi, I would like to run the following commands:

    Code:
    regress gypci, robust
    estat sbreak
    over id which is an encoded identifier of countries. Also if it is possible I would like to save all test statistics and if it can be done also all selected years from sbreak into new dataset which would have three columns: c1 id, c2 wald test statistics from sbreak and c3 selected break year from sbreak.

    Below is the example of my dataset:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long id double gypci int year
    1   .09156394379937814 1995
    1   -.1946548261513857 1996
    1   .45043848042028495 1997
    1  -.06380285276712752 1998
    1  -.18338492534911402 1999
    1   .11603080672480595 2000
    1   -.5918871227579264 2001
    1   .21945248334668577 2002
    1   .09863957820774673 2003
    1    .4457930628629913 2004
    1  -.20626793628824608 2005
    1   .24845927625451938 2006
    1 -.022434578997770308 2007
    1   -.6777062107409749 2008
    1  -1.1262354856201757 2009
    1    .9548509824936363 2010
    1  -.12776224134650863 2011
    1    -.430161120751026 2012
    1   .09153558707071675 2013
    1  .025060175267392643 2014
    1  .020079096725331275 2015
    1  .004253382006176759 2016
    1    .0589749711884323 2017
    1  -.08630903937951132 2018
    2   .09156394379937814 1995
    2   -.1946548261513857 1996
    2   .45043848042028495 1997
    2  -.06380285276712752 1998
    2  -.18338492534911402 1999
    2   .11603080672480595 2000
    2   -.5918871227579264 2001
    2   .21945248334668577 2002
    2   .09863957820774673 2003
    2    .4457930628629913 2004
    2  -.20626793628824608 2005
    2   .24845927625451938 2006
    2 -.022434578997770308 2007
    2   -.6777062107409749 2008
    2  -1.1262354856201757 2009
    2    .9548509824936363 2010
    2  -.12776224134650863 2011
    2    -.430161120751026 2012
    2   .09153558707071675 2013
    2  .025060175267392643 2014
    2  .020079096725331275 2015
    2  .004253382006176759 2016
    2    .0589749711884323 2017
    2  -.08630903937951132 2018
    3   .09156394379937814 1995
    3   -.1946548261513857 1996
    3   .45043848042028495 1997
    3  -.06380285276712752 1998
    3  -.18338492534911402 1999
    3   .11603080672480595 2000
    3   -.5918871227579264 2001
    3   .21945248334668577 2002
    3   .09863957820774673 2003
    3    .4457930628629913 2004
    3  -.20626793628824608 2005
    3   .24845927625451938 2006
    3 -.022434578997770308 2007
    3   -.6777062107409749 2008
    3  -1.1262354856201757 2009
    3    .9548509824936363 2010
    3  -.12776224134650863 2011
    3    -.430161120751026 2012
    3   .09153558707071675 2013
    3  .025060175267392643 2014
    3  .020079096725331275 2015
    3  .004253382006176759 2016
    3    .0589749711884323 2017
    3  -.08630903937951132 2018
    4   .09156394379937814 1995
    4   -.1946548261513857 1996
    4   .45043848042028495 1997
    4  -.06380285276712752 1998
    4  -.18338492534911402 1999
    4   .11603080672480595 2000
    4   -.5918871227579264 2001
    4   .21945248334668577 2002
    4   .09863957820774673 2003
    4    .4457930628629913 2004
    4  -.20626793628824608 2005
    4   .24845927625451938 2006
    4 -.022434578997770308 2007
    4   -.6777062107409749 2008
    4  -1.1262354856201757 2009
    4    .9548509824936363 2010
    4  -.12776224134650863 2011
    4    -.430161120751026 2012
    4   .09153558707071675 2013
    4  .025060175267392643 2014
    4  .020079096725331275 2015
    4  .004253382006176759 2016
    4    .0589749711884323 2017
    4  -.08630903937951132 2018
    5   .09156394379937814 1995
    5   -.1946548261513857 1996
    5   .45043848042028495 1997
    5  -.06380285276712752 1998
    end
    label values id id
    label def id 1 "Afghanistan", modify
    label def id 2 "Albania", modify
    label def id 3 "Algeria", modify
    label def id 4 "American Samoa", modify
    label def id 5 "Andorra", modify
    I tried to create the first part of my request by the following loop:

    Code:
    for i in 1/197 {
    regress gypci if id = `i', robust
    estat sbsingle
    }
    But I am getting invalid syntax.

    Thanks for any help in advance.

  • #2
    This code does what I understand you to want, but instead of creating a new dataset, it adds two new variables to the existing dataset. I don't show sample output because your sample data consists of the same values for each id, so it's not particularly illuminating.
    Code:
    xtset id year
    generate wald = 0
    generate int break = 0
    forvalues i = 1/4 {
        regress gypci if id == `i', robust
        estat sbsingle
        replace wald = r(chi2) if id == `i'
        replace break = `r(breakdate)' if id == `i'
    }
    Added in edit:

    You have accidentally posted your topic in Statalist's Mata Forum, which is used for discussions of Stata's Mata language, which is different than Stata's command language, and different than Stata's matrix commands. Your question would have seen a much larger audience if you had posted it in Statalist's General Forum.
    Last edited by William Lisowski; 15 Apr 2020, 07:37.

    Comment


    • #3
      Originally posted by William Lisowski View Post
      This code does what I understand you to want, but instead of creating a new dataset, it adds two new variables to the existing dataset. I don't show sample output because your sample data consists of the same values for each id, so it's not particularly illuminating.
      Code:
      xtset id year
      generate wald = 0
      generate int break = 0
      forvalues i = 1/4 {
      regress gypci if id == `i', robust
      estat sbsingle
      replace wald = r(chi2) if id == `i'
      replace break = `r(breakdate)' if id == `i'
      }
      Added in edit:

      You have accidentally posted your topic in Statalist's Mata Forum, which is used for discussions of Stata's Mata language, which is different than Stata's command language, and different than Stata's matrix commands. Your question would have seen a much larger audience if you had posted it in Statalist's General Forum.
      Thanks for a response, when I run the code above it works but I totally forgot to mention that the id does not always increase in increment of 1 because some countries have no observation, the code you gave works but stops at a first country with missing observations - its possible to adjust the code in a way that it skips those? Also sorry for posting it in mata forum that was by accident, can moderators move it or should I delete this tread and open new in proper forum?

      Comment


      • #4
        This should do.
        Code:
        xtset id year
        generate wald = 0
        generate int break = 0
        drop if id==5 // too few observations
        drop if id==3 // test break in sequence
        levelsof id, local(idlist)
        display "`idlist'"
        foreach i of local idlist {
            quietly regress gypci if id == `i', robust
            estat sbsingle
            replace wald = r(chi2) if id == `i'
            replace break = `r(breakdate)' if id == `i'
        }

        Comment


        • #5
          Please what commands can be used for pedroni and kao cointegration tests in Stata? I'm using Stata 14

          Comment


          • #6
            Benjamin Onah -

            Welcome to Statalist.

            This is a new question that has nothing to do with this topic, which is about saving results from postestimation commands. Furthermore, even that topic was posted in the wrong location, as discussed in post #2.

            Please start a new topic in the General Forum and post your question there.

            On the top of this page, click "Forums for Discussing Stata".

            On the page that opens, click "General".

            On the page that opens, click "+ New Topic".

            Submit your question on the page that opens.

            Comment

            Working...
            X