### How to calculate confidence interval of a proportion using R.

confidence interval of a proportion is now one of the important aspect of statistics use in everyday life in fact almost all big company make use of it especially those companies with artificial intelligent and tech.

let's take a look at some questions and see how R could have be of help to solve it.

### Question1

What is the critical value if n = 100 and confidence interval is 99%?

1.96

2.32

2.63

1.68

**2.58**

### Question2

Out 1500 students in Idia hostel, 1000 are allergic to Zikites so often avoid Zik hall route. Half of the remaining proportion wouldn't use Indy route as well. Obtain 95% confidence interval of the true population proportion of students who would use Zik route?

[0.6353, 0.6981]

[0.3019, 0.3647]

[0.8145, 0.8522]

**[0.3095, 0.3572]**

[0.8085, 0.8582]

### Question3

In the last revision class, 0.7 actively participated. What is the standard error if the population of students in attendance was 2500?

0.038

0.21

0.029

0.458

**No answer**

### Question4

If we use a ______ confidence percentage, then our margin of error will be _______.

[small, large]

[small, small]

[large, small]

[medium, large]

**[large, large]**

### Question5

Out 1500 students in Idia hostel, 1000 are allergic to Zikites so often avoid Zik hall route. Half of the remaining proportion wouldn't use Indy route as well. Obtain 95% confidence interval of the true population proportion of students who wouldn't use Indy route?

**[0.1478, 0.186]**

[0.1418, 0.1915]

[0.1505, 0.1828]

[0.8145, 0.8522]

[0.8085, 0.8582]

### Question6

A study finds that out of 2305 Awoites, 1383 Awoites are friends with Katangites. What is the margin of error, at 95% confidence interval?

4%

**2%**

4.5%

2.5%

3%

### Question7

What is the critical value if n = 20 and confidence interval is 99%?

**2.54**

2.63

2.86

1.96

2.58

### Question8

Given that the MOE is ±4.5%. At 95% confidence interval and standard deviation of 2, what is the sample size?

0.7588

76

**7588**

7588.34

75.88

### Question9

Out 1500 students in Idia hostel, 1000 are allergic to Zikites so often avoid Zik hall route. Half of the remaining proportion wouldn't use Indy route as well. Obtain 95% confidence interval of the true population proportion of students who wouldn't use Zik route?

[0.6353, 0.6981]

[0.3019, 0.3647]

**[0.6428, 0.6905]**

[0.8145, 0.8522]

[0.8085, 0.8582]

### Question10

A study finds that 0.6 of every Awoite are friends with Zikites. If the MOE is 2%. How many Awoites are friends with Zikites?

2304.96

**2305**

23

23.05

SOLUTION

firstly I will create a robot that will keep solving the questions that look alike so the codes are:

```
# for confidence Interval when given a proportion,number of population and the confidence level
confidenceInterval=function(p,n,confidenceLevel){
pn=p/n
q=1-pn
alpha=1-confidenceLevel
me=abs(qnorm(alpha/2))*sqrt(pn*q/n)
CI=p+me
CI2=p-me
print(CI)
print(CI2)
}
# To calculate the confidence Interval proportions when proportion,number of population and the confidence level are given
confprops=function(proportionSize,populationSize,confidenceLevel){
pproportion=proportionSize/populationSize
qproportion=1-pproportion
alpha=1-confidenceLevel
marginerror=abs(qnorm(alpha/2))*sqrt(pproportion*qproportion/n)
CI=pproportion+marginerror
CI2=pproportion-marginerror
print(CI)
CI2
}
# To Calculate the margin error when given proportion size,poputlation size and confidence level
marginOfError=function(proportionSize,populationSize,confidenceLevel){
p=proportionSize/populationSize
q=1-p
alpha=1-confidenceLevel
zscore=abs(qnorm(alpha/2))
marginOfError=zscore*sqrt(p*q/populationSize)
marginOfError
}
To calculate the population size when margin error, confidence level and standard deviation is given.
size=function(marginOfError,confidenceLevel,standardDeviation){
alpha=1-confidenceLevel
zscore=abs(qnorm(alpha/2))
size=(zscore*standardDeviation/marginOfError)^2
size
}
```

Copy the code above and paste. then you have just created a robot for most of the questions we need to solve concerning the topic.

### for question1

To calculate the critical value we calculate the zscore of the half-alpha.

where alpha=1-confidence level

Codes>>

```
confidenceLevel=99/100
alpha=1-confidenceLevel
half_alpha=alpha/2
criticalValue=abs(qnorm(half_alpha))
print(criticalValue)
```

Result>>

```
> confidenceLevel=99/100
> alpha=1-confidenceLevel
> half_alpha=alpha/2
> criticalValue=abs(qnorm(half_alpha))
> print(criticalValue)
[1] 2.575829
```

