Home > Net >  Three inputs to pmap but only iterate over two of them
Three inputs to pmap but only iterate over two of them

Time:03-31

I've got this function:

power_fcn <- function(.x, .y, .l){
  tmp <- pwr.t.test(n = .x, 
                    d = NULL, 
                    power = 0.8, 
                    sig.level = .y,
                    type = .l, 
                    alternative = "two.sided")

  tibble(n_per_arm = .x, 
         power = tmp$power, 
         sig_level = tmp$sig.level, 
         test = tmp$alternative,
         type = tmp$type, 
         cohens_d = tmp$d)
}

I want to be able to run this twice, and change the type on each call. For example, the first call would be for type = "two.sample" and would look like this:

d1 <- crossing(n = seq(100, 500, by = 100),
               alpha = c(0.05, 0.005))

power_df <- purrr::pmap_dfr(
  .x = d1$n,
  .y = d1$alpha,
  .l = "two.sample", #this is the bit i want to be able to vary on each call
  .f = power_fcn
)

However, R is trying to iterate over .l as well as .x and .y. I don't want it to do that. I could include type in the crossing() but that would mean I could only ever run the power_fcn call once and it would include both "two.sample" and "paired", for example. If I want to be able to run it multiple times, e.g.:

power_df_twosample <- purrr::pmap_dfr(
 .x = d1$n,
 .y = d1$alpha,
 .l = "two.sample",
 .f = power_fcn
)

power_df_paired <- purrr::pmap_dfr(
 .x = d1$n,
 .y = d1$alpha,
 .l = "paired",
 .f = power_fcn
)

I need to find a way for R to not iterate over .l, or find another way to pass a dynamic object to the power_fcn (for type) within map2_dfr.

Can anyone help here?

CodePudding user response:

I guess this one works. .l in pmap should be a list of vectors (or a dataframe) and your function iterates over them. Other arguments such as type (or .x and .y in you original code) are passed also and I think they don't need to be vectors nor they are iterated.

power_fcn <- function(n, alpha, type) {
  tmp <- pwr.t.test(n = n, 
                    d = NULL, 
                    power = 0.8, 
                    sig.level = alpha,
                    type = type, 
                    alternative = "two.sided")

  tibble(n_per_arm = n, 
         power = tmp$power, 
         sig_level = tmp$sig.level, 
         test = tmp$alternative,
         type = tmp$type, 
         cohens_d = tmp$d)
}

d1 <- crossing(n = seq(100, 500, by = 100),
               alpha = c(0.05, 0.005))

power_df <-
  pmap_dfr(.l = d1, .f = power_fcn, type = "two.sample")

##> # A tibble: 10 × 5
##>    n_per_arm power sig_level test      cohens_d
##>        <dbl> <dbl>     <dbl> <chr>        <dbl>
##>  1       100   0.8     0.005 two.sided    0.521
##>  2       100   0.8     0.05  two.sided    0.398
##>  3       200   0.8     0.005 two.sided    0.367
##>  4       200   0.8     0.05  two.sided    0.281
##>  5       300   0.8     0.005 two.sided    0.299
##>  6       300   0.8     0.05  two.sided    0.229
##>  7       400   0.8     0.005 two.sided    0.259
##>  8       400   0.8     0.05  two.sided    0.198
##>  9       500   0.8     0.005 two.sided    0.231
##> 10       500   0.8     0.05  two.sided    0.177

  • Related