combineCosts: Combine the costs of two gGraph objects

View source: R/combineCosts.R

combineCostsR Documentation

Combine the costs of two gGraph objects

Description

The function combineCosts combines the edge costs of two gGraph objects. The first object is used as a temlate to generate the objects with the combined costs. Two two gGraph objects must have the same edges.

Usage

combineCosts(x1, x2, method = c("sum", "product", "function"), FUN = NULL, ...)

Arguments

x1

The firt gGraph (which will be used as a template to build the combined gGraph)

x2

The second gGraph from which costs will be combined

method

a character string indicating which method should be used to combined edge cost from the two gGraph. Currently available options are 'sum', 'prod' and 'function', where the combined costs are computed as the sum, the product or a custom function (defined in FUN) of the costs of its nodes.

FUN

a function used to compute the cost between two nodes (needed if method="function").

...

additional parameters to be passed to FUN.

Details

Note that costs are inversely proportional to connectivity between edges: the larger the cost associated to an edge, the lower the connectivity between the two concerned nodes.

Also note that 'costs' defined in geoGraph are equivalent to 'weights' as defined in graph and RBGL packages.

Value

A gGraph object with the newly defined costs, basedd on the combination of the two gGraph objects, used as weightings of edges.

Examples

data("worldgraph.40k")
# new graph with custom cost function
exp.cost <- function(x1, x2, cost.coeff) {
  exp(-abs(x1 - x2) * cost.coeff)
}
# create a set of node costs
worldgraph.40k@nodes.attr$meanProd <- runif(n = 40962)
new_costs_graph <-
  setCosts(
    worldgraph.40k,
    node.values = worldgraph.40k@nodes.attr$meanProd,
    method = "function",
    FUN = exp.cost,
    cost.coeff = 0.5
  )
# combine costs from the original graph with the new costs
combine_costs_graph <- combineCosts(worldgraph.40k, new_costs_graph, method = "sum")

thibautjombart/geograph documentation built on Jan. 27, 2024, 10 p.m.