Announcement

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

  • eclass results into postfile?

    I am attempting to capture p-values (for a coefficient, not the model) after a regression into a postfile:

    Code:
    // clear all
    
    *prelims
    sysuse auto, clear
    regress mpg ib1.foreign
    matrix list r(table)
    
    *define program
    capture program drop regress1
    program define regress1, rclass // eclass ?
        replace mpg = mpg * rpoisson(2)
        regress mpg ib1.foreign
    end
    
    set seed 1234
    
    * create postfile
    tempname sim_file
    postfile sim_file p using "mysim.dta", replace
    
    * run program & extract p into postfile
    forvalues i=1/10 {
            drop _all
            quietly regress1
            tempname M
            matrix `M' = r(table)
            return scalar p = `M' [4,1]
            post sim_file (`p')
         }
    postclose sim_file
    
    use mysim.dta, clear
    sum p
    Clearly something is going wrong with the matrix / r(table) steps. There are several threads with similar problems, but I'm think I'm missing something obvious?




    Last edited by Andrew Lover; 29 Sep 2017, 08:34. Reason: Clarified which p-value is of interest here.
    __________________________________________________ __
    Assistant Professor, Department of Biostatistics and Epidemiology
    School of Public Health and Health Sciences
    University of Massachusetts- Amherst

  • #2
    Code:
    clear all
    
    *prelims
    sysuse auto, clear
    regress mpg ib1.foreign
    matrix list r(table)
    
    *define program
    capture program drop regress1
    program define regress1 // eclass ?
        replace mpg = mpg * rpoisson(2)
        regress mpg ib1.foreign
    end
    
    regress1
    
    set seed 1234
    
    * create postfile
    tempname sim_file
    postfile `sim_file' p using "mysim.dta", replace
    
    * run program & extract p into postfile
    forvalues i=1/10 {
            sysuse auto,clear
            regress1
            tempname M
            matrix `M' = r(table)
            mat list `M'
            local p = `M'[4,1]
            di `p'
            post `sim_file' (`p')
         }
    postclose `sim_file'
    
    use mysim.dta, clear
    sum p
    There were a few errors in the code you posted.
    1. After the drop _all, regress1 no longer works as your dataset is empty.
    2. You declare a tempname sim_file, but then don't enclose sim_file in apostrophes
    3. You cannot return something if you're not running a program.
    4. There was a space between the matrix name `M' and the index [4,1]
    5. You try to post a local `p', but this is nowhere defined.
    6. When you declare a program rclass, it clears the r-results. So unless you actually return something to r-, r(table) doesn't exist

    Comment


    • #3
      Fantastic, thanks Jesse- clearly I had been staring at that for too long!
      __________________________________________________ __
      Assistant Professor, Department of Biostatistics and Epidemiology
      School of Public Health and Health Sciences
      University of Massachusetts- Amherst

      Comment

      Working...
      X