Creates a diffnet class object

Description

diffnet objects contain difussion 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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
as_diffnet(graph, toa, t0 = min(toa, na.rm = TRUE), t1 = max(toa, na.rm =
  TRUE), vertex.dyn.attrs = NULL, vertex.static.attrs = NULL,
  id.and.per.vars = NULL, graph.attrs = NULL,
  undirected = getOption("diffnet.undirected"),
  self = getOption("diffnet.self"),
  multiple = getOption("diffnet.multiple"), name = "Diffusion Network",
  behavior = "Unspecified")

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),
  mode = "out", ...)

nodes(graph)

diffnetLapply(graph, FUN, ...)

## S3 method for class 'diffnet'
str(object, ...)

## S3 method for class 'diffnet'
dimnames(x)

## S3 method for class 'diffnet'
t(x)

## S3 method for class 'diffnet'
dim(x)

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

Vertices dynamic attributes (see details).

vertex.static.attrs

Vertices static attributes (see details).

id.and.per.vars

A character vector of length 2. Optionally specified to check the order of the rows in the attribute data.

graph.attrs

Graph dynamic attributes (not supported yet).

undirected

Logical scalar. When TRUE only the lower triangle will be processed.

self

Logical scalar. When TRUE allows loops (self edges).

multiple

Logical scalar. When TRUE allows multiple edges.

name

Character scalar. Name of the diffusion network (descriptive).

behavior

Character scalar. Name of the behavior been analyzed (innovation).

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 sna::gplot.

vertex.cex

Numeric scalar/vector. Size of the vertices.

edge.col

Character scalar/vector. Color of the edges.

mode

Character scalar. In the case of plot, passed to gplot. Otherwise, in the case of summary, passed to igraph::distances.

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, for summary, further arguments to be passed to igraph::distances, otherwise is ignored.

object

A diffnet object.

slices

Either an integer or character vector. While integer vectors are used as indexes, character vectors are used jointly with the time period labels.

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 TRUE weights will be considered. Otherwise non-zero values will be replaced by ones.

FUN

a function to be passed to lapply

Details

diffnet objects hold both, static and dynamic vertex attributes. When creating diffnet objects, these can be specified using the arguments vertex.static.attrs and vertex.dyn.attrs; depending on whether the attributes to specify are static or dynamic, netdiffuseR currently supports the following objects:

Class Dimension Check sorting
Static attributes
matrix with n rows id
data.frame with n rows id
vector of length n -
Dynamic attributes
matrix with n * T rows id, per
data.frame with n * T rows id, per
vector of length n*T -
list of length T with matrices or data.frames of n rows id, per

The last column, Check sorting, lists the variables that the user should specify if he wants the function to check the order of the rows of the attributes (notice that this is not possible for the case of vectors). By providing the name of the vertex id variable, id, and the time period id variable, per, the function makes sure that the attribute data is presented in the right order. See the example below. If the user does not provide the names of the vertex id and time period variables then the function does not check the way the rows are sorted, further it assumes that the data is in the correct order.

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)]* 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:

1
 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).

Value

A list of class diffnet with the following elements:

graph

A list of length T. Containing sparse square matrices of size n and class dgCMatrix.

toa

An integer vector of size T with times of adoption.

adopt, cumadopt

Numeric matrices of size n*T as those returned by toa_mat.

vertex.static.attrs

If not NULL, a data frame with n rows with vertex static attributes.

vertex.dyn.attrs

A list of length T with data frames containing vertex attributes throught time (dynamic).

graph.attrs

A data frame with T rows.

meta

A 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.

  • name: Character scalar.

  • behavior: Character scalar.

Auxiliary functions

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

  1. per Indicating the time period to which the observation corresponds.

  2. toa Indicating 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.

nodes(graph) is an alias for graph$meta$ids.

Author(s)

George G. Vega Yon

See Also

Default options are listed at netdiffuseR-options

Other data management functions: edgelist_to_adjmat, egonet_attrs, isolated, survey_to_diffnet

Other diffnet methods: %*%, as.array.diffnet, c.diffnet, diffnet-arithmetic, diffnet_index

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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)

# Plotting slice 4
plot(diffnet, t=4)

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

# 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)

# Unsorted data -------------------------------------------------------------
# Loading example data
data(fakesurveyDyn)

# Creating a diffnet object
fs_diffnet <- survey_to_diffnet(
   fakesurveyDyn, "id", c("net1", "net2", "net3"), "toa", "group",
   timevar = "time", keep.isolates=TRUE, warn.coercion=FALSE)

# Now, we extract the graph data and create a diffnet object from scratch
graph <- fs_diffnet$graph
ids <- fs_diffnet$meta$ids
graph <- Map(function(g) {
  dimnames(g) <- list(ids,ids)
  g
  }, g=graph)
attrs <- diffnet.attrs(fs_diffnet, as.df=TRUE)
toa   <- diffnet.toa(fs_diffnet)

# Lets apply a different sorting to the data to see if it works
n <- nrow(attrs)
attrs <- attrs[order(runif(n)),]

# Now, recreating the old diffnet object (notice -id.and.per.vars- arg)
fs_diffnet_new <- as_diffnet(graph, toa=toa, vertex.dyn.attrs=attrs,
   id.and.per.vars = c("id", "per"))

# Now, retrieving attributes. The 'new one' will have more (repeated)
attrs_new <- diffnet.attrs(fs_diffnet_new, as.df=TRUE)
attrs_old <- diffnet.attrs(fs_diffnet, as.df=TRUE)

# Comparing elements!
tocompare <- intersect(colnames(attrs_new), colnames(attrs_old))
all(attrs_new[,tocompare] == attrs_old[,tocompare], na.rm = TRUE) # TRUE!

# diffnetLapply -------------------------------------------------------------

data(medInnovationsDiffNet)
diffnetLapply(medInnovationsDiffNet, function(x, cumadopt, ...) {sum(cumadopt)})

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.