Announcement

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

  • calculating 95%CI for single proportion

    Hi,

    I am calculating 95% CI for single proportion using stata and wrote a sample command based on formula for doing this:
    Code:
    phat = +- z*(sqrt(phat*(1-phat))/n
    .

    I am wondering if I am correct with my answers or if there are other reliable and more efficient way of doing this in stata.

    Code:
    clear
    input str6 race long m long x
    BLK 67 8
    BLK 68 11
    BLK 60 56
    CAU 49 43
    CAU 48 3
    CAU 67 2
    ASI 134 5
    ASI 64 1
    ASI 48 5
    end
    
    gen phat=(x/m)
    gen prop = phat*100
    
    gen lb = (phat - 1.96*(sqrt(phat*(1-phat))/m))*100
    gen ub = (phat + 1.96*(sqrt(phat*(1-phat))/m))*100
    The result I got is as follows:
    Code:
     
    race m x phat prop lb ub
    BLK 67 8 .119403 11.9403 10.99171 12.88889
    BLK 68 11 .1617647 16.17647 15.11509 17.23785
    BLK 60 56 .9333333 93.33334 92.51849 94.14819
    CAU 49 43 .877551 87.7551 86.44389 89.06631
    CAU 48 3 .0625 6.25 5.261582 7.238418
    CAU 67 2 .0298507 2.985075 2.487248 3.482901
    ASI 134 5 .0373134 3.731343 3.454122 4.008564
    ASI 64 1 .015625 1.5625 1.18269 1.94231
    ASI 48 5 .1041667 10.41667 9.169304 11.66403
    Please kindly respond if there are other methods simple and/or complex for calculation confidence intervals for single proportion.

    Regards.

  • #2
    Well your formula for the confidence intervals is wrong: the /m needs to be inside the square root. There is also the question whether you want to use normal-theory confidence intervals. Particularly with some of the very small values of x, their coverage probabilities will likely be quite off the mark. And some of the lower limits will be negative.

    You might be better off using the Clopper-Pearson "exact" confidence interval that actually provides the nominal coverage level. You can do this by running the -cii- command on each observation in your data and saving the results. The -runby- command, written by Robert Picard and me, and available from SSC, automates this process fairly simply:

    Code:
    clear
    input str6 race long m long x
    BLK 67 8
    BLK 68 11
    BLK 60 56
    CAU 49 43
    CAU 48 3
    CAU 67 2
    ASI 134 5
    ASI 64 1
    ASI 48 5
    end
    
    capture program drop one_ci
    program define one_ci
        cii proportions  `=m[1]' `=x[1]'
        gen prop = 100*r(proportion)
        gen lb = 100*r(lb)
        gen ub = 100*r(ub)
        exit
    end
    
    gen long obs_no = _n
    runby one_ci, by(obs_no)

    Comment


    • #3
      Also asked only last week: https://www.statalist.org/forums/for...rtions-command

      Comment


      • #4
        Thanks Clyde, your explanation and code was helpful.
        Thanks Nick for providing more information re codes.

        Comment

        Working...
        X