Home > other >  Can I run a bunch of poisson.exact tests on summarized data?
Can I run a bunch of poisson.exact tests on summarized data?

Time:07-06

I have my data summarized in a table and want to run poisson.exact tests on each summarized row. I am reading my data from a csv file. I can get the desired output if I run the query on just one row of data. I'm having trouble figuring out how to run a poisson test for each row separately, though. enter image description here

    results <- tribble(
  ~group, ~a, ~b, ~c, ~d,
  1,    302,    70137,  180541.2398,    3158205.224,
  2,    3673,   66766,  1298973.019,    2039773.445,
  3,    16142,  54297,  958777.9383,    2379968.526,
  4,    50322,  20117,  900454.267, 2438292.197
)

The results2 code works for one row and contains all the output I'm looking for.

    results <- tribble(
  ~group, ~a, ~b, ~c, ~d,
  1,    302,    70137,  180541.2398,    3158205.224
)
    results2 <- do(results, tidy(poisson.test(c(.$a,.$b),c(.$c,.$d))))

enter image description here

I can get the df2 code to work on the multiple rows version but it calculates a rate for a/b and I'm looking for a rate ratio plus all the other fields from poisson.test above.

    results <- tribble(
  ~group, ~a, ~b, ~c, ~d,
  1,    302,    70137,  180541.2398,    3158205.224,
  2,    3673,   66766,  1298973.019,    2039773.445,
  3,    16142,  54297,  958777.9383,    2379968.526,
  4,    50322,  20117,  900454.267, 2438292.197
)

    df2=results %>%
    mutate (rate = map2_dbl(.$a,.$b, ~poisson.exact(.x, .y)$estimate))

Can anyone help me get the first output for every row in my table?

CodePudding user response:

You can do the following

library(tidyverse)
library(broom)
results %>%
    transpose() %>%
    map_dfr(
        ~ tidy(poisson.test(c(.x$a, .x$b), c(.x$c, .x$d))) %>%
            mutate(group = .x$group, .before = 1))
## A tibble: 4 x 9
#  group estimate statistic     p.value parameter conf.low conf.high method                 alternative
#  <dbl>    <dbl>     <dbl>       <dbl>     <dbl>    <dbl>     <dbl> <chr>                  <chr>      
#1     1   0.0753       302 Inf.  e-324     3809.   0.0670    0.0843 Comparison of Poisson~ two.sided  
#2     2   0.0864      3673 Inf.  e-324    27405.   0.0835    0.0893 Comparison of Poisson~ two.sided  
#3     3   0.738      16142   2.72e-265    20228.   0.725     0.751  Comparison of Poisson~ two.sided  
#4     4   6.77       50322 Inf.  e-324    18997.   6.66      6.89   Comparison of Poisson~ two.sided  

Explanation: We transpose the tibble to turn it into a "by-row" list, then use map_dfr with broom::tidy to store the output of poisson.test in a tibble. We then add the group information, and place it in a column before all other columns using .before = 1.


Sample data

results <- tribble(
    ~group, ~a, ~b, ~c, ~d,
    1,    302,    70137,  180541.2398,    3158205.224,
    2,    3673,   66766,  1298973.019,    2039773.445,
    3,    16142,  54297,  958777.9383,    2379968.526,
    4,    50322,  20117,  900454.267, 2438292.197
)
  • Related