I have a foodborne outbreak dataset (I made up the data in this example for privacy reasons) of foods at a picnic that tells which people ate the food and which people got sick. I would like to create functions and code to generate a table with automated calculations of attack rate and risk ratio.
First, this is the dataset I have:
patient sick apple banana strawberry yogurt sandwich
1: patient1 yes no no yes no no
2: patient2 yes yes no no yes no
3: patient3 no yes yes yes yes yes
4: patient4 yes no no yes yes no
5: patient5 no no no no yes no
6: patient6 no no no no no no
7: patient7 yes no no no yes yes
To calculate the attack rate (in this case for an apple), I use this code:
sick_apple <- picnic %>%
group_by(sick, apple) %>%
summarise(total=n())
sick_apple
This gives an output like this (sick=="yes" means they got sick, and apple=="yes" means they ate an apple):
# A tidytable: 4 × 3
# Groups: sick
sick apple total
<chr> <chr> <int>
1 no no 2
2 no yes 1
3 yes no 3
4 yes yes 1
In this example, the attack rate for people who ate an apple is the number of people who ate an apple and got sick, divided by the total number of people who ate an apple, in this case 1/(1+1)=0.5. The attack rate for people who did not eat an apple would therefore be 3/(3+2)=0.6.
Okay, so now for the risk ratio. This one is a little more complex to calculate and I use a function from the epitools package to do this one. To calculate the risk ratio for an apple I use this code:
apple_table <- matrix(c(2,1,3,1), nrow=2, ncol=2)
apple_table
riskratio.wald(apple_table)
This gives an output that looks like this:
> apple_table <- matrix(c(2,1,3,1), nrow=2, ncol=2)
> apple_table
[,1] [,2]
[1,] 2 3
[2,] 1 1
> riskratio.wald(apple_table)
$data
Outcome
Predictor Disease1 Disease2 Total
Exposed1 2 3 5
Exposed2 1 1 2
Total 3 4 7
$measure
risk ratio with 95% C.I.
Predictor estimate lower upper
Exposed1 1.0000000 NA NA
Exposed2 0.8333333 0.1751513 3.964825
$p.value
two-sided
Predictor midp.exact fisher.exact chi.square
Exposed1 NA NA NA
Exposed2 0.8571429 1 0.8091498
$correction
[1] FALSE
attr(,"method")
[1] "Unconditional MLE & normal approximation (Wald) CI"
In the section of output under $measure, the risk ratio is 0.83333.
So those are the calculations that I am concerned with. What I want to do next (but have no idea how to do) is use this to generate an automated table for a list of foods that looks like this:
# A tibble: 5 × 8
food_item ate_got_sick total.1 ate_attack_rate didnt_eat_got_sick total.2 didnt_eat_attack_rate risk_ratio
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Apple 1 2 0.5 3 5 0.6 0.833
2 Banana 0 1 0 4 6 0.667 0
3 Strawberry 2 3 0.667 2 4 0.5 1.33
4 Yogurt 3 5 0.6 1 2 0.5 1.33
5 Sandwich 1 2 0.5 3 5 0.6 1.11