Announcement

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

  • Extracting the same specific matrix cell results for multiple independent variables through a loop using postfile and post

    Hello,

    I am using the punaf command to obtain a Population Attributable Fraction with 95% CI after running a regression command:

    Code:
    logit asthma2 i._racegr2 ib4._smoker3, or vce(robust)
    punaf, atspec(_smoker3=4) eform
    matrix list r(cimat)
    This displays:

    HTML Code:
    r(cimat)[1,3]
          Estimate    Minimum    Maximum
    PAF  .09701989  .08873796  .10522656
    I would like to use the postfile command to store these three results in a new dataset for MULTIPLE independent variables. I created the following loop with local macros to store the results:

    Code:
    tempname memhold
    postfile memhold paf_asthma paf_ast_ll paf_ast_ul paf_bpmeds paf_bpm_ll paf_bpm_ul using "newdataset", replace
    
    quietly {
        foreach yvar in asthma2 bpmeds {
                logit `yvar' i._racegr2 ib4._smoker3, or vce(robust)
                punaf, atspec(_smoker3=4) eform
                matrix A= r(cimat)
                local a = A[1,1]
                local b = A[1,2]
                local c = A[1,3]
                post memhold (`a') (`b') (`c')
                                       }
            }
    
    postclose memhold
    
    use "newdataset", clear
    
    list
    Which generates the following error:

    post: 6 expressions expected and only 3 found
    r(198);
    Basically the line featuring post should have 6 placeholders for the 6 variables I specify in the postfile line, rather than the 3 I currently have. Changing it to:

    Code:
    post memhold (`a') (`b') (`c') (`a') (`b') (`c')
    Creates 2 observations rather than 1 and repeats the PAF estimate and confidence limits twice in each line:

    +-----------------------------------------------------------------------+
    | paf_ast~a paf_as~ll paf_as~ul paf_bpm~s paf_bp~ll paf_bp~ul |
    |-----------------------------------------------------------------------|
    1. | .0970199 .088738 .1052266 .0970199 .088738 .1052266 |
    2. | -.0064044 -.0091866 -.0036298 -.0064044 -.0091866 -.0036298 |
    +-----------------------------------------------------------------------+

    I would just like to have the 6 variables with only 1 observation, and with the first 3 cells in line 2 moved up to their correct position in cells 4-6 in line 1. Is there a loop that I could add within the code for post memhold to accomplish this? Thank you!
Working...
X