1
|
|
#' @title Quantification of (indirect) relations
|
2
|
|
#' @description Function to aggregate positions defined via indirect relations to construct centrality
|
3
|
|
#' scores.
|
4
|
|
#' @param tau_x Numeric matrix containing indirect relations calculated with [indirect_relations].
|
5
|
|
#' @param type String indicating the type of aggregation to be used. See Details for options.
|
6
|
|
#' @details The predefined functions are mainly wrappers around base R functions.
|
7
|
|
#' type='sum', for instance, is equivalent to `rowSums()`. A non-base functions is
|
8
|
|
#' type='invsum' which calculates the inverse of type='sum'.
|
9
|
|
#' type='self' is mostly useful for walk based relations, e.g. to count closed walks.
|
10
|
|
#' Other self explanatory options are type='mean', type='min', type='max' and type='prod'.
|
11
|
|
#' @return Scores for the index defined by the indirect relation `tau_x` and the
|
12
|
|
#' used aggregation type.
|
13
|
|
#' @author David Schoch
|
14
|
|
#' @seealso [indirect_relations], [transform_relations]
|
15
|
|
#' @examples
|
16
|
|
#' library(igraph)
|
17
|
|
#' library(magrittr)
|
18
|
|
#'
|
19
|
|
#' g <- graph.empty(n=11,directed = FALSE)
|
20
|
|
#' g <- add_edges(g,c(1,11,2,4,3,5,3,11,4,8,5,9,5,11,6,7,6,8,
|
21
|
|
#' 6,10,6,11,7,9,7,10,7,11,8,9,8,10,9,10))
|
22
|
|
#'#degree
|
23
|
|
#' g %>% indirect_relations(type='adjacency') %>%
|
24
|
|
#' aggregate_positions(type='sum')
|
25
|
|
#'
|
26
|
|
#' #closeness centrality
|
27
|
|
#' g %>% indirect_relations(type='dist_sp') %>%
|
28
|
|
#' aggregate_positions(type='invsum')
|
29
|
|
#'
|
30
|
|
#' #betweenness centrality
|
31
|
|
#' g %>% indirect_relations(type='depend_sp') %>%
|
32
|
|
#' aggregate_positions(type='sum')
|
33
|
|
#'
|
34
|
|
#' #eigenvector centrality
|
35
|
|
#' g %>% indirect_relations(type='walks',FUN=walks_limit_prop) %>%
|
36
|
|
#' aggregate_positions(type='sum')
|
37
|
|
#'
|
38
|
|
#'#subgraph centrality
|
39
|
|
#' g %>% indirect_relations(type='walks',FUN=walks_exp) %>%
|
40
|
|
#' aggregate_positions(type='self')
|
41
|
|
#'
|
42
|
|
#' @export
|
43
|
|
aggregate_positions <- function(tau_x, type = "sum") {
|
44
|
1
|
if (type == "sum") {
|
45
|
1
|
return(rowSums(tau_x))
|
46
|
1
|
} else if (type == "prod") {
|
47
|
1
|
return(apply(tau_x, 1, prod))
|
48
|
1
|
} else if (type == "mean") {
|
49
|
1
|
return(rowMeans(tau_x))
|
50
|
1
|
} else if (type == "max") {
|
51
|
1
|
return(apply(tau_x, 1, max))
|
52
|
1
|
} else if (type == "min") {
|
53
|
1
|
return(apply(tau_x, 1, min))
|
54
|
1
|
} else if (type == "invsum") {
|
55
|
1
|
return(rowSums(tau_x)^-1)
|
56
|
1
|
} else if (type == "self") {
|
57
|
1
|
diag(tau_x)
|
58
|
|
} else {
|
59
|
1
|
stop(paste(type, " not supported. See function details for options."))
|
60
|
|
}
|
61
|
|
}
|