Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor rdiffnet #46

Merged
merged 35 commits into from
Nov 22, 2024
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift click to select a range
7f3defd
Adding myself to the project
aoliveram Oct 29, 2024
18cd21f
Some discussion about the dimensions of ans (exposure calculation)
aoliveram Oct 30, 2024
34f2e73
just fixing a paragraph in Ego exposure
aoliveram Oct 31, 2024
f27f174
looking as.vector things
aoliveram Oct 31, 2024
8977e4f
out object from exposure_for()
aoliveram Oct 31, 2024
ef83eb2
More dimensional analysis. Changes to avoid ambiguous names
aoliveram Nov 1, 2024
8a8282c
stats.R fixed
aoliveram Nov 1, 2024
bd5c92f
out object (in exposure_for() function) now allows q diff processes
aoliveram Nov 1, 2024
8f3314e
Working forms of .exposure, exposure_for, and exposure.list
aoliveram Nov 2, 2024
b7f689a
updates for .exposure and exposure.list functions
aoliveram Nov 4, 2024
6ffdba5
correcting labels of variables
aoliveram Nov 4, 2024
d4720e0
Fixing tests of diffnet
aoliveram Nov 4, 2024
bcabadb
changes to exposure.list() to allow arrays of cumadopt. Add multidiff…
aoliveram Nov 5, 2024
587babb
aditional test -multidiffusion exposure calculations-
aoliveram Nov 6, 2024
9f9a25e
updating to Steps 1.1 (initial adopters) and 1.2 (finding seed nodes…
aoliveram Nov 6, 2024
a54800b
updating cumadopt, exposure simulation, and toa for multi-diff processes
aoliveram Nov 6, 2024
1d34b5d
adding a set of tests for rdiffnet_validate_args function
aoliveram Nov 8, 2024
83d1d66
rdiffnet function updated to allow multi-diff. An small error in rdif…
aoliveram Nov 8, 2024
c5990c4
generalization of rdiffnet_make_threshold function. Some others modif…
aoliveram Nov 11, 2024
3ef1d72
lot of work in new_diffnet and toa_mat functions. New tests for rdiff…
aoliveram Nov 12, 2024
39a3840
changes in new_diffnet and toa_mat. Now all the original tests for th…
aoliveram Nov 12, 2024
3b360b3
updating rdiffnet_validate_args to allow objects seed.nodes different…
aoliveram Nov 13, 2024
b183316
Merge branch 'master' into refactor-rdiffnet
aoliveram Nov 13, 2024
271047b
rdiffnet now allow multiple diff, showing the results. There is still…
aoliveram Nov 13, 2024
0615685
rdiffnet now allow multiple diff, showing the results. There is still…
aoliveram Nov 13, 2024
e871f33
Now rdiffnet allow multiple diff, and shows the name -Behavior-, -Num…
aoliveram Nov 13, 2024
3755959
some minor changes in summary.diffnet
aoliveram Nov 14, 2024
875b22f
advances in summary.diffnet() for multi-diff, but this will be change…
aoliveram Nov 15, 2024
e9a34cd
changes in exposure.list and exposure_for to allow personalized attrs…
aoliveram Nov 18, 2024
345df65
minor changes in toa_mat
aoliveram Nov 18, 2024
e01535e
now new_diffnet sets the num_of behavior internally
aoliveram Nov 19, 2024
b6e63ca
more changes to toa_mat to compute num_of_adoption on more classes
aoliveram Nov 19, 2024
516cf33
now toa_mat can compute adopt and cumadopt from diffnet (multiple) an…
aoliveram Nov 19, 2024
90f4af5
all comments were addressed, except -behavior- as a vector.
aoliveram Nov 19, 2024
93dc056
checking the status of "dynamic" and "static" graphs.
aoliveram Nov 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
updating cumadopt, exposure simulation, and toa for multi-diff processes
  • Loading branch information
aoliveram committed Nov 6, 2024
commit a54800bbd14860fe2fb2deba6566ae4eff17e78f
151 changes: 104 additions & 47 deletions R/rdiffnet.r
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 316,7 @@ rdiffnet <- function(
seed.p.adopt = 0.05,
seed.graph = "scale-free",
rgraph.args = list(),
rewire = TRUE,
rewire = TRUE, #set TRUE originally
rewire.args = list(),
threshold.dist = runif(n),
exposure.args = list(),
Expand Down Expand Up @@ -369,9 369,9 @@ rdiffnet <- function(
# Step 0.1: Rewiring or not ------------------------------------------------

# Rewiring
if (rewire)
if (rewire) {
sgraph <- do.call(rewire_graph, c(list(graph=sgraph), rewire.args))

}
sgraph <- lapply(sgraph, `attr<-`, which="undirected", value=NULL)

# Step 1.0: Setting the seed nodes -----------------------------------------
Expand All @@ -394,7 394,7 @@ rdiffnet <- function(
n0[[i]] <- max(1, n * seed.p.adopt[i])
}

} else if (length(seed.p.adopt)==1 && length(seed.p.adopt) == behavior.num) {
} else if (length(seed.p.adopt)== 1 && behavior.num == 1) {

if ((seed.p.adopt > 1) | (seed.p.adopt < 0)) {
stop("The proportion of initial adopters should be a number in [0,1]")
Expand All @@ -405,30 405,31 @@ rdiffnet <- function(

n0 <- max(1, n*seed.p.adopt)
} else {
stop("Number of initial adopters. Mismatch between length(seed.p.adopt) and behavior.num")
stop("Error in setting number of initial adopters. Mismatch between length(seed.p.adopt) and behavior.num")
}


# Step 1.2: Finding seed nodes
if (length(seed.nodes) > 1 && length(seed.nodes) == behavior.num) {
dlist <- list()
if (length(seed.nodes) > 1 && length(seed.nodes) == behavior.num && class(seed.nodes)!="list") {
# multi-diff. Something like seed.nodes <- c("marginal", "central"), and behavior.num <- 2

d <- list()
if (any(seed.nodes %in% c("central", "marginal"))) {
d <- dgr(sgraph)[, 1, drop = FALSE]
central_d <- rownames(d[order(d, decreasing = TRUE), , drop = FALSE])
marginal_d <- rownames(d[order(d, decreasing = FALSE), , drop = FALSE])
dg <- dgr(sgraph)[, 1, drop = FALSE]
central_d <- rownames(dg[order(dg, decreasing = TRUE), , drop = FALSE])
marginal_d <- rownames(dg[order(dg, decreasing = FALSE), , drop = FALSE])
}

# assign nodes characters values in seed.nodes
for (i in seq_along(seed.p.adopt)) {
dlist[[i]] <- switch(seed.nodes[i],
"central" = as.numeric(central_d[1:floor(n0[[i]])]),
"marginal" = as.numeric(marginal_d[1:floor(n0[[i]])]),
"random" = sample.int(n, floor(n0[[i]])),
stop("Unsupported -seed.nodes- value. It must be either \"central\", \"marginal\", or \"random\"")
for (i in seq_along(seed.nodes)) { # assign nodes characters values in seed.nodes
d[[i]] <- switch(seed.nodes[i],
"central" = as.numeric(central_d[1:floor(n0[[i]])]),
"marginal" = as.numeric(marginal_d[1:floor(n0[[i]])]),
"random" = sample.int(n, floor(n0[[i]])),
stop("Unsupported -seed.nodes- value. It must be either \"central\", \"marginal\", or \"random\"")
)
}
} else if (length(seed.nodes) == 1 && length(seed.nodes) == behavior.num) {
} else if (length(seed.nodes) == 1 && behavior.num == 1) {
# Single-diff. Something like seed.nodes <- "central"

if (seed.nodes %in% c("central","marginal")) {

Expand All @@ -447,43 448,86 @@ rdiffnet <- function(
stop("Unsupported -seed.nodes- value. It must be either \"central\", \"marginal\", or \"random\"")
}
} else if (class(seed.nodes)=="list" && length(seed.nodes) != behavior.num) {

stop("Finding seed nodes. Mismatch between length(seed.nodes) and behavior.num")
# Something like seed.nodes <- c("marginal", "central"), BUT behavior.num <- 3
stop("Error in finding seed nodes. Mismatch between length(seed.nodes) and behavior.num")

} else if (!inherits(seed.nodes, "character")) {

if (length(seed.nodes) >= 1 && length(seed.nodes) == behavior.num) {
d <- seed.nodes
} else if (class(seed.nodes)=="list" && length(seed.nodes) != behavior.num) {
if (class(seed.nodes)=="list" && length(seed.nodes) != behavior.num) {
# Something like seed.nodes <- list(c(1,4), c(3,6,8)), BUT behavior.num <- 3
stop("Particular seed nodes provided. Mismatch between length(seed.nodes) and behavior.num")
} else {
} else {
# single-diff and multi-diff. # Something like seed.nodes <- c(3,6,8)), AND behavior.num <- 1,
# or seed.nodes <- list(c(1,4), c(3,6,8)), AND behavior.num <- 2
d <- seed.nodes
}

} else {stop("Unsupported -seed.nodes- value. See the manual for references.") }

# Step 1.3: Defining cumadopt and toa (time of adoption) --------------------
cumadopt <- matrix(0L, ncol=t, nrow=n)
toa <- matrix(NA, ncol=1, nrow= n)

# Setting seed nodes via vector
toa[d] <- 1L
cumadopt[d,] <- 1L
if (class(d) == "list") {
# multi-diff

if (length(d) != behavior.num) {
stop("Error: length(d) must be the same as behavior.num")
}

cumadopt <- array(0L, dim = c(n, t, behavior.num))

# Setting seed nodes via array
for (i in seq_along(d)) {
cumadopt[d[[i]],,i] <- 1L
}
} else {
# single-diff
cumadopt <- matrix(0L, ncol=t, nrow=n)
toa <- matrix(NA, ncol=1, nrow= n)

# Setting seed nodes via vector
toa[d] <- 1L # REMINDER TO DELETE THIS OBJECT !!!
cumadopt[d,] <- 1L
}

# Step 2.0: Thresholds -------------------------------------------------------
thr <- rdiffnet_make_threshold(threshold.dist, n)
thr <- rdiffnet_make_threshold(threshold.dist, n) # REMINDER TO CHANGE rdiffnet_make_threshold

# Step 3.0: Running the simulation -------------------------------------------
for (i in 2:t) {
if (!is.na(dim(cumadopt)[3])) {
# multi-diff. Computing exposure
# ONLY MEANWHILE
thr <- array(c(thr,rev(thr)), dim=c(length(thr), dim(cumadopt)[3]))

exposure.args[c("graph", "cumadopt")] <- list(sgraph[i], cumadopt[,i, ,drop=FALSE])
expo <- do.call(exposure, exposure.args)
#for (q in 1:dim(cumadopt)[3]) {
# exposure.args[c("graph", "cumadopt")] <- list(sgraph[i], cumadopt[,i,q,drop=FALSE])
#}

toa <- matrix(NA, nrow = dim(cumadopt)[1], ncol = dim(cumadopt)[3])

for (q in 1:dim(cumadopt)[3]) {
whoadopts <- which( (expo[,,q] >= thr[,q]) )# & is.na(toa))
cumadopt[whoadopts, i:t, q] <- 1L
# ADD SOMETHING TO DISADOPT
# Initialize 'toa' with NA values
toa[, q] <- apply(cumadopt[,, q], 1, function(x) {
first_adopt <- which(x == 1)
if (length(first_adopt) > 0) first_adopt[1] else NA
})
}

# Computing exposure
exposure.args[c("graph", "cumadopt")] <- list(sgraph[i], cumadopt[,i,drop=FALSE])
expo <- do.call(exposure, exposure.args)
} else {
# single-diff. Computing exposure
exposure.args[c("graph", "cumadopt")] <- list(sgraph[i], cumadopt[,i,drop=FALSE])
expo <- do.call(exposure, exposure.args)

whoadopts <- which( (expo >= thr) & is.na(toa))
toa[whoadopts] <- i
cumadopt[whoadopts, i:t] <- 1L
whoadopts <- which( (expo >= thr) & is.na(toa))
toa[whoadopts] <- i
cumadopt[whoadopts, i:t] <- 1L
}
}
# GENERALIZE TO MULTI-DIFF
reachedt <- max(toa, na.rm=TRUE)

# Checking the result
Expand All @@ -498,15 542,28 @@ rdiffnet <- function(
# Checking attributes
isself <- any(sapply(sgraph, function(x) any(Matrix::diag(x) != 0) ))

new_diffnet(
graph = sgraph,
toa = as.integer(toa),
self = isself,
t0 = 1,
t1 = t,
vertex.static.attrs = data.frame(real_threshold=thr),
name = name,
behavior = behavior
)
if (!is.na(dim(cumadopt)[3])) {
new_diffnet(
graph = sgraph,
toa = toa,
self = isself,
t0 = 1,
t1 = t,
vertex.static.attrs = data.frame(real_threshold=thr),
name = name,
behavior = behavior
)
} else {
new_diffnet(
graph = sgraph,
toa = as.integer(toa),
self = isself,
t0 = 1,
t1 = t,
vertex.static.attrs = data.frame(real_threshold=thr),
name = name,
behavior = behavior
)
}
}

Loading