Announcement

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

  • random treatment,50 times loop and store the estimate with p value for the variable of interest (POST) and display.

    Hi,

    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. It's better if I can store the estimate with the p-value (or in a star representation). I tried the following. Where am I going wrong? Any help is appreciated.


    drop post
    drop random
    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)
    est clear //estimates clear


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

    matlist p

    // for the computation of the p-values
    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
    }




  • Aditi Roy
    replied
    Thanks, Phil. However, it is still not working. I am getting the p-value of the last regression only for Post. I want all the "p values" for the variable post after the loop runs 50 times and then I want to count the of times p-values of the variable post was under 0.10.

    [code delimiters][set matsize 5000
    set more off
    unab y : agriculture
    local l: word count `post'
    matrix p = J(100,5,.) //defined matrix
    matrix rownames p = `y'
    matrix colnames p = "p"
    foreach i of numlist 1/50 {
    set seed 2846895
    generate random=uniform()
    sort random
    generate post=0
    replace post=1 in 1/1600
    reg agriculture post i.country_id i.year i.country_id#c.line_time_trend [aw=ypop], cluster( country_id )
    matrix p = 2*ttail(e(df_r), abs(_b[post]/_se[post]))
    drop random
    drop post
    }
    //to display the value of p
    matlist p
    count if p<=0.10]

    Can anyone please help!

    Leave a comment:


  • Mike Lacy
    replied
    As an addition to Phil's comments, I'd suggest you take a look at the built-in -permute- command, which sounds close to what I think you want.

    Leave a comment:


  • Phil Bromiley
    replied
    You didn't get a quick answer. You'll increase your chances of a useful answer by following the FAQ on asking questions - provide Stata code in code delimiters, readable Stata output, and sample data using dataex. You can also use a simpler model - the extra variables just add complexity. Without data, we can't replicate your problem to see what is going on.

    Your regression saves a matrix called r(table). If you don't mess with the order of variables, you can refer to the probability of a given variable based on its location in r(table). I don't know if you need to copy r(table) into a different matrix name first.
    Instead of the second loop, I'd just set up a larger matrix before the loops with all missing, and a counter for the row of the matrix, then I'd write the results into the right places in the matrix. Sometimes, instead of a matrix, I write the values into a variable so I can easily do statistics.

    Leave a comment:

Working...
X