How to Calculate a Binomial Confidence Interval in R?
In this article, we will discuss how to calculate a Binomial Confidence interval in R Programming Language. We can calculate Binomial Confidence Interval by using the below formulae:
p +/- z*(√p(1-p) / n)
where,
- p is for the proportion of successes
- z is the chosen value
- n is the sample size
We can calculate by using the below methods
Method 1: Use the prop.test() function
This function is used to calculate the 95% binomial confidence interval.
Syntax: prop.test(x, n, conf.level=.95, correct=FALSE)
where,
- x is the input variable
- n is the sample size
- conf.level is the confidence level which is used to calculate the 95% binomial confidence interval.
R
# calculate for 34 print ( prop.test (x = 34, n = 100, conf.level = .95, correct = FALSE )) |
Output:
1-sample proportions test without continuity correction data: 34 out of 100, null probability 0.5 X-squared = 10.24, df = 1, p-value = 0.001374 alternative hypothesis: true p is not equal to 0.5 95 percent confidence interval: 0.2546152 0.4372227 sample estimates: p 0.34
Method 2: Use the binconf() function
binconf() function is available in the Hmisc package. To install this package run the following commands:
install.packages("Hmisc")
Syntax of binconf():
Syntax: binconf(x, n, alpha)
where
- x is the input variable
- n is the sample size
- alpha is the binomial confidence level
R
# load the library library (Hmisc) # calculate for 34 with 95%confidence level print ( binconf (x=34, n=100, alpha=.05)) |
Output:
PointEst Lower Upper 0.34 0.2546152 0.4372227
Method 3: Calculate the Confidence Interval with Formulae
In this method, we will use binomial confidence interval in R using this formula:
Syntax: p + c(-qnorm(1-a/2), qnorm(1-a/2))*sqrt((1/100)*p*(1-p))
where,
- p is the proportional value
- a is the significance level
R
# p value p = 52/56 # alpha value a = 0.05 # calculate binomial interval print (p + c (- qnorm (1-a/2), qnorm (1-a/2))* sqrt ((1/100)*p*(1-p))) |
Output:
[1] 0.8780946 0.9790482
Contact Us