# Calculating lagged exposure -----------------------------------------------
set.seed(8)
graph <- rdiffnet(20, 4)
expo0 <- exposure(graph)
expo1 <- exposure(graph, lags = 1)
# These should be equivalent
stopifnot(all(expo0[, -4] == expo1[, -1])) # No stop!
# Calculating the exposure based on Structural Equivalence ------------------
set.seed(113132)
graph <- rdiffnet(100, 4)
SE <- lapply(struct_equiv(graph), "[[", "SE")
SE <- lapply(SE, function(x) {
x <- 1/x
x[!is.finite(x)] <- 0
x
})
# These three lines are equivalent to:
expo_se2 <- exposure(graph, alt.graph="se", valued=TRUE)
# Notice that we are setting valued=TRUE, but this is not necesary since when
# alt.graph = "se" the function checks this to be setted equal to TRUE
# Weighted Exposure using degree --------------------------------------------
eDE <- exposure(graph, attrs=dgr(graph))
# Which is equivalent to
graph[["deg"]] <- dgr(graph)
eDE2 <- exposure(graph, attrs="deg")
# Comparing using incoming edges -------------------------------------------
eIN <- exposure(graph, outgoing=FALSE)
# Structral equivalence for different communities ---------------------------
data(medInnovationsDiffNet)
# Only using 4 time slides, this is for convenience
medInnovationsDiffNet <- medInnovationsDiffNet[, , 1:4]
# METHOD 1: Using the c.diffnet method:
# Creating subsets by city
cities <- unique(medInnovationsDiffNet[["city"]])
diffnet <- medInnovationsDiffNet[medInnovationsDiffNet[["city"]] == cities[1]]
diffnet[["expo_se"]] <- exposure(diffnet, alt.graph="se", valued=TRUE)
for (v in cities[-1]) {
diffnet_v <- medInnovationsDiffNet[medInnovationsDiffNet[["city"]] == v]
diffnet_v[["expo_se"]] <- exposure(diffnet_v, alt.graph="se", valued=TRUE)
diffnet <- c(diffnet, diffnet_v)
}
# We can set the original order (just in case) of the data
diffnet <- diffnet[medInnovationsDiffNet$meta$ids]
diffnet
# Checking everything is equal
test <- summary(medInnovationsDiffNet, no.print=TRUE) ==
summary(diffnet, no.print=TRUE)
stopifnot(all(test[!is.na(test)]))
# METHOD 2: Using the 'groupvar' argument
# Further, we can compare this with using the groupvar
diffnet[["expo_se2"]] <- exposure(diffnet, alt.graph="se",
groupvar="city", valued=TRUE)
# These should be equivalent
test <- diffnet[["expo_se", as.df=TRUE]] == diffnet[["expo_se2", as.df=TRUE]]
stopifnot(all(test[!is.na(test)]))
# METHOD 3: Computing exposure, rbind and then adding it to the diffnet object
expo_se3 <- NULL
for (v in unique(cities))
expo_se3 <- rbind(
expo_se3,
exposure(
diffnet[diffnet[["city"]] == v],
alt.graph = "se", valued=TRUE
))
# Just to make sure, we sort the rows
expo_se3 <- expo_se3[diffnet$meta$ids,]
diffnet[["expo_se3"]] <- expo_se3
test <- diffnet[["expo_se", as.df=TRUE]] == diffnet[["expo_se3", as.df=TRUE]]
stopifnot(all(test[!is.na(test)]))
# METHOD 4: Using the groupvar in struct_equiv
se <- struct_equiv(diffnet, groupvar="city")
se <- lapply(se, "[[", "SE")
se <- lapply(se, function(x) {
x <- 1/x
x[!is.finite(x)] <- 0
x
})
diffnet[["expo_se4"]] <- exposure(diffnet, alt.graph=se, valued=TRUE)
test <- diffnet[["expo_se", as.df=TRUE]] == diffnet[["expo_se4", as.df=TRUE]]
stopifnot(all(test[!is.na(test)]))
# Examples for multi-diffusion ---------------------------
# Running a multi-diffusion simulation, with q=2 behaviors
set.seed(999)
n <- 40; t <- 5; q <- 2;
graph <- rgraph_ws(n, t, p=.3)
seed_prop_adopt <- rep(list(0.1), q)
diffnet <- rdiffnet(seed.graph = graph, t = t, seed.p.adopt = seed_prop_adopt)
# Getting the cumulative adoption array of dims n x T x q
cumadopt_2 <- diffnet$cumadopt # list of matrices
cumadopt_2 <- array(unlist(cumadopt_2), dim = c(n, t, q))
expo2 <- exposure(diffnet$graph, cumadopt = cumadopt_2)
# With an attribute --
X <- matrix(runif(n * t), nrow = n, ncol = t) # matrix n x T
ans3 <- exposure(diffnet$graph, cumadopt = cumadopt_2, attrs=X)
X <- array(runif(n * t * q), dim = c(n, t, q)) # array n x T x q
ans4 <- exposure(diffnet$graph, cumadopt = cumadopt_2, attrs=X)
# Exposure based on Structural Equivalence --
diffnet_1 <- split_behaviors(diffnet)[[1]]
se <- struct_equiv(diffnet)
se <- lapply(se, function(x) {
ans <- methods::as(x$SE, "dgCMatrix")
ans@x <- 1/(ans@x + 1e-20)
ans
})
ans6 <- exposure(diffnet, cumadopt = cumadopt_2, alt.graph = se, valued=TRUE)
Run the code above in your browser using DataLab