Announcement

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

  • Using postestimation margins and returned results to generate new variable

    Dear Forum,

    I will appreciate guidance on how I can create a new variable from stored results.

    Specifically, I ran a logit model (#1, below) with postestimation margins (#2) to calculate probability of positive outcome (returned as "Margin" in #2). I would like to generate a new variable equal to the returned "Margin".

    I thought if I used the "return list" function (#3, below) the "margin" will be returned under the scalars, and I could then use "generate newvar = r(margin)". However, the returned list of scalars does not have "margin".

    Please kindly help with suggestions.

    Regards,
    Itai

    __________


    #1. logit hpt i.hiv i.sex i.race [pweight = wt_hiv], vce(robust) cformat(%9.2f)

    #2. margins sex race, predict(pr) over(hiv) cformat(%9.2f)

    Click image for larger version

Name:	Margins.PNG
Views:	1
Size:	109.5 KB
ID:	1632772



    #3. return list

    Click image for larger version

Name:	Return list.PNG
Views:	1
Size:	106.5 KB
ID:	1632773

  • #2
    If you use the -post- option of margins, you can extract these from the matrix -r(table)-.

    Code:
    logit hpt i.hiv i.sex i.race [pweight = wt_hiv], vce(robust) cformat(%9.2f)
    margins sex race, predict(pr) over(hiv) cformat(%9.2f) post
    mat Margin = (r(table)[1,1...])'
    svmat Margin
    list Margin in 1/15

    Comment


    • #3
      The results you seek are returned in the matrix r(table). Try
      Code:
      matrix list r(table)
      following the margins command to see that it contains all the results from the table.

      Added in edit: crossed with Andrew's post, which leads me to believe that I don't fully understand what you were seeking.
      Last edited by William Lisowski; 21 Oct 2021, 14:46.

      Comment


      • #4
        Thank you very much Andrew and William. The suggestion by Andrew worked very well.

        Comment


        • #5
          A further question, for which guidance will be much appreciated. I would like to calculate prevalence ratio and prevalence difference between 2 levels of a dichotomous exposure (HIV) as shown in table below. I thought I might use margins with expression command along the lines shown below but obviously I am out of depth.

          NOTE - the text in red is not part of the STATA code but illustrative of my coding intent.


          logit hpt i.hiv i.sex i.agegrp i.race [pweight = wt_hiv], vce(robust) cformat(%9.2f) margins sex agegrp race, expression((predict(pr) [for HIV pos] - (predict(pr) [for HIV neg])) cformat(%9.2f) | for prevalence difference margins sex agegrp race, expression((predict(pr) [for HIV pos] / (predict(pr) [for HIV neg])) cformat(%9.2f) | for prevalence ratio


          Please kindly help with suggestions.

          ____________

          Click image for larger version

Name:	Adjusted PD and PR.PNG
Views:	1
Size:	26.1 KB
ID:	1632842

          Comment

          Working...
          X