Announcement

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

  • Bootstrapped confidence interval for the fraction of positive value of a difference between two variables

    Dear Statalist users,

    I have two variable y1 and y0 and the same number of observations for each variable. I need to compute the fraction or percentage of positive differences y1-y0.
    I would like to obtain a 95% confidence interval for the value of this fraction using bootstrapping (as I have seen in a paper) but I cannot figure out how to do so.

    Thank you very much for your help.

    Best regards,

    Morgane




  • #2
    Maybe try something like the following? (Begin at the "Begin here" comment. The top part is to create a toy dataset for use in illustration.)
    Code:
    version 17.0
    
    clear *
    
    // seedem
    set seed 919663938
    
    quietly drawnorm y1 y0, double corr(1 0.5 \ 0.5 1) n(50)
    
    *
    * Begin here
    *
    program define deltEm
        version 17.0
        syntax [if]
    
        tempvar del
        generate byte `del' = (y1 - y0) > 0 // strictly positive
        summarize `del' `if', meanonly
    end
    
    quietly bootstrap prp = r(mean), bca reps(400): deltEm
    estat bootstrap, all
    
    exit
    Last edited by Joseph Coveney; 30 Mar 2023, 09:39. Reason: tempname → tempvar

    Comment


    • #3
      Dear Joseph,

      Thank you very much for your help, it worked and helped me a lot.

      Best,

      Morgane

      Comment


      • #4
        Originally posted by Morgane Monjour View Post
        Thank you very much for your help, it worked and helped me a lot.
        You're welcome, but it dawned on me afterward that there's no need to re-generate the indicator variable for every bootstrap sample.

        Taking that out of the loop makes things a bit cleaner and more efficient.
        Code:
        *
        * Begin here
        *
        
        tempvar del
        generate byte `del' = (y1 - y0) > 0
        
        quietly bootstrap prp = r(mean), bca reps(400): summarize `del', meanonly
        estat bootstrap, all
        
        exit

        Comment

        Working...
        X