Tidygraph:在父级别计算子摘要

时间:2018-06-13 15:18:32

标签: r tidygraph

使用R中的tidygraph包,给定一个树,我想计算树中每个节点的每个直接子节点的值的平均值,总和,方差....

我的直觉是使用map_bfs_back_dbl或相关的,并尝试修改帮助示例,但卡住了

library(tidygraph)

# Collect values from children
create_tree(40, children = 3, directed = TRUE) %>%
  mutate(value = round(runif(40)*100)) %>%
  mutate(child_acc = map_bfs_back_dbl(node_is_root(), .f = function(node, path, ...) {
    if (nrow(path) == 0) .N()$value[node]
    else {
      sum(unlist(path$result[path$parent == node]))
    }
  }))

对于上述内容,我希望树中每个父母的所有直接,第一级孩子的平均value

更新:: 我尝试过这种方法(计算子属性的方差):

library(tidygraph)
create_tree(40, children = 3, directed = TRUE) %>%
  mutate(parent = bfs_parent(),
         value = round(runif(40)*100)) %>% 
  group_by(parent) %>%
  mutate(var = var(value))

哪个关闭:

# Node Data: 40 x 3 (active)
# Groups:    parent [14]
  parent value   var
*  <int> <dbl> <dbl>
1     NA  2.00    NA
2      1 13.0   1393
3      1 63.0   1393
4      1 86.0   1393
5      2 27.0    890
6      2 76.0    890
# ... with 34 more rows

我希望看到的是:

# Node Data: 40 x 3 (active)
# Groups:    parent [14]
  parent value   var  child_var
*  <int> <dbl> <dbl>      <dbl>
1     NA  2.00    NA       1393
2      1 13.0   1393        890 
3      1 63.0   1393       (etc)
4      1 86.0   1393
5      2 27.0    890
6      2 76.0    890
# ... with 34 more rows

移动(第一个)&#34; var&#34;值最多由&#34; parent&#34;标识的节点值。救命?建议?

编辑: 这就是我最后做的事情:

tree <- create_tree(40, children = 3, directed = TRUE) %>%
  mutate(parent = bfs_parent(),
         value = round(runif(40) * 100),
         name = row_number()) %>%
  activate(nodes) %>%
  left_join(
    tree %>%
      group_by(parent) %>%
      mutate(var = var(value)) %>% activate(nodes) %>% as_tibble() %>%
      group_by(parent) %>% summarize(child_stat = first(var)),
    by=c("name" = "parent")
  )

感觉不是非常整洁,但似乎有效。开放优化。

1 个答案:

答案 0 :(得分:0)

我刺了一个&#34; tidygraph&#34;在这里做事的方式。主要功能是用于计算value列的方差:

calc_child_stats <- function(neighborhood, ...){
  ## By default the neighborhood includes the parent and all of it's children
  ## First remove the parent, then run analysis
  neighborhood %>% activate(nodes) %>% 
    slice(-1) %>% 
    select(value) %>% 
    pull %>% 
    var
}

一旦你拥有了这个功能,那么只需拨打map_local而不是map_bfs的简单电话:

tree <- create_tree(40, children = 3, directed = TRUE) %>%
  mutate(value = round(runif(40)*100))

tree %>% mutate(var = map_local_dbl(order = 1, mode="out", .f = calc_child_stats))
#> # A tbl_graph: 40 nodes and 39 edges
#> #
#> # A rooted tree
#> #
#> # Node Data: 40 x 2 (active)
#>   value   var
#>   <dbl> <dbl>
#> 1    29  34.3
#> 2    45 433  
#> 3    56 225. 
#> 4    47 868  
#> 5    78 604. 
#> 6    43 283  
#> # ... with 34 more rows
#> #
#> # Edge Data: 39 x 2
#>    from    to
#>   <int> <int>
#> 1     1     2
#> 2     1     3
#> 3     1     4
#> # ... with 36 more rows

虽然我的tidygraph版本更多&#34; graphy&#34;它看起来不是很快,所以我在两种方法之间创建了一个快速的微基准测试:

library(microbenchmark)
microbenchmark(tree %>% mutate(var = map_local_dbl(order = 1, mode="out", .f = calc_child_stats)))
#> Unit: milliseconds
#>                                                                                       expr
#>  tree %>% mutate(var = map_local_dbl(order = 1, mode = "out",      .f = calc_child_stats))
#>       min       lq     mean   median      uq      max neval
#>  115.3325 123.0303 127.7889 126.6683 130.057 191.6065   100
microbenchmark(calc_child_stats_dplyr(tree))
#> Unit: milliseconds
#>                          expr      min       lq     mean   median       uq
#>  calc_child_stats_dplyr(tree) 4.915917 5.213939 6.292579 5.573978 6.717745
#>       max neval
#>  16.72846   100

reprex package(v0.2.0)创建于2018-06-15。

果然,dplyr方式更快,所以我现在坚持这样做。他们在我的测试中给出了相同的值。

为了完整性,这是我使用复制op方法的fxn:

calc_child_stats_dplyr <- function(tree){
  tree <- tree %>%
    mutate(parent = bfs_parent(),
           name = row_number())

  tree %>% activate(nodes) %>%
    left_join(
      tree %>%
        group_by(parent) %>%
        mutate(var = var(value)) %>% 
        activate(nodes) %>% 
        as_tibble() %>%
        group_by(parent) %>% 
        summarize(child_stat = first(var)),
      by=c("name" = "parent")
    )
}