Announcement

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

  • Store p-values of looped regression in table/matrix

    Dear Statalist-Users,

    I am running a looped regression:

    Code:
    foreach x of varlist ER_1_Day- Ln_ER_12_Month {
    foreach y of varlist Neg_Words {
        quietly regress `x' `y' LN_Market_Cap LN_Price_to_Book Momentum
        estimates store Coeff_`x'_Neg
    }
    }
    Now I wish to store the p-values of "Neg_Words" with the corresponding x of varlist ER_1_Day- Ln_ER_12_Month in a table/matrix.

    Any advice how to do so? Thanks for your help!

  • #2
    A couple of comments:

    You don't need a double loop as the varlist Neg_Words contains just one variable.

    Conventionally the y is the dependent/left-hand-side/explained variable while the x is used for the independent/right-hand-side/explanatory variable. In your code the meaning has switched.

    You cannot store a mixture of numbers and strings (words) in a matrix, but you can assign names to rows and columns.

    So here is how I would do this:

    Code:
    // open an example dataset
    sysuse auto, clear
    
    // store the unabreviated varlist in the local macro y
    // you can use that to name the rows in your matrix
    unab y : rep78-turn
    
    // count the number of variables in `y'
    local l : word count `y'
    
    // prepare an empty matrix in which to store the
    // p-values
    matrix p = J(`l',1,.)
    matrix rownames p = `y'
    matrix colnames p = "p"
    
    // look at the empty matrix
    matlist p
    
    // loop over the variables and store the p-values
    // see http://www.stata-journal.com/article.html?article=st0137
    // for the computation of the p-values
    local i = 1
    foreach var of local y {
        reg `var' foreign price
        matrix p[`i++',1] = 2*ttail(e(df_r), abs(_b[foreign]/_se[foreign]))
    }
    
    // admire the result
    matlist p
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thank you for comments and advice! This helped a lot

      Is there a possibility to assign stars to the p-values depending on the level of the p-value?
      Last edited by Magdalena Hetterich; 20 Oct 2016, 06:52.

      Comment


      • #4
        I tried to execute the following
        // prepare an empty matrix in which to store the // p-values matrix p = J(`l',1,.) matrix rownames p = `y' matrix colnames p = "p" However, it is not working and showing a syntax error.

        Comment


        • #5
          It runs when I execute the code. A common mistake is to try to execute a do file line by line. Often that is fine, but not always. Especially if you defined local macros, like I did in that example. Local macros disappear after you finished executing. This is a safety feature, but it also means you have to run the definition of the local and the use of that local in one go.
          ---------------------------------
          Maarten L. Buis
          University of Konstanz
          Department of history and sociology
          box 40
          78457 Konstanz
          Germany
          http://www.maartenbuis.nl
          ---------------------------------

          Comment


          • #6
            Thanks. However, it is still not working. I am trying to implement on a bit different question. I want to randomly assign treatment and run the same regression in the loop for 50 times and then I want to see how many times my variable of interest (POST) is significant.


            So this is what i am running

            foreach i of numlist 1/50 {

            generate random=uniform()
            sort random
            generate post=0
            replace post=1 in 1/50

            tsset id year

            set matsize 800
            set more off

            reg agriculture post i.country_id i.year i.country#c.line_time_trend [aw=ypop], cluster( country_id )
            est sto b_1

            esttab b_1,keep( post ) se star(* 0.10 ** 0.05 *** 0.001)

            unab y : post

            local l : word count `post'

            matrix p = J(`l',1,.)
            matrix rownames p = `y'
            matrix colnames p = "p"

            matlist p

            local i = 1
            foreach var of local y {
            reg `var' post
            matrix p[`i++',1] = 2*ttail(e(df_r), abs(_b[post]/_se[post]))
            }

            matlist p
            }

            Comment


            • #7
              I am trying to run loop within loop and get the p values in the matrix. I want to assign random treatment (post) to the non treated countries in the same year and run the regression for 3 different outcome variables for 50 times in a loop. And then get the value of p in the matrix. Can anyone please help me.

              I am doing the following. I am either getting the matrix table with missing p values (.) or getting conformability error. please help !!


              drop random
              drop post
              set more off
              set matsize 800
              set seed 6000
              unab y : wheat rice lentils
              local l : word count `y'
              matrix p = J(50,`l',.)
              matrix colnames p =`y'
              matlist p

              foreach i of numlist 1/50 {

              generate random=uniform()
              sort year random
              generate post=0
              replace post=1 in 1 in 1/29 // for year 2010
              replace post=1 in 296/337 // for year 2011
              replace post=1 in 578/619 // for year 2012

              foreach var of local y {
              quietly reg `var' post i.country_id i.year i.country#c.line_time_trend [aw=ypop], cluster( country_id )
              matrix p[1,`i++'] = 2*ttail(e(df_r), abs(_b[post]/_se[post]))
              }
              drop random
              drop post
              }
              matlist p

              Comment


              • #8
                Triplicate post! Asked and answered at https://www.statalist.org/forums/for...-output-matrix, and also asked on yet another thread. Don't do this! If you don't get an answer to your question, reposting it unchanged is not going to help you. Either your question isn't of any interest to anybody, or, more likely, it is asked in a way that makes it confusing or difficult to answer so people pass it by. In your case, the difficult-to-read layout of your code and the absence of example data to run it with both discourage people from answering your question.

                Comment

                Working...
                X