Calculate risk ratios and risk differences using a Poisson distribution for person-time data. Function works on individual level data or aggregated data p244 2nd Edition

rate(data, outcome, denominator, exposure, per_unit, ci_level = 95)

Arguments

data

A dataframe

outcome

Variable with the outcomes as a numeric variable

denominator

Variable giving the amount of time at risk

exposure

Variable giving whether exposed or not

per_unit

Multiplier for rate values, e.g. 1000 for n outcomes per 1000 denominator

ci_level

A numeric value giving the confidence interval

Examples


# Using individual level data

data(ebola)
library(dplyr)
#> 
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#> 
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#> 
#>     intersect, setdiff, setequal, union
ebola %>%
 mutate(male = ifelse(sex == "male", 1, 0)) %>%
  rate(outcome = died, denominator = days_at_risk, exposure = male,
  per_unit = 100)
#> # A tibble: 2 × 10
#>    male outcome denominator  rate rate_ratio rate_ratio_lci rate_ratio_uci
#>   <dbl>   <dbl>       <dbl> <dbl>      <dbl>          <dbl>          <dbl>
#> 1     0     133        1056  12.6       1.04          0.799           1.36
#> 2     1      91         754  12.1       1             0.766           1.31
#> # ℹ 3 more variables: rate_diff <dbl>, rate_diff_lci <dbl>, rate_diff_uci <dbl>

# Using aggregated data
# Table 14-2
cancer_xray <- data.frame(cases = c(41, 15), pyar = c(28010, 19017),
radiation = c(1, 0))
cancer_xray
#>   cases  pyar radiation
#> 1    41 28010         1
#> 2    15 19017         0
cancer_xray %>%
  rate(outcome = cases, denominator = pyar, exposure = radiation,
    per = 100000)
#> # A tibble: 2 × 10
#>   radiation outcome denominator  rate rate_ratio rate_ratio_lci rate_ratio_uci
#>       <dbl>   <dbl>       <dbl> <dbl>      <dbl>          <dbl>          <dbl>
#> 1         0      15       19017  78.9       1             0.554           1.81
#> 2         1      41       28010 146.        1.86          1.03            3.35
#> # ℹ 3 more variables: rate_diff <dbl>, rate_diff_lci <dbl>, rate_diff_uci <dbl>