Question2,5,9

Note that the number of student that will use the Zik's route are 500. s0, P-value=500, n=1500,ConfidLevel=95%=0.95. so we use our robot and call the function.

Also those that will not use indy in question5 are 250. because half of 500 is 250.

so, P-value=250, n=1500,ConfidLevel=95%=0.95.

Question9 is also similar those that will not use zik are 1000.

so, P-value=1000, n=1500,ConfidLevel=95%=0.95

codes>>

```
# To calculate the confidence Interval proportions when proportion,number of population and the confidence level are given
confprops=function(proportionSize,populationSize,confidenceLevel){
pproportion=proportionSize/populationSize
qproportion=1-pproportion
alpha=1-confidenceLevel
marginerror=abs(qnorm(alpha/2))*sqrt(pproportion*qproportion/populationSize)
CI=pproportion+marginerror
CI2=pproportion-marginerror
print(CI)
CI2
}
confprops(500,1500,0.95)
confprops(250,1500,0.95)
confprops(1000,1500,0.95)
```

Result>>

```
> > confprops=function(proportionSize,populationSize,confidenceLevel){
+ pproportion=proportionSize/populationSize
+ qproportion=1-pproportion
+ alpha=1-confidenceLevel
+ marginerror=abs(qnorm(alpha/2))*sqrt(pproportion*qproportion/populationSize)
+ CI=pproportion+marginerror
+ CI2=pproportion-marginerror
+ print(CI)
+ CI2
+ }
> confprops(500,1500,0.95)
[1] 0.3571893
[1] 0.3094774
> confprops(250,1500,0.95)
[1] 0.1855264
[1] 0.1478069
> confprops(1000,1500,0.95)
[1] 0.6905226
[1] 0.6428107
```

Question3.

for question 3 we are given the proportion already and the population size.

so standardError= stanadardDeviation/sqrt(N) or square root of {p(1-p)/n}. now our P=0.7 and n=2500. so we solve with codes

Codes>>>

```
p=0.7
q=1-p
n=2500
standardError=sqrt(p*q/n)
print(standardError)
```

Result>>|

```
p=0.7
> q=1-p
> n=2500
> standardError=sqrt(p*q/n)
> print(standardError)
[1] 0.009165151
```

### Question4

This is very direct as we all known that the higher the confidence level the bigger the margin of error needed.

Question6

here we need margin of error just copy the code I have written for it as robot. and call the function substituting respective value.

N=2305 P=1383 CL=0.95

```
size,poputlation size and confidence level
marginOfError=function(proportionSize,populationSize,confidenceLevel){
p=proportionSize/populationSize
q=1-p
alpha=1-confidenceLevel
zscore=abs(qnorm(alpha/2))
marginOfError=zscore*sqrt(p*q/populationSize)
marginOfError
}
marginOfError(1383,2305,0.95)
```

Result>>

```
> marginOfError=function(proportionSize,populationSize,confidenceLevel){
+ p=proportionSize/populationSize
+ q=1-p
+ alpha=1-confidenceLevel
+ zscore=abs(qnorm(alpha/2))
+ marginOfError=zscore*sqrt(p*q/populationSize)
+ marginOfError
+ }
> marginOfError(1383,2305,0.95)
[1] 0.01999946
```

Notice: if you approximate this you get 0.02. which is the same as 2%.

Question8.

Firstly we make N the subject of the formula. that give us

size(N)=(zscore*standardDeviation/marginOfError)^2.

that is how we make the robot using the important parameters. now let's see the codes when ME=0.45,CL=0.95,SD=2

```
size=function(marginOfError,confidenceLevel,standardDeviation){
alpha=1-confidenceLevel
zscore=abs(qnorm(alpha/2))
size=(zscore*standardDeviation/marginOfError)^2
size
}
size(0.045,0.95,2)
```

Results>>

```
> size=function(marginOfError,confidenceLevel,standardDeviation){
+ alpha=1-confidenceLevel
+ zscore=abs(qnorm(alpha/2))
+ size=(zscore*standardDeviation/marginOfError)^2
+ size
+ }
> size(0.045,0.95,2)
[1] 7588.067
```

Question10.

from question6 it is clear that there are 2305 of awoite. so

P=0.6,N=2305, ME=0.02.

since Confidence interval = P+ or - ME

so, cofidnce interval =0.6+0.02 =0.62

number of student friendly with zik =0.62*2305= 1429

since we are given the proportion we multiply by the total population to get the actual value.

so actually No answer but we pick something likely to be answer.

I hope you find this article helpful?? it is still your boy Maxwizard!.

you are free to chat me up on whatsapp `09153036869`

if you have any doubt or correction on this. Enjoy coding!

## Top comments (0)