Announcement

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

  • Running a loop n times with different outcome variable and storing the p values in an output matrix

    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

  • #2
    In order to properly debug this code, example data that is compatible with it is required. Use the -dataex- command to show that. If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Moreover, the code you show is not the code producing the error you describe. The code you show contains numerous syntax errors that cause it to break before reaching the calculations you describe. So please show the actual code that you need help with in the future. Also to make it more readable, enclose it inside code delimiters. (Read Forum FAQ #12 if you are not familiar with code delimiters.)

    That said, your reference to p[1, `i++'] is clearly wrong. Matrix p is an n x 3 matrix. But i ranges from 1/50 under the control of your -foreach i of numlist 1/50 {- loop. (Also it is dangerous to explicitly increment the loop variable inside the loop when it is being controlled by the foreach. ) I think you don't want i to have anything to do with the second subscript in p. I think you need a different index, let's call it j, which needs to be initialized to 1 before the -foreach var of local y {- loop and then gets explicitly incremented inside the loop. Also, I think you do want `i' to be the first subscript in your command that sets the values in p. So something like this:
    Code:
        local j = 1
        foreach var of local y {
            quietly reg `var' post i.country_id i.year i.country#c.line_time_trend , cluster( country_id )
            matrix p[`i',`j++'] = 2*ttail(e(df_r), abs(_b[post]/_se[post]))
        }

    Comment


    • #3
      Now, I understand where I was going wrong. Thanks, Clyde

      Comment

      Working...
      X