Announcement

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

  • Postfile and foreach loop in time-series coef reporting

    I know it may be a really elementary problem, but... I have a dataset with 1464 variables and I am trying to do time-series regression in all of them and exporting the results with the -postfile- command. However, no results are being posted in the new dataset. I am new to the use of the -postfile- command along with -foreach-. I have not been able to find an answer in the other posts of Statalist. Can someone provide me with any advice?

    *================================
    tempname memhold
    postfile `memhold' `var'_beta `var'_se `var'_p using "testabcdef.dta" , replace
    foreach var of local varlist {
    prais `var' year, rhotype(regress)
    local b = _b[`var']
    local se = _se[`var']
    test `var'
    local p=r(p)
    post `memhold' (`b') (`se') (`p')
    }
    postclose `memhold'

    *===============================

    Thank you all,
    Last edited by Alessandro Bigoni; 28 Aug 2018, 05:35.

  • #2
    You can't properly write -postfile `memhold' `var'_beta `var'_se `var'_p using "testabcdef.dta" , replace-, at a point in the code where `var' has not yet been defined. (However, you should still get a postfile, with variables named _beta, _se, and _p, and it should have numbers in it.)

    Also, while I am unfamiliar with -prais-, assuming it works like most Stata estimation commands, the first variable mentioned is the outcome variable and subsequent variables are the predictors. If that is correct for -prais-, then references to _b[`var'] and the like make no sense: there is no coefficient for the outcome variable. There will be a coefficient and standard error for year, which I assume is what you meant to collect in your postfile. Similarly, it makes no sense to -test- the outcome variable: you can only -test- predictors.

    I assume what you really want to do is more like this (changes from your code italicized:
    Code:
    tempname memhold
    postfile `memhold' str32 varname beta se p using "testabcdef.dta" , replace
    foreach var of local varlist {
        prais `var' year, rhotype(regress)
        local b = _b[year]
        local se = _se[year]
        test year
        local p=r(p)
        post `memhold' (`"`var'"') (`b') (`se') (`p')
    }
    postclose `memhold'
    use testabcdef, clear
    Not tested because you provided no example data. Beware of typos. And perhaps there are other problems I have not noticed.

    I am surprised that your code ran and produced empty output. since I would expect that the invalid references to _b[`var'], etc. would yield error messages and cause the code to halt.

    Comment


    • #3
      Thank you, Clyde. I really did make a little bit of a mess. A friend helped me out with the issue but using some mata coding. The solution ended up being:

      Code:
      local estimates1
      mata : res = J(1163,5,.)
      local c = 0
      local vars " allmag1101 allmag1102 allmag1201 allmag1202 allma..."
      
      foreach var of varlist `vars' {
                      local c = `c' + 1        
                      prais `var' year, rhotype(regress)            
                      mata :  res[`c',(1,2,3)] = st_matrix("r(table)")'[1,(1,5,6)]
                      mata : res[`c', 4] = st_numscalar("e(N)")
                      mata : res[`c', 5] = st_numscalar("e(ll)")
      }
       
      mata : res[(1,3),.]
      clear
      getmata ( est lo up N ll ) = res
      gen str20 allmag = ""
      local c = 0
      
      foreach var in `vars' {
                      local c = `c' + 1
                      replace allmag = "`var'" if _n == `c'}
      compress
      list

      Comment

      Working...
      X