Incidence rate confidence interval in R

Viewed 86

I assume that I miss the correct English terms here to search for myself.

I calculate the incidence rate like this:

> observed_cases_n = 112575
> population_n = 752487
> rate_relation = 1000
> CI = 95
> ir = observed_cases_n / population_n * rate_relation

The result is

> ir
[1] 149.6039

Is the term "incidence rate" correct here?

Now I want to compute the confidence interval for that. I can do this with Python code but I don't know how to translate that into R. This is the Python code.

import math
from statistics import NormalDist

ir = 149.6039

# "incidence rate" as cases per 1.000 people
rate_relation = 1000

# Strength of the confidence interval
CI = 95

# Variance of raw incidence rate
ir_variance = ir * (rate_relation - ir) / population_n

nd = NormalDist(mu=1, sigma=0.5).inv_cdf((1 + CI / 100) / 2)

# Confidence interval
ci_lower = ir - nd * math.sqrt(ir_variance)
ci_upper = ir + nd * math.sqrt(ir_variance)

print(f'The {CI}% confidence interval is {ci_lower} to {ci_upper}.')
1 Answers

Normal distribution percentiles can be calculated as (analogous to NormalDist.inv_cdf)

nd = qnorm((1 + CI / 100) / 2, mean=0, sd=1)

Square roots are simply sqrt

ci_lower = ir - nd * sqrt(ir_variance)
ci_upper = ir + nd * sqrt(ir_variance)

To print out the result you can use cat:

cat(paste0('The ', CI, '% ',
           'confidence interval is ',
           ci_lower, ' to ', ci_upper, '.'))

> The 95% confidence interval is 148.798014280215 to 150.409812565963.
Related