Calculating Confidence Intervals (group proportions) for complete columns

Viewed 15

I have the following dataset.

# A tibble: 10 x 6
   Geslacht age_cat    DD   pop year  prevalence
   <chr>    <fct>   <int> <int> <chr>      <dbl>
 1 M        <40        42 36642 2021      0.115 
 2 M        41-50      88 16327 2021      0.539 
 3 M        51-60     289 20232 2021      1.43  
 4 M        61-70     558 18068 2021      3.09  
 5 M        71-80     527 14025 2021      3.76  
 6 M        81-90     174  5555 2021      3.13  
 7 M        90+        22  1293 2021      1.70  
 8 V        <40        27 35887 2021      0.0752
 9 V        41-50      36 16444 2021      0.219 
10 V        51-60     206 20178 2021      1.02 

df <- structure(list(Geslacht = c("M", "M", "M", "M", "M", "M", "M", 
"V", "V", "V", "V", "V", "V", "V"), age_cat = structure(c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("<40", 
"41-50", "51-60", "61-70", "71-80", "81-90", "90+"), class = "factor"), 
    DD = c(42L, 88L, 289L, 558L, 527L, 174L, 22L, 27L, 36L, 206L, 
    347L, 321L, 160L, 29L), pop = c(36642L, 16327L, 20232L, 18068L, 
    14025L, 5555L, 1293L, 35887L, 16444L, 20178L, 17965L, 14437L, 
    7150L, 2300L), year = c("2021", "2021", "2021", "2021", "2021", 
    "2021", "2021", "2021", "2021", "2021", "2021", "2021", "2021", 
    "2021"), prevalence = c(0.114622564270509, 0.538984504195504, 
    1.428430209569, 3.08833296435687, 3.75757575757576, 3.13231323132313, 
    1.7014694508894, 0.0752361579402012, 0.218924835806373, 1.02091386658737, 
    1.9315335374339, 2.22345362609961, 2.23776223776224, 1.26086956521739
    )), row.names = c(NA, -14L), class = c("tbl_df", "tbl", "data.frame"
))

The column Prevalence is (df$DD(events) / df$Pop(population)) * 100. Previously ive used the function exactci to calculate the CI for only one row(for proportions and Clopper–Pearson exact method).

exactci(events, population,
        conf.level = 0.95)

However, this function does not allow to calculate it for a complete column. Because there are several (larger) datasets, calculating the CI for each row like this will cost a lot of time. Is there a way to calculate the CI intervals for all the rows in the same script?

As a desired result I would like to add two different columns with the 95% confidence intervals (lower & upper) to the DF.

   Geslacht age_cat    DD   pop year  prevalence    LCI     UCI
 1 M        <40        42 36642 2021      0.115     ...     ...
 2 M        41-50      88 16327 2021      0.539     ...     ...
 3 M        51-60     289 20232 2021      1.43      etc
 4 M        61-70     558 18068 2021      3.09  
 5 M        71-80     527 14025 2021      3.76  
 6 M        81-90     174  5555 2021      3.13  
 7 M        90+        22  1293 2021      1.70  
 8 V        <40        27 35887 2021      0.0752
 9 V        41-50      36 16444 2021      0.219 
10 V        51-60     206 20178 2021      1.02 
0 Answers
Related