For a given set of vertices V, retrieves each vertex's alter's attributes. This function enables users to calculate exposure on variables other than the attribute that is diffusing. Further, it enables the specification of alternative functions to use to characterize ego's personal network including calculating the mean, maximum, minimum, median, or sum of the alters' attributes. These measures may be static or dynamic over the interval of diffusion and they may be binary or valued.
Any class of accepted graph format (see netdiffuseR-graphs
).
If graph
is static, Numeric matrix with \(n\) rows, otherwise a list of numeric matrices with \(n\) rows.
Integer vector. Set of vertices from which the attributes will be retrieved.
Character scalar. Either "outgoing"
, "incoming"
.
Function. Applied to each
Logical scalar. When TRUE returns a data.frame instead of a list (see details).
Logical scalar. When TRUE
autolinks (loops, self edges) are allowed (see details).
Logical scalar. When TRUE
weights will be considered. Otherwise non-zero values will be replaced by ones.
Further arguments to be passed to fun
.
A list with ego alters's attributes. By default, if the graph is static, the
output is a list of length length(V)
with matrices having the following
columns:
Either the corresponding value of the tie.
Alter's id
Further attributes contained in attrs
On the other hand, if graph
is dynamic, the output is list of length
\(T\) of lists of length length(V)
with data frames having the following
columns:
The corresponding value of the adjacency matrix.
Alter's id
Time id
Further attributes contained in attrs
By indexing inner/outer edges, this function retrieves ego network attributes for all \(v \in V\), which by default is the complete set of vertices in the graph.
When as.df=TRUE
the function returns a data.frame of size
\((|V|\times T)\times k\) where \(T\) is the number of time
periods and \(k\) is the number of columns generated by the function.
The function can be used to create network effects as those in the RSiena package. The difference here is that the definition of the statistic directly relies on the user. For example, in the RSiena package, the dyadic covariate effect 37. covariate (centered) main effect (X)
$$% s_{i37}(x) = \sum_j x_{ij}(w_{ij}-\bar w) $$
Which, having a diffnet object with attributes named x
and w
,
can be calculated as
egonet_attrs(diffnet, as.df=TRUE, fun=function(dat) {
sum(dat[, "x"]*(dat[, "w"] - mean(dat[, "w"])))
})
Furthermore, we could use the median centered instead, for example
egonet_attrs(diffnet, as.df=TRUE, fun=function(dat) {
sum(dat[, "x"]*(dat[, "w"] - median(dat[, "w"])))
})
Where for each \(i\), dat
will be a matrix with as many rows
as individuals in his egonetwork. Such matrix holds the column names of the
attributes in the network.
When self = TRUE
, it will include ego's attributes, regardless
the network has loops or not.
Other data management functions:
diffnet-class
,
edgelist_to_adjmat()
,
isolated()
,
survey_to_diffnet()
# Simple example with diffnet -----------------------------------------------
set.seed(1001)
diffnet <- rdiffnet(150, 5, seed.graph="small-world")
#> 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.
# Adding attributes
indeg <- dgr(diffnet, cmode="indegree")
head(indeg)
#> 1 2 3 4 5
#> 1 1 1 2 1 2
#> 2 1 1 1 1 1
#> 3 2 2 2 2 2
#> 4 3 2 4 3 2
#> 5 5 6 4 4 4
#> 6 2 1 2 2 2
diffnet[["indegree"]] <- indeg
# Retrieving egonet's attributes (vertices 1 and 20)
egonet_attrs(diffnet, V=c(1,20))
#> $`1`
#> $`1`$`1`
#> id value per toa real_threshold indegree
#> 2 2 1 1 2 0.242223512 1
#> 150 150 1 1 NA 0.005603659 2
#>
#> $`1`$`20`
#> id value per toa real_threshold indegree
#> 19 19 1 1 NA 0.8361915 2
#> 21 21 1 1 NA 0.6509093 1
#>
#>
#> $`2`
#> $`2`$`1`
#> id value per toa real_threshold indegree
#> 2 2 1 2 2 0.242223512 1
#> 150 150 1 2 NA 0.005603659 2
#>
#> $`2`$`20`
#> id value per toa real_threshold indegree
#> 19 19 1 2 NA 0.8361915 3
#> 21 21 1 2 NA 0.6509093 2
#>
#>
#> $`3`
#> $`3`$`1`
#> id value per toa real_threshold indegree
#> 2 2 1 3 2 0.242223512 1
#> 150 150 1 3 NA 0.005603659 2
#>
#> $`3`$`20`
#> id value per toa real_threshold indegree
#> 19 19 1 3 NA 0.8361915 3
#> 21 21 1 3 NA 0.6509093 1
#>
#>
#> $`4`
#> $`4`$`1`
#> id value per toa real_threshold indegree
#> 2 2 1 4 2 0.242223512 1
#> 150 150 1 4 NA 0.005603659 2
#>
#> $`4`$`20`
#> id value per toa real_threshold indegree
#> 21 21 1 4 NA 0.6509093 1
#> 90 90 1 4 NA 0.6757134 3
#>
#>
#> $`5`
#> $`5`$`1`
#> id value per toa real_threshold indegree
#> 2 2 1 5 2 0.242223512 1
#> 150 150 1 5 NA 0.005603659 1
#>
#> $`5`$`20`
#> id value per toa real_threshold indegree
#> 19 19 1 5 NA 0.8361915 2
#> 21 21 1 5 NA 0.6509093 2
#>
#>
# Example with a static network ---------------------------------------------
set.seed(1231)
n <- 20
net <- rgraph_ws(n = n, k = 4, p = .5)
someattr <- matrix(rnorm(n * 2), ncol= 2, dimnames = list(NULL, c("a", "b")))
# Maximum of -a- in ego network
ans <- egonet_attrs(net, someattr, fun = function(x) max(x[,"a"]))
ans
#> [1] -0.8624120 1.2340156 0.3785647 0.5097039 1.6779573 0.2843847
#> [7] 1.6779573 0.5097039 1.6779573 1.6779573 0.5517721 1.6779573
#> [13] 1.6779573 0.2843847 0.3785647 0.3488837 1.6779573 1.6779573
#> [19] 0.3785647 0.5517721
# checking it worked, taking a look at node 1, 2, and 3
max(someattr[which(net[1,] == 1),"a"]) == ans[1] # TRUE
#> [1] TRUE
max(someattr[which(net[2,] == 1),"a"]) == ans[2] # TRUE
#> [1] TRUE
max(someattr[which(net[3,] == 1),"a"]) == ans[3] # TRUE
#> [1] TRUE