Learn R Programming

netdiffuseR (version 1.16.2)

as_diffnet: Creates a diffnet class object

Description

diffnet objects contain difussion of innovation networks. With adjacency matrices and time of adoption (toa) vector as its main components, most of the package's functions have methods for this class of objects.

Usage

as_diffnet(graph, toa, t0 = min(toa, na.rm = TRUE), t1 = max(toa, na.rm =
  TRUE), vertex.dyn.attrs = NULL, vertex.static.attrs = NULL,
  graph.attrs = NULL, undirected = getOption("diffnet.undirected"),
  self = getOption("diffnet.self"),
  multiple = getOption("diffnet.multiple"))

diffnet.subset.slices(graph, slices)

diffnet.attrs(graph, element = c("vertex", "graph"), attr.class = c("dyn", "static"), as.df = FALSE)

diffnet.attrs(graph, element = "vertex", attr.class = "static") <- value

diffnet.toa(graph)

diffnet.toa(graph, i) <- value

## S3 method for class 'diffnet': plot(x, y = NULL, t = 1, displaylabels = FALSE, vertex.col = c("blue", "grey"), gmode = ifelse(x$meta$undirected, "graph", "digraph"), vertex.cex = "degree", edge.col = "gray", mode = "fruchtermanreingold", layout.par = NULL, main = "Diffusion network in time %d", ...)

## S3 method for class 'diffnet': print(x, ...)

## S3 method for class 'diffnet': summary(object, slices = NULL, no.print = FALSE, skip.moran = FALSE, valued = getOption("diffnet.valued", FALSE), ...)

## S3 method for class 'diffnet': [(x, i, j, drop = FALSE)

Arguments

graph
A dynamic graph (see netdiffuseR-graphs).
toa
Numeric vector of size $n$. Times of adoption.
t0
Integer scalar. Passed to toa_mat.
t1
Integer scalar. Passed to toa_mat.
vertex.dyn.attrs
List of length $T$. Contains matrices with vertex attributes.
vertex.static.attrs
Numeric matrix with $n$ rows.
graph.attrs
Numeric matrix with a single row.
undirected
Logical scalar.
self
Logical scalar.
multiple
Logical scalar.
slices
Integer vector.
element
Character vector/scalar. Indicates what to retrieve/alter.
attr.class
Character vector/scalar. Indicates the class of the attribute, either dynamic ("dyn"), or static ("static").
as.df
Logical scalar. When TRUE returns a data.frame.
value
In the case of diffnet.toa, replacement, otherwise see below.
i
Indices specifying elements to replace. See Extract.
x
A diffnet object.
y
Ignored.
t
Integer scalar indicating the time slice to plot.
displaylabels
Logical scalar. When TRUE, plot shows vertex labels.
vertex.col
Character scalar/vector. Color of the vertices.
gmode
Character scalar. Passed to gplot.
vertex.cex
Numeric scalar/vector. Size of the vertices.
edge.col
Character scalar/vector. Color of the edges.
mode
Character scalar. Name of the layout algorithm to implement (see details).
layout.par
Layout parameters (see details).
main
Character. A title template to be passed to sprintf.
...
In the case of plot, further arguments passed to gplot, otherwise is ignored.
object
A diffnet object.
no.print
Logical scalar. When TRUE suppress screen messages.
skip.moran
Logical scalar. When TRUE Moran's I is not reported (see details).
valued
Logical scalar. When FALSE non-zero values in the adjmat are set to one.
j
Ignored.
drop
Ignored

Value

  • A list of class diffnet with the following elements:
  • graphA list of length $T$. Containing sparse square matrices of size $n$ and class dgCMatrix.
  • toaAn integer vector of size $T$ with times of adoption.
  • adopt, cumadoptNumeric matrices of size $n\times T$ as those returned by toa_mat.
  • vertex.static.attrsIf not NULL, a matrix with $n$ rows with vertex static attributes.
  • vertex.dyn.attrsA list of length $T$ with matrices containing vertex attributes throught time (dynamic).
  • graph.attrsIf not NULL, a numeric matrix with 1 row containing graph attributes.
  • metaA list of length 9 with the following elements:
    • type: Character scalar equal to"dynamic".
    • class: Character scalar equal to"list".
    • ids: Character vector of size$n$with vertices' labels.
    • pers: Integer vector of size$T$.
    • nper: Integer scalar equal to$T$.
    • n: Integer scalar equal to$n$.
    • self: Logical scalar.
    • undirected: Logical scalar.
    • multiple: Logical scalar.

Auxiliary functions

diffnet.subset.slices retrieves a subsection, in terms of time periods, of the graph. When doing so, it modifies the toa vector as well as the adopt and cumadopt matrices collapsing network tinmming. For example, if a network goes from time 1 to 20 and we set slices=3:10, all individuals who adopted prior to time 3 will be set as adopters at time 3, and all individuals who adopted after time 10 will be set as adopters at time 10, changing the adoption and cumulative adoption matrices. Importantly, slice must be an integer range without gaps.

diffnet.attrs Allows retriving network attributes. In particular, by default returns a list of length $T$ with matrices with the following columns:

  1. perIndicating the time period to which the observation corresponds.
  2. toaIndicating the time of adoption of the vertex.
  3. Further columns depending on the vertex and graph attributes.

Each vertex static attributes' are repeated $T$ times in total so that these can be binded (rbind) to dynamic attributes.

When as.df=TRUE, this convenience function is useful as it can be used to create event history (panel data) datasets used for model fitting.

Conversely, the replacement method allows including new vertex or graph attributes either dynamic or static (see examples below).

diffnet.toa(graph) works as an alias of graph$toa. The replacement method, diffnet.toa<- used as diffnet.toa(graph)<-..., is the right way of modifying times of adoption as when doing so it performs several checks on the time ranges, and recalculates adoption and cumulative adoption matrices using toa_mat.

Details

Plotting is done via the function gplot, and its layout via gplot.layout, both from the (sna) package.

vertex.cex can either be a numeric scalar, a numeric vector or a character scalar taking any of the following values "degree", "indegree", or "outdegree". The later will be passed to dgr to calculate degree of the selected slice and will be normalized as

$$vertex.cex = d/[max(d) - min(d)]\times 2 + .5$$

where d=sqrt(dgr(graph)).

In the case of the summary method, Moran's I is calculated over the cumulative adoption matrix using as weighting matrix the inverse of the geodesic distance matrix. All this via moran. For each time period t, this is calculated as:

m = moran(C[,t], G^(-1))

Where C[,t] is the t-th column of the cumulative adoption matrix, G^(-1) is the element-wise inverse of the geodesic matrix at time t, and moran is netdiffuseR's moran's I routine. When skip.moran=TRUE Moran's I is not reported. This can be useful when the graph is particuarly large (tens of thousands of vertices) as when doing so geodesic distances are not calculated, which avoids allocating a square matrix of size $n$ on the memory. As a difference from the adjacency matrices, the matrix with the geodesic distances can't be stored as a sparse matrix (saving space).

See Also

Default options are listed at netdiffuseR-options

Examples

Run this code
# Creating a random graph
set.seed(123)
graph <- rgraph_ba(t=9)
graph <- lapply(1:5, function(x) graph)

# Pretty TOA
names(graph) <- 2001L:2005L
toa <- sample(c(2001L:2005L,NA), 10, TRUE)

# Creating diffnet object
diffnet <- as_diffnet(graph, toa)
diffnet
summary(diffnet)

# ATTRIBUTES ----------------------------------------------------------------

# Adding new attributes to the network
diffnet.attrs(diffnet, "vertex", "static") <- cbind(posnum=1:diffnet$meta$n)
diffnet.attrs(diffnet, "vertex", "dyn") <-
 lapply(1:diffnet$meta$nper, function(x) cbind(rand=runif(diffnet$meta$n)))

# Retrieving attributes
diffnet.attrs(diffnet, "vertex", "static")

# Now as a data.frame (only static)
diffnet.attrs(diffnet, "vertex", "static", as.df = TRUE)

# Now as a data.frame (all of them)
diffnet.attrs(diffnet, as.df = TRUE)

Run the code above in your browser using DataLab