Announcement

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

  • Using "statsby" to present multiple panel results

    Hi all,

    I used the following commands to produce multiple panel residual unit root test results.

    //predict the residuals for all panels
    su group, meanonly
    forvalues i = 1/`r(max)' {
    regress ln_TX_R ln_REER ln_RGDP_P gfc gfc_ln_RGDP_P t if group == `i'
    predict res`i' if group==`i', res

    }
    end

    //DF unit root test of the residuals [null: unitroot (error term not stationary) = no cointegration]
    su group, meanonly
    forvalues i = 1/`r(max)' {
    dfuller res`i' if group==`i'

    }
    end


    And the results are as below,
    I would like to have test stat, 1%, 5%, 10% CV all in one table, or imported to excel for further analysis.
    I know someone posted before to use the command "statsby"

    i tried the following but to no avail..

    statsby, by(group): dfuller res`i' if group==`i'

    and sometimes i get the following message:
    no; data in memory would be lost

    any help will be greatly appreciate.
    Thanks
    Attached Files

  • #2
    Try this:

    Code:
    statsby, by(group) clear: dfuller res`i' if group==`i'
    
    // OR
    
    statsby, by(group) saving(my_results_file, replace): dfuller res`i' if group==`i'
    -statsby- generates a data set of results. It then has to do something with those results. If you don't use the -saving()- option to tell -statsby- to put them in a file on disk, then it tries to put them into active memory. But if you already have a data set in active memory and it has been modified since you loaded it in, then, as usual, Stata will refuse to clobber your active memory without assurance that you are aware of the potential loss of data. It's just like -use-, in that regard: you can't -use- a new data set when a changed data set is in active memory unless you specify the -clear- option. Same thing for -statsby-.

    Added: I don't really know much about the -dfuller- command as it doesn't come up in my line of work. But according to the -help file-, the scalars it returns in r() do not include everything you are asking for here. What -statsby- will collect is just those scalars from r(). So you may need to do some extra programming to calculate those critical values, as they do not appear to be among them.


    Last edited by Clyde Schechter; 12 Dec 2016, 11:26.

    Comment


    • #3
      Clyde,

      When i include

      statsby, by(group) saving(my_results_file, replace): dfuller res`i' if group==`i' in the loop, it only saves the last results which is group 60. How can i make it so that it saves results of all 60 panel test results?
      Attached Files

      Comment


      • #4
        In addition to Clyde's excellent point, note that there is little value in doing something statsby, by(group) if the rest of your code specifies a single value of group.

        That is like saying "do this for every country: do the calculation when the country is specified to be Ruritania". That boils down to "do this for Ruritania".

        Comment


        • #5
          Oh, sorry, my mistake. Take out the -if group == `i'- part of the command.

          Comment


          • #6
            Clyde,

            If i remove -if group == `i'- , it doesn't even run..
            i get an error message..
            Attached Files

            Comment


            • #7
              And it's telling you why. dfuller wants single panels. What defines a panel in your data? Why do (you think) you want to use statsby here? It looks as if you are just looping over groups by yourself, so statsby is redundant any way.

              Otherwise put, many operations can be done by

              1. arranging yourself to loop over groups

              2. getting statsby to do it for you

              but it's a matter of one or the other, not both.
              Last edited by Nick Cox; 12 Dec 2016, 11:45.

              Comment


              • #8
                OK, because I don't really know -dfuller-, I think my advice to use -statsby- was mistaken. -statsby- begins by running the command on the entire data set, which isn't possible for -dfuller- when you have panel data. So you have to accumulate these results in a -postfile- inside your loop instead.

                Code:
                // SET UP A POSTFILE TO CAPTURE RESULTS
                capture postutil clear
                postfile handle int group float zt pvalue using my_results_file, replace
                //predict the residuals for all panels
                su group, meanonly
                forvalues i = 1/`r(max)' {
                    regress ln_TX_R ln_REER ln_RGDP_P gfc gfc_ln_RGDP_P t if group == `i'
                    predict res`i' if group==`i', res
                    dfuller res`i' if group == `i'
                    post handle (`i') (`r(Zt)') (`r(p)')
                }
                postclose handle
                You will now have your results (Z(t) and p value) in my_results_file.dta.

                Comment


                • #9
                  Clyde,

                  This is perfect, thanks a lot!!!
                  One additional question.
                  I am also running egranger regression and wanting to store the coefficient of ln_REER.


                  capture postutil clear
                  postfile handle int group float _b pvalue using egranger, replace
                  su group, meanonly
                  forvalues i = 1/`r(max)' {
                  egranger ln_TX_R ln_REER ln_RGDP_P gfc gfc_ln_RGDP_P if group == `i', trend regress
                  post handle (`i') (`r(_b[ln_REER])') (`r(p)')
                  }
                  postclose handle
                  end


                  i tried but it isn't working.
                  Any help? Thanks!
                  Attached Files

                  Comment


                  • #10
                    I don't know anything about -egranger-; never heard of it before your post. It doesn't seem to be a part of official Stata. When posting questions about a user-written command, you should indicate where it comes from. But given that you are trying to store a regression coefficient, it's a pretty safe bet that it won't be found in r(_b[ln_REER]). The error message you are getting suggests that, in fact, there is no such thing as r(_b[ln_REER]). If -egranger- works like other commands that include regression analyses, the coefficients will be found in _b[] and in e(b). So probably you should replace that expression by just _b[ln_REER]. While a p-value might be stored in r(p), it might be elsewhere. You're going to have to just run -egranger- by itself once and then examine what is stores in e() and r() to find out where the things you want are (-ereturn list- and -return list-). Then you can revise the -post- command according to what you find there.

                    By the way, it is possible that the p-value you want to retrieve won't be found anywhere. Some commands don't do that, they leave it to you to recalculate it from other things like t or z-statistics that are stored.

                    Comment


                    • #11
                      In addition to Clyde's ever helpful comments, I note (without ever having used it or tried to understand it) that

                      egranger is from SSC.

                      It's e-class in any case. Any r-class results left would be a side-effect of something else.

                      egranger runs regress so as Clyde surmises the last-fitted coefficients should be accessible in _b[]. after it runs.

                      Comment


                      • #12
                        Clyde, and Nick,

                        Thanks again for your prompt response. Since what i'm interested in is long term relationship (first step coefficient) of egranger which is just a regular OLS regression,

                        I modified the code as following (using the codes provided previously by Clyde, but somehow it is not going through. Looks like there's something wrong with the way i specify (`_b[ln_REER]')?

                        One further question would be, do i need to calculate the p-value? looks like they don't provide commands for p values.. in which case i also need to retrieve var e(V) of ln_REER. Should that be _v[ln_REER]?


                        capture postutil clear
                        postfile handle int group float _b using twostep, replace
                        su group, meanonly
                        forvalues i = 1/`r(max)' {
                        regress ln_TX_R ln_REER ln_RGDP_P gfc gfc_ln_RGDP_P t if group == `i'
                        post handle (`i') (`_b[ln_REER]')
                        }
                        postclose handle
                        Attached Files

                        Comment


                        • #13
                          _b[ln_REER] is not a local macro, so it shouldn't have `' around it. So the post command should read:
                          Code:
                          post handle (`i') (_b[ln_REER])

                          Comment

                          Working...
                          X