Search code examples
rprobabilitysamplingconfidence-interval

How to calculate 95% confidence interval for a proportion in R?


Assume I own a factory that produces 150 screws a day and there is a 22% error rate. Now I am going to estimate how many screws are faulty each day for a year (365 days) with

    rbinom(n = 365, size = 150, prob = 0.22)

which generates 365 values in this way

45 31 35 31 34 37 33 41 37 37 26 32 37 38 39 35 44 36 25 27 32 25 30 33 25 37 36 31 32 32 43 42 32 33 33 38 26 24 ...................

Now for each of the value generated, I am supposed to calculate a 95% confidence interval for the proportion of faulty screws in each day.

I am not sure how I can do this. Is there any built in functions for this (I am not supposed to use any packages) or should I create a new function?


Solution

  • If the number of trials per day is large enough and the probability of failure not too extreme, then you can use the normal approximation https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval.

    # number of failures, for each of the 365 days
    f <- rbinom(365, size = 150, prob = 0.22)
    
    # failure rates
    p <- f/150
    
    # confidence interval for the failur rate, for each day
    p + 1.96*sqrt((p*(1-p)/150))
    p - 1.96*sqrt((p*(1-p)/150))