Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use purrr to apply a function to 3 lists using all named vectors in each list

I have some data stored in a list where I am trying to apply the following code to:

library(tidyverse)
library(purrr)
library(BVAR)
mins <- map(df1, ~ .x / 100)
maxs <- map(df1, ~ .x * 100)

So, now I have 3 lists and I want to use purrr to pmap a function over those lists:

The code I have is the following which does not work:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

pmap(
  list(
    mode = df1,
    min = mins,
    max = maxs
    # scale = 0.004,
    # shape = 0.004
  ),
  ~bv_psi(..1, ..2, ..3)
  )

The idea is to apply something like:

bv_psi(scale = 0.004, shape = 0.004, mode = df1, min = min, max = max)

Shape / Scale: 0.004 / 0.004
#1 Mode / Bounds: 0.0000000001 / [0.000000000001, 0.00000001]
#2 Mode / Bounds: 2.329554 / [0.02329554, 232.9554]
#3 Mode / Bounds: 0.3086792 / [0.003086792, 30.86792]
#4 Mode / Bounds: 0.289515 / [0.00289515, 28.9515]
#5 Mode / Bounds: 0.3444753 / [0.003444753, 34.44753]

But to all the coloumns.

Data:

df1 <- list(FRA = c(var1 = 0.348484926831081, var2 = 2.32955373143543, 
var3 = 0.308679204532957, var4 = 0.242541092355223, var5 = 0.344475341669246
), ITA = c(var1 = 0.348484926831081, var2 = 2.26657313151514, 
var3 = 0.34565008444245, var4 = 0.242541092355223, var5 = 0.403082188649582
), NOR = c(var2 = 1.20962811046986, var3 = 0.289000898298659, 
var4 = 0.242541092355223, var5 = 0.34539772064608), SPA = c(var1 = 0.348484926831081, 
var2 = 2.59042002848148, var3 = 0.748413615669766, var4 = 0.242541092355223, 
var5 = 0.403082188649582), GER = c(var1 = 0.350388888258007, 
var2 = 1.58267709209138, var3 = 0.230489915987761, var4 = 0.243815124309394, 
var5 = 0.332742111113146))

>Solution :

You can use pmap, with a names list in .l (as you did), but with your scale and shape params in ...

pmap(list(mode=df1,min=mins,max=maxs),bv_psi, scale=0.004, shape=0.004)

Alternatively, you can use ~ style call to bv_psi like these examples:

# Using default values for scale and shape
pmap(list(df1,mins,maxs), ~bv_psi(mode=..1, min=..2, max=..3))

# explicitly passing values for scale and shape
pmap(list(df1,mins,maxs), ~bv_psi(scale=0.001, shape=0.002,mode=..1, min=..2, max=..3))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading