Access and assign (replace) elements from the adjacency matrices or the vertex attributes data frames.
# S3 method for class 'diffnet'
x[[name, as.df = FALSE]]
# S3 method for class 'diffnet'
x[[i, j]] <- value
# S3 method for class 'diffnet'
x[i, j, k, drop = FALSE]
# S3 method for class 'diffnet'
x[i, j, k] <- value
A diffnet class object.
String vector. Names of the vertices attributes.
Logical scalar. When TRUE
returns a data frame, otherwise
a list of length \(T\).
Index of the i-th row of the adjacency matrix (see details).
Index of the j-th column of the adjacency matrix (see details)
Value to assign (see details)
Index of the k-th slice of the adjacency matrix (see details).
Logical scalar. When TRUE
returns an adjacency matrix, otherwise
a filtered diffnet object.
In the case of the assigning methods, a diffnet object. Otherwise,
for [[.diffnet
a vector extracted from one of the attributes data frames,
and for [.diffnet
a list of length length(k)
with the corresponding
[i,j]
elements from the adjacency matrix.
The [[.diffnet
methods provides access to the diffnet attributes
data frames, static and dynamic. By providing the name
of the corresponding
attribute, depending on whether it is static or dynamic the function will return
either a data frame–static attributes–or a list of these–dynamic attributes.
For the assigning method, [[<-.diffnet
, the function will infer what
kind of attribute is by analyzing the dimensions of value
, in particular
we have the following possible cases:
Class | Dimension | Inferred |
matrix | \(n\times T\) | Dynamic |
matrix | \(n\times 1\) | Static |
matrix | \((n\times T)\times 1\) | Dynamic |
data.frame | \(n\times T\) | Dynamic |
data.frame | \(n\times 1\) | Static |
data.frame | \((n\times T)\times 1\) | Dynamic |
vector | \(n\) | Static |
vector | \(n\times T\) | Dynamic |
list * | \(T\) data.frames/matrices/vectors | Dynamic |
*: With \(n\times 1\) data.frame
/matrix
or \(n\) length vector.
Other cases will return with error.
In the case of the slices index k
, either an
integer vector with the positions, a character vector with the labels of the
time periods or a logical vector of length T
can be used to specify
which slices to retrieve. Likewise, indexing vertices works in the same way
with the only difference that, instead of time period labels and a logical vector
of length T
, vertices ids labels and a logical vector of length n
should be provided.
When subsetting slices, the function 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 k=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, k
have no
gaps, and it should be within the graph time period range.
Other diffnet methods:
%*%()
,
as.array.diffnet()
,
c.diffnet()
,
diffnet-arithmetic
,
diffnet-class
,
plot.diffnet()
,
summary.diffnet()
# Creating a random diffusion network ---------------------------------------
set.seed(111)
graph <- rdiffnet(50,4)
#> Warning: The option -copy.first- is set to TRUE. In this case, the first graph will be treated as a baseline, and thus, networks after T=1 will be replaced with T-1.
# Accessing to a static attribute
graph[["real_threshold"]]
#> [1] 0.666962209 0.391814531 0.272925325 0.288155083 0.749986316 0.518509553
#> [7] 0.737585892 0.564430301 0.269947958 0.874264158 0.660889278 0.238031827
#> [13] 0.809849920 0.235137960 0.508440020 0.921349213 0.964881821 0.128340925
#> [19] 0.325600014 0.805541834 0.450212701 0.362721951 0.778399902 0.808176714
#> [25] 0.463539605 0.480844416 0.030444142 0.789020953 0.253293309 0.614091277
#> [31] 0.580593804 0.932828544 0.004737207 0.655205778 0.462660256 0.633963026
#> [37] 0.334573158 0.892599212 0.671109634 0.858136740 0.117454546 0.258317890
#> [43] 0.734482044 0.632933825 0.641191932 0.356005288 0.318120651 0.137058817
#> [49] 0.827643429 0.296913551
# Accessing to subsets of the adjacency matrix
graph[1,,1:3, drop=TRUE]
#> $`1`
#> 1 x 50 sparse Matrix of class "dgCMatrix"
#>
#> [1,] 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#>
#> [1,] . . . . . . . . . . . . .
#>
#> $`2`
#> 1 x 50 sparse Matrix of class "dgCMatrix"
#>
#> [1,] 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#>
#> [1,] . . . . . . . . . . . . .
#>
#> $`3`
#> 1 x 50 sparse Matrix of class "dgCMatrix"
#>
#> [1,] 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#>
#> [1,] . . . . . . . . . . . . .
#>
graph[,,1:3, drop=TRUE][[1]]
#> 50 x 50 sparse Matrix of class "dgCMatrix"
#>
#> [1,] 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [2,] 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [3,] . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [4,] 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [5,] . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [6,] . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [7,] . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . .
#> [8,] . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . .
#> [9,] . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . .
#> [10,] . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [11,] 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [12,] . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [13,] . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . .
#> [14,] 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [15,] 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [16,] 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [17,] . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [18,] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [19,] . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . .
#> [20,] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . .
#> [21,] . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [22,] . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [23,] . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [24,] . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [25,] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [26,] . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . .
#> [27,] . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [28,] . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [29,] . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [30,] . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . .
#> [31,] . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [32,] . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . .
#> [33,] . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [34,] . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [35,] . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [36,] . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [37,] . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . .
#> [38,] 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [39,] . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . .
#> [40,] . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . .
#> [41,] . . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . .
#> [42,] . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . .
#> [43,] . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [44,] . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . .
#> [45,] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [46,] . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . .
#> [47,] . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [48,] . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> [49,] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . .
#> [50,] . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . .
#>
#> [1,] . . . . . . . . . . . . .
#> [2,] . . . . . . . . . . . . .
#> [3,] . . . . . . . . . . . . .
#> [4,] . . . . . . . . . . . . .
#> [5,] . . . . . . . . . . . . .
#> [6,] . . . . . . . . . . . . .
#> [7,] . . . . . . . . . . . . .
#> [8,] . . . . . . . . . . . . .
#> [9,] . . . . . . . . . . . . .
#> [10,] . . . . . . . . . . . . .
#> [11,] . . . . . . . . . . . . .
#> [12,] . . . . . . . . . . . . .
#> [13,] . . . . . . . . . . . . .
#> [14,] . . . . . . . . . . . . .
#> [15,] . . . . . . . . . . . . .
#> [16,] . . . . . . . . . . . . .
#> [17,] . . . . . . . . . . . . .
#> [18,] . . . 1 . . . . . . . . .
#> [19,] . . . . . . . . . . . . .
#> [20,] . . . . . . . . . . . . .
#> [21,] . . . . . . . . . . . . .
#> [22,] . . . . . . . . . . . . .
#> [23,] . . . . . . . . . . . . .
#> [24,] . . . . . . . . . . . . .
#> [25,] . . 1 . . . . . . . . . .
#> [26,] . . . . . . . . . . . . .
#> [27,] . . . . . . . . . . . . .
#> [28,] . . . . . . . . . . . . .
#> [29,] . . . . . . . . . . . . .
#> [30,] . . . . . . . . . . . . .
#> [31,] . . . . . . . . . . . . .
#> [32,] . . . . . . . . . . . . .
#> [33,] . . . . . . . . . . . . .
#> [34,] . . . . . . . . . . . . .
#> [35,] . . . . . . . . . . . . .
#> [36,] . . . . . . . . . . . . .
#> [37,] . . . . . . . . . . . . .
#> [38,] . . . . . . . . . . . . .
#> [39,] . . . . . . . . . . . . .
#> [40,] . . . . . . . . . . . . .
#> [41,] . . . . . . . . . . . . .
#> [42,] . . . . . . . . . . . . .
#> [43,] . . . . . . . . . . . . .
#> [44,] . . . . . . . . . . . . .
#> [45,] . 1 . . . . . . . . . . .
#> [46,] . . . . . . . . . . . . .
#> [47,] . . . . . . . . . . . . .
#> [48,] . . . . . . . . . . . . .
#> [49,] . . . . . . . . . . . . .
#> [50,] . . . . . . . . . . . . .
# ... Now, as diffnet objects (the default)
graph[1,,1:3, drop=FALSE]
#> Dynamic network of class -diffnet-
#> Name : A diffusion network
#> Behavior : Random contagion
#> # of nodes : 1 (1)
#> # of time periods : 3 (1 - 3)
#> Type : directed
#> Final prevalence : 0.00
#> Static attributes : real_threshold (1)
#> Dynamic attributes : -
graph[,,1:3, drop=FALSE]
#> Dynamic network of class -diffnet-
#> Name : A diffusion network
#> Behavior : Random contagion
#> # of nodes : 50 (1, 2, 3, 4, 5, 6, 7, 8, ...)
#> # of time periods : 3 (1 - 3)
#> Type : directed
#> Final prevalence : 0.06
#> Static attributes : real_threshold (1)
#> Dynamic attributes : -
# Changing values in the adjacency matrix
graph[1, , , drop=TRUE]
#> $`1`
#> 1 x 50 sparse Matrix of class "dgCMatrix"
#>
#> [1,] 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#>
#> [1,] . . . . . . . . . . . . .
#>
#> $`2`
#> 1 x 50 sparse Matrix of class "dgCMatrix"
#>
#> [1,] 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#>
#> [1,] . . . . . . . . . . . . .
#>
#> $`3`
#> 1 x 50 sparse Matrix of class "dgCMatrix"
#>
#> [1,] 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#>
#> [1,] . . . . . . . . . . . . .
#>
#> $`4`
#> 1 x 50 sparse Matrix of class "dgCMatrix"
#>
#> [1,] 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#>
#> [1,] . . . . . . . . . . . . .
#>
graph[1,,] <- -5
graph[1, , , drop=TRUE]
#> $`1`
#> 1 x 50 sparse Matrix of class "dgCMatrix"
#>
#> [1,] -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5
#>
#> [1,] -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5
#>
#> $`2`
#> 1 x 50 sparse Matrix of class "dgCMatrix"
#>
#> [1,] -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5
#>
#> [1,] -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5
#>
#> $`3`
#> 1 x 50 sparse Matrix of class "dgCMatrix"
#>
#> [1,] -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5
#>
#> [1,] -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5
#>
#> $`4`
#> 1 x 50 sparse Matrix of class "dgCMatrix"
#>
#> [1,] -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5
#>
#> [1,] -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5
#>
# Adding attributes (dynamic) -----------------------------------------------
# Preparing the data
set.seed(1122)
x <- rdiffnet(30, 4, seed.p.adopt=.15)
#> Warning: The option -copy.first- is set to TRUE. In this case, the first graph will be treated as a baseline, and thus, networks after T=1 will be replaced with T-1.
# Calculating exposure, and storing it diffe
expoM <- exposure(x)
expoL <- lapply(seq_len(x$meta$nper), function(x) expoM[,x,drop=FALSE])
expoD <- do.call(rbind, expoL)
# Adding data (all these are equivalent)
x[["expoM"]] <- expoM
x[["expoL"]] <- expoL
x[["expoD"]] <- expoD
# Lets compare
identical(x[["expoM"]], x[["expoL"]]) # TRUE
#> [1] TRUE
identical(x[["expoM"]], x[["expoD"]]) # TRUE
#> [1] TRUE