使用purrr将函数应用于3个列表,使用每个列表中的所有命名向量

js4nwp54  于 2023-03-15  发布在  其他
关注(0)|答案(2)|浏览(117)

我有一些数据存储在一个列表中,我尝试将下面的代码应用到其中:

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

现在我有了3个列表,我想使用purrr to pmap函数对这些列表执行操作:
我的代码是以下这不工作:

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

我们的想法是应用类似于:

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]

但所有的柱子。
数据:

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))
ar7v8xwq

ar7v8xwq1#

可以使用pmap,名称列表在.l中(如您所做的),但是scaleshape参数在...

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

或者,您可以使用~样式调用bv_psi,如下例所示:

# 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))
vaj7vani

vaj7vani2#

map调用中的另一个选项:

library(purrr)
library(BVAR)

map(df1, function(x){
  min = x / 100 
  max  = x * 100
  
  bv_psi(
    min = min, max = max, mode = x, scale = 0.004, shape = 0.004
  )
})

输出:

$FRA
Shape / Scale: 0.004 / 0.004
#1 Mode / Bounds: 0.3484849 / [0.003484849, 34.84849]
#2 Mode / Bounds: 2.329554 / [0.02329554, 232.9554]
#3 Mode / Bounds: 0.3086792 / [0.003086792, 30.86792]
#4 Mode / Bounds: 0.2425411 / [0.002425411, 24.25411]
#5 Mode / Bounds: 0.3444753 / [0.003444753, 34.44753]

相关问题