Thank you very much @mishabalyasin,
As for my 3-rd question:
Yes, you are right (I have got this on my mind) I would like to extract p.values from an object called marg_combos according to my another post:
https://forum.posit.co/t/getting-all-possible-combinations-for-2x2-tables-with-fixed-margins-and-totals/103480/5
The topic was solved and finally I found a solution but I regarded this as workoround using intermediate steps and cbind() function, excel, etc. I was wondering if another more elegant way exists and if it could be done in purrr on one go, having the desired result as this below or just three columns: tab_id, mat, P_values_extracted:
This is why I am learning purrr now.
What I tried so far (taken from my previous post):
library(tidyverse)
## Constraints
r1_marg <- 20
r2_marg <- 20
c1_marg <- 29
c2_marg <- 11
## Range of values
r1c1 <- c(0:max(c(r1_marg, c1_marg)))
r1c2 <- c(0:max(c(r1_marg, c2_marg)))
r2c1 <- c(0:max(c(c1_marg, r2_marg)))
r2c2 <- c(0:max(c(c2_marg, r2_marg)))
marg_combos <- expand_grid(r1c1, r1c2,
r2c1, r2c2) %>%
filter(r1c1 + r1c2 == r1_marg &
r2c1 + r2c2 == r2_marg &
r1c1 + r2c1 == c1_marg &
r1c2 + r2c2 == c2_marg) %>%
tibble::rowid_to_column(var = "tab_id") %>%
pivot_longer(r1c1:r2c2, names_to = "pos") %>%
group_by(tab_id) %>%
summarize(mat = list(matrix(value,
nrow = 2, ncol = 2, byrow = TRUE))) %>%
group_by(tab_id) %>%
mutate(fisher = map(mat, fisher.test))
marg_combos
#> # A tibble: 12 x 3
#> # Groups: tab_id [12]
#> tab_id mat fisher
#> <int> <list> <list>
#> 1 1 <int[,2] [2 x 2]> <htest>
#> 2 2 <int[,2] [2 x 2]> <htest>
#> 3 3 <int[,2] [2 x 2]> <htest>
#> 4 4 <int[,2] [2 x 2]> <htest>
#> 5 5 <int[,2] [2 x 2]> <htest>
#> 6 6 <int[,2] [2 x 2]> <htest>
#> 7 7 <int[,2] [2 x 2]> <htest>
#> 8 8 <int[,2] [2 x 2]> <htest>
#> 9 9 <int[,2] [2 x 2]> <htest>
#> 10 10 <int[,2] [2 x 2]> <htest>
#> 11 11 <int[,2] [2 x 2]> <htest>
#> 12 12 <int[,2] [2 x 2]> <htest>
Created on 2021-05-09 by the reprex package (v2.0.0)
Thanks and credits to kjhealy.
My previous attempts:
marg_combos %>% mutate(p_value=map(.$fisher,~broom::tidy(.x))) %>% unnest(p.value)
which gives:
mutate(stats = map(marg_combos$fisher, ~broom::glance(.x)))
marg_combos %>%
split(.$mat) %>%
map(pluck, "p.value")
on which my laptop hangs all the time
marg_combos %>% bind_rows(marg_combos) %>%
mutate_if(is.list, simplify_all) %>%
unnest()
unlist(lapply(marg_combos, function(i) i[[3]][[p.value]]))
and other errors telling me that input should has got names and that $ is not good for atomic vectors.
I would like to kindly ask you to comment about these errors, so it would be very educational for me in order to understand.
Thank you very much indeed,