Map + pmap, cannot find variables

I am trying to match simulation results using dplyr and purrr. My results are saved as a list of data frames with the results of several different classification algorithms, and I'm trying to use purrr and dplyr to summarize these results.

I'm trying to calculate - the number of objects assigned to each cluster - the number of objects in the cluster that really belong to the cluster - the number of true positive results, false positives, false negatives and true negatives using 3 different algorithms (KEEP1 - KEEP3) - for 2 algorithms I have there is access to the probability of being in a cluster, so I can compare it with alternative alpha options - and therefore I can calculate true positive results, etc. using a different alpha choice.

I found this: https://github.com/tidyverse/dplyr/issues/3101 , which I successfully used in a single list item to get exactly what I wanted:

f <- function(.x, .y) {
  sum(.x & .y)
}

actions <- list(
  .vars = lst(
    c('correct'),
    c('KEEP1', 'KEEP2', 'KEEP3'),
    c('pval1', 'pval2')
  ),
  .funs = lst(
    funs(Nk = length, N_correct = sum),
    funs(
      TP1 = f(., .y = correct),
      FN1 = f(!(.), .y = correct),
      TN1 = f(!(.), .y = !(correct)),
      FP1 = f(., .y = !(correct))
    ),
    funs(
      TP2 = f((. < alpha0) , .y = correct),
      FN2 = f(!(. < alpha0), .y = correct),
      TN2 = f(!(. < alpha0), .y = !(correct)),
      FP2 = f((. < alpha0), .y = !(correct))
    )
  )
)

reproducible_data <- replicate(2,
  data_frame(
    k = factor(rep(1:10, each = 20)),  # group/category
    correct = sample(x = c(TRUE, FALSE), 10 * 20, replace = TRUE, prob = c(.8, .2)),
    pval1 = rbeta(10 * 20, 1, 10),
    pval2 = rbeta(10 * 20, 1, 10),
    KEEP1 = pval1 < 0.05,
    KEEP2 = pval2 < 0.05,
    KEEP3 = runif(10 * 20) > .2,
    alpha0 = 0.05,
    alpha = 0.05 / 20 # divided by no. of objects in each group (k)
),
  simplify = FALSE)

# works
df1 <- reproducible_data[[1]]
pmap(actions,  ~df1 %>% group_by(k) %>% summarize_at(.x, .y)) %>%
  reduce(inner_join,by = 'k')

, . "" ( , 0, , , ). dplyr/purrr, .

# does not work
out_summary <- map(
  reproducible_data, 
  pmap(actions,  ~ as_tibble(.) %>% group_by("k") %>% summarize_at(.x, .y)) %>%
    reduce(inner_join,by = 'k')
)
# this doesn't either
out_summary <- map(
  reproducible_data, 
  pmap(actions,  ~ as_tibble(.) %>% group_by("k") %>% summarize_at(.x, .y, alpha = alpha, alpha0 = alpha0, correct = correct)) %>%
    reduce(inner_join,by = 'k')
)

'k' $group_by (k) $, $group_by ('k') $, , pmap. , dplyr purrr, .

- , $as_tibble() $ pmap. , , , . !

+6
1

map(
  reproducible_data,
  function(df1) { 
    pmap(actions,  ~ df1 %>% 
                       as_tibble() %>% 
                       group_by(k) %>% 
                       summarize_at(.x, .y)) %>% 
      reduce(inner_join, by = "k")
  } 
)

, map pmap. function map, df1, . ( pmap_df, ( , pmap_df ). Lmk, .

group_by("k") vs. group_by(k)

: group_by("k") "k" "k", . , , . - , (, dplyr, ). map df1, .

+1

All Articles