r语言 - 列表列上的咕噜声 pmap 和几个向量



我正在学习如何使用咕噜声,并认为它对跟踪一些计算很有用。

但是,我不确定为什么我不能使用 purrr::p map 执行涉及以下组件的特定操作:

每个元素长度为 n 的列表长度为 1 的向量长度为 1 的向量长度为 n 的向量1.、2.和 3.都位于同一数据框(名为"operations_df"(中。4. 位于数据帧之外,但是每个列表元素长度相同的向量(长度都相同(。因此,函数调用基本上涉及将每个元素乘以 1 的向量。通过 4. 中的每个元素,然后用 2 和 3 对生成的 1 个元素向量进行加/减。

如果我通过 map2 函数分解事情,这工作正常。但是我想知道如何让它与 pmap 在一行中工作?

library(purrr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
# generate data
data <- rbeta(n = 10, shape1 = 80, shape2 = 80)
prob_k1 <- rbeta(n = 10, shape1 = 80, shape2 = 10)
prob_k2 <- 1-prob_k1

# perform operations on prob_k and data in a data.frame
operations_df <- tibble(components = c('1', '2'),
                        probability = list(prob_k1, prob_k2)) %>%
  # sum over list column
  mutate(n = map_dbl(probability, sum)) %>%
  # mean for each row, using list column and a single 1-element vector
  mutate(mu = map2_dbl(probability, n, ~ (1/.y) * sum(data * .x))) 
operations_df
#> # A tibble: 2 x 4
#>   components probability     n    mu
#>   <chr>      <list>      <dbl> <dbl>
#> 1 1          <dbl [10]>   8.93 0.504
#> 2 2          <dbl [10]>   1.07 0.506
# this doesn't work
# variance for each row, using list column, and two 1-element vectors
operations_df %>%
  mutate(var = pmap_dbl(probability, n, mu, ~ (1/(..2-1)) * sum(..1 * data^2) - ..3^2))
#> Result 1 must be a single double, not NULL of length 0
# this does work
(1/(operations_df$n[1]-1)) * sum(operations_df$probability[[1]] * data^2) - operations_df$mu[1]^2
#> [1] 0.0342961
(1/(operations_df$n[2]-1)) * sum(operations_df$probability[[2]] * data^2) - operations_df$mu[2]^2
#> [1] 3.800814
# breaking it up into two map2 calls works:
operations_df %>%
  mutate(var = map2_dbl(n, probability, ~ (1/(.x-1)) * sum(.y * data^2))) %>%
  mutate(var = map2_dbl(var, mu, ~ .x - .y^2))
#> # A tibble: 2 x 5
#>   components probability     n    mu    var
#>   <chr>      <list>      <dbl> <dbl>  <dbl>
#> 1 1          <dbl [10]>   8.93 0.504 0.0343
#> 2 2          <dbl [10]>   1.07 0.506 3.80

pmap()只接受参数列表,而不是像map()map2()那样一次获取一个参数,因此在运行突变之前,您需要列表中的参数。

library(purrr)
library(dplyr)
set.seed(10)
# generate data
data <- rbeta(n = 10, shape1 = 80, shape2 = 80)
prob_k1 <- rbeta(n = 10, shape1 = 80, shape2 = 10)
prob_k2 <- 1-prob_k1
# perform operations on prob_k and data in a data.frame
operations_df <- tibble(components = c('1', '2'),
                        probability = list(prob_k1, prob_k2)) %>%
    mutate(n = map_dbl(probability, sum)) %>%
    mutate(mu = map2_dbl(probability, n, ~ (1/.y) * sum(data * .x))) 
# pmap only takes a list and not parameters one at a time like map & map2
# see ?pmap for more deetz
operations_df %>%
    mutate(var = pmap_dbl(list(probability, n, mu), ~(1/(..2-1)) * sum(..1 * data^2) - ..3^2))
#> # A tibble: 2 x 5
#>   components probability     n    mu    var
#>   <chr>      <list>      <dbl> <dbl>  <dbl>
#> 1 1          <dbl [10]>   8.77 0.476 0.0303
#> 2 2          <dbl [10]>   1.23 0.479 1.01
# This produces the same output without the complication of thinking about mutate(). 
list(operations_df$probability, 
     operations_df$n, 
     operations_df$mu) %>% 
    pmap_dbl(~(1/(..2-1)) * sum(..1 * data^2) - ..3^2)
#> [1] 0.0303307 1.0087492

创建于 2019-06-06 由 reprex 软件包 (v0.2.1(

最新更新