Announcement

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

  • Displaying specific output in a loop

    Dear all,

    I'n using Stata 13.1. I have a balanced panel data with 106 countries.
    For each country I want to test if it demonstrates an inverted U shape over time in some variable. For this end I ran a loop which applies -Utest- command.
    Yet, as an output I just want to see the P>|t| value of the U shape test for each country.
    I tried the following code:
    Code:
    gen long obs = _n
    forval i = 1/106 {      
         su obs if country_code == `i', meanonly      
         di country[r(min)]      
        qui regress ir time time2 if country_code==`i'  
        qui utest time time2        
        local P>|t = e(P>|t)
        di "     P>|t|   = "  
            
    }
    But it returns:
    Albania
    P>|t| =
    Algeria
    P>|t| =
    Argentina
    P>|t| =

    and so on..

    Any ideas how can I can receive the required output?
    Many thanks for your time!
    Anat

  • #2
    Your display command
    Code:
    di " P>|t| = "
    doesn't include the macro into which you stored the result of interest to you, so that's why your output doesn't display the result. But it wouldn't be able to, because in the local command
    Code:
    local P>|t = e(P>|t)
    you chose P>|t as you macro name, and that is not a valid macro name. Perhaps something like
    Code:
    local pvalue = e(P>|t)
    di " P>|t| = `pvalue' "
    will give you the result you are looking for.

    Comment


    • #3
      I would name your local as just p for starters; don't think that's a legal local name, "P>|t". The UTEST command is a third party command? What is it leaving behind if you type 'return list' after running it? Looking at it just now, I don't think it is leaving behind a p-value. Do you really need the UTEST command to test for nonlinearity? Can you pick up the p-value of interest by:

      regress y x xsquared
      test xsquared
      local p = r(p)

      Comment


      • #4
        Dear Dave and William,
        The Utest command is indeed a third party command, and it doesn't leave behind the p value after 'return list' so it was really pointless to do what I did.
        Dave. your command works but I want to test for more than non linearity, I want to test whether an inverse U shape exists, and there are cases were both x and xsquared are significant (with positive and negative coefficients respectively) and the Utest's null (of monotone relationships) is not rejected.
        Ideally, I would like to set a condition in the loop that only the countries for which the null is rejected will be presented, but apparently I can't even limit the display to the P(value) without having to see the entire output of the Utest.
        Thank you very much for your effort and time!
        Anat

        Comment


        • #5
          I wouldn't write off Dave Airey's approach just yet.. Testing for significance of a quadratic term will identify not only U and inverted U relationships, but will also pick up lesser degrees of curvilinearity in relationships that are still monotone within the range of the data. But you can catch that using some algebra.

          If the coefficients for x and x^2 are, respectively, b_lin and b_quad, you can calculate -b_lin/(2*b_quad). That number is the value of x where the fitted quadratic reaches its maximum (or minimum as the case may be). If that value of x is clearly outside the range of values of x that occur in the data, then the significant quadratic term is capturing monotone curvilinearity. But if that value of x is within the range of x's in the data, then you have a U or inverted U-like relationship. This may still not get you exactly what you would get from Utest (a program I have no familiarity with), but it would be a reasonable approach to use in its own right.

          Comment


          • #6
            Thank you very much Clyde! this is a perfect solution!

            Comment


            • #7
              Clyde,
              Following your advice to calculate -b_lin/(2*b_quad), I have tried to write a code that returns for each panel unit: b_lin and b_quad coefficients and the calc. max/min value.

              Despite all my efforts, the code is wrong, it returns the same numbers for all the countries,

              Also, is there a way to ask Stata to display only results for countries for which both b_lin and b_quad are significant in say 5%?
              Code:
              gen long obs = _n
              forval i = 1/106 {      
                  su obs if country_code == `i', meanonly      
                  di country[r(min)]      
                  qui regress interestrateunited time time2  
                  local btime= _b[time]
                  di `btime'
                  local btime_sq=_b[time2]
                  di `btime_sq'
                  qui test time 
                  local p1= r(p)
                  qui test time2  
                  local p2 =r(p)
                     local A=(_b[time]/(-2*_b[time2])) 
                      di `A'    
              }
              Thank you,
              Anat

              Comment


              • #8
                It's because in your loop, the regression statement is carried out on the full data set (minus observations missing one of the variables) each time through the loop, so everything else is the same. Make it -qui regress interestrateunited time time2 if country_code == `i'- and I think you'll have what you want.

                Comment


                • #9
                  In your case I would use statsby to create a dataset of coefficients and p values and even the turning point of the coefficient. statsby will make it with (long) one line.

                  Code:
                  statsby ///
                      b_time=_b[time] ///
                      pvalue_time=(2 * ttail(e(df_r), abs(_b[time]/_se[time]))) ///
                      b_time_sq=_b[time#time] ///
                      pvalue_time_sq=(2 * ttail(e(df_r), abs(_b[time#time]/_se[time#time]))) ///
                      turn_point=(_b[time]/(-2*_b[time#time])) ///
                  , by(country):  reg interestrateunited c.time##c.time
                  Now if you want to test, as Clyde suggest, when the turning point is occurs, you can use this code to add the minimum and maximum values by country (again with statsby).

                  Code:
                  tempfile main
                  save `main', replace
                  sort country
                  statsby min_time=r(min) max_time=r(max), by(country): su time
                  tempfile summary
                  save `summary', replace
                  use `main', clear
                  statsby b_time=_b[time] p_time=(2 * ttail(e(df_r), abs(_b[time]/_se[time]))) ///
                  b_time_sq=_b[time#time] p_time_sq=(2 * ttail(e(df_r), abs( _b[time#time]/_se[time#time]))) ///
                  turn_point=(_b[time]/(-2*_b[time#time])) ///
                  ,  by(country):  reg interestrateunited c.time##c.time
                  merge 1:1 country using `summary', nogen
                  Now you can keep countries only where the pvalue of the linear and the quadratic term is significant in 5%. for example:
                  Code:
                  list if p_time<.05 & p_time_sq<.05
                  * Part of this example succeeds due to Maarten Buis input in a post I found in the Statalist archive

                  Comment


                  • #10
                    Dear Oded Mcdossi, It is working perfectly!!! saved me our of work!
                    I use Stata for 5 years and I always discover new possibilities
                    thank you so very much!

                    Comment

                    Working...
                    X