用purrr弄湿我的脚。
我正在评估β分布的不同参数组合,并尝试传递x值向量和分布参数。
请参阅以下代码:
library(tidyr)
library(purrr)
library(dplyr)
x <- seq(0.01, 0.99, 0.01)
alpha <- c(0.1, 0.5, 1:5, 10)
beta <- c(0.5, 1, 2, 5)
df <- expand.grid(alpha = alpha, beta = beta, x = x) %>%
nest(x) %>%
mutate(plotdata = pmap(list(data$x, alpha, beta), dbeta)) %>%
unnest()
在这种情况下,pmap的正确语法是什么?
答案 0 :(得分:0)
让我们以第一个案例为例。
x = 0.01
alpha = 0.1
beta = 0.5
dbeta(shape1 = alpha, shape2 = beta, x = x)
# [1] 5.600381
第二个例子是:
x = 0.01
alpha = 0.5
beta = 0.5
dbeta(shape1 = alpha, shape2 = beta, x = x)
# [1] 3.199135
如果这是您期望的输出,那么我们可以概括它:
x <- seq(0.01, 0.99, 0.01)
alpha <- c(0.1, 0.5, 1:5, 10)
beta <- c(0.5, 1, 2, 5)
df <- expand.grid(alpha = alpha, beta = beta, x = x)
args_beta <- with(df, list(x = x, shape1 = alpha, shape2 = beta))
args_beta %>%
pmap(dbeta) %>%
unlist() %>%
as.data.frame() %>%
cbind(df, .)
然后输出为:
alpha beta x .
1 0.1 0.5 0.01 5.600381e+00
2 0.5 0.5 0.01 3.199135e+00
3 1.0 0.5 0.01 5.025189e-01
4 2.0 0.5 0.01 7.537784e-03
5 3.0 0.5 0.01 9.422230e-05
6 4.0 0.5 0.01 1.099260e-06