Learn R Programming

netdiffuseR (version 1.16.7)

diffusionMap: Creates a heatmap based on a graph layout and times of adoption

Description

Basically creates a smooth-scatter plot in which each observation is weighted by x.

Usage

diffusionMap(graph, ...)
diffmap(graph, ...)
"diffusionMap"(graph, x, x.adj = round_to_seq, layout = NULL, jitter.args = list(), kde2d.args = list(n = 100), ...)
"diffusionMap"(graph, slice = nslices(graph), ...)
"image"(x, ...)
"print"(x, ...)
"plot"(x, y = NULL, ...)

Arguments

graph
A square matrix of size $n * n$.
...
Arguments passed to method.
x
An vector of length $n$. Usually a toa vector.
x.adj
Function to adjust x. If not NULL then it is applied to x at the beginning (see details).
layout
Either a $n *2$ matrix of coordinates or a layout function applied to graph (must return coordinates).
jitter.args
A list including arguments to be passed to jitter.
kde2d.args
A list including arguments to be passed to kde2d.
slice
Integer scalar. Slice of the network to be used as baseline for drawing the graph.
y
Ignored.

Value

A list of class diffnet_diffmap
coords
A matrix of size $n*2$ of vertices coordinates.
map
Output from kde2d. This is a list with 3 elements, vectors x, y and matrix z of size $n*n$ (passed via kde2d.args).
h
Bandwidth passed to kde2d.

Details

The image is created using the function kde2d from the MASS package. The complete algorithm follows:
  1. x is coerced into integer and the range is adjusted to start from 1. NA are replaced by zero.
  2. If no layout is passed, layout is computed using layout_nicely from igraph
  3. Then, a kde2d map is computed for each level of x. The resulting matrices are added up as a weighted sum
  4. The jitter function is applied to the repeated coordinates.
  5. 2D kernel is computed using kde2d over the coordinates.

The resulting matrix can be passed to image or similar.

The argument x.adj uses by default the function round_to_seq which basically maps x to a fix length sequence of numbers such that x.adj(x) resembles an integer sequence.

See Also

Other visualizations: drawColorKey, hazard_rate, plot_adopters, plot_diffnet2, plot_diffnet, plot_infectsuscep, plot_threshold, rescale_vertex_igraph

Examples

Run this code

# Example with a random graph --------------------------------------------------

set.seed(1231)

# Random small-world diffusion network
x <- netdiffuseR::rdiffnet(300, 20, seed.graph="small-world",
 seed.nodes = "random")

# Diffusion map (no random toa)
dm0 <- diffusionMap(x, kde2d.args=list(n=100, h=4))

# Random
diffnet.toa(x) <- (x$toa)[order(runif(300))]

# Diffusion map (random toa)
dm1 <- diffusionMap(x, layout = dm0$coords, kde2d.args=list(n=100, h=4))

oldpar <- par(no.readonly = TRUE)
col <- adjustcolor(
 colorRampPalette(c("white","lightblue", "yellow", "red"))(100),.5)
par(mfrow=c(1,2), oma=c(1,0,0,0))
image(dm0, col=col, main="Non-random Times of Adoption")
image(dm1, col=col, main="Random Times of Adoption")
par(mfrow=c(1,1))
mtext("Both networks have the same distribution on times of adoption", 1,
 outer = TRUE)
par(oldpar)

# Example with Brazilian Farmers --------------------------------------------

dn <- brfarmersDiffNet

# Setting last TOA as NA
diffnet.toa(dn)[dn$toa == max(dn$toa)] <-
  NA

# Coordinates
coords <- sna::gplot.layout.fruchtermanreingold(
  as.matrix(dn$graph[[1]]), layout.par=NULL
)

# Plotting diffusion
plot_diffnet2(dn, layout=coords, vertex.size = 300)

# Adding diffusion map
out <- diffusionMap(dn, layout=coords, kde2d.args=list(n=100, h=50))
col <- adjustcolor(colorRampPalette(c("white","lightblue", "yellow", "red"))(100),.5)
with(out$map, .filled.contour(x,y,z,pretty(range(z), 100),col))

Run the code above in your browser using DataLab