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
Show file tree
Hide file tree
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
changes to exposure.list() to allow arrays of cumadopt. Add multidiff…
…-test-discussion too.
  • Loading branch information
aoliveram committed Nov 5, 2024
commit bcabadbfb71957e079b63058a74dc5a0e19e0cc6
17 changes: 13 additions & 4 deletions R/stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -638,10 638,19 @@ exposure.list <- function(
# attrs can be either
# degree, indegree, outdegree, or a user defined vector.
# by default is user equal to 1
da <- dim(attrs)
if (!length(da)) stop("-attrs- must be a matrix of size n by T.")
if (any(da != dim(cumadopt))) stop("Incorrect size for -attrs-. ",
"It must be of size that -cumadopt-.")

dim_attrs <- dim(attrs) # default n x T matrix of 1's
if (!length(dim_attrs)) stop("-attrs- must be a matrix of size n by T.")

if (!is.na(dim(cumadopt)[3])) {
attrs <- array(rep(attrs, q), dim = c(dim_attrs, dim(cumadopt)[3]))
dim_attrs <- dim(attrs) # now n x T x q array of 1's, q behaviors
if (any(dim_attrs != dim(cumadopt))) stop("Incorrect size for -attrs-. ",
"Does not match n dim or t dim.")
} else {
if (any(dim_attrs != dim(cumadopt))) stop("Incorrect size for -attrs-. ",
"It must be of size that -cumadopt-.")
}

add_dimnames.mat(cumadopt)

Expand Down
131 changes: 131 additions & 0 deletions playground/multidiff-test-discussion.R
Original file line number Diff line number Diff line change
@@ -0,0 1,131 @@

test_that("multidiffusion exposure calculations", {
# Generating data
diffnet <- rdiffnet(40,5, seed.p.adopt = .1)

#data(medInnovationsDiffNet)
#exposure(medInnovationsDiffNet)

# two spreads
cumadopt_2 <- medInnovationsDiffNet$cumadopt
cumadopt_2 <- array(c(cumadopt_2,cumadopt_2[rev(1:nrow(cumadopt_2)),]), dim=c(dim(cumadopt_2), 2))

# Default
#ans0 <- exposure(medInnovationsDiffNet)#exposure(diffnet)
ans1 <- as.matrix(do.call(cbind,lapply(medInnovationsDiffNet$meta$pers, function(x) {
s <- medInnovationsDiffNet$graph[[x]]
for (q in 1:dim(cumadopt)[3]) {
( s %*% cumadopt_2[,x,q,drop=FALSE])/(1e-15 Matrix::rowSums(s))
}
})))

exposure(medInnovationsDiffNet$graph, medInnovationsDiffNet$cumadopt)

ans2 <- exposure(medInnovationsDiffNet$graph, cumadopt = cumadopt_2)
#ans3 <- exposure(as.array(medInnovationsDiffNet), cumadopt = cumadopt_2)

expect_equivalent(ans0, ans1)
expect_equivalent(ans0, ans2)
expect_equivalent(ans0, ans3)

# With an attribute
X <- matrix(diffnet[["real_threshold"]], ncol=5, nrow=40, byrow = FALSE)
ans0 <- exposure(diffnet, attrs=X)
ans1 <- exposure(diffnet, attrs="real_threshold")
expect_equivalent(ans0, ans1)

expect_error(exposure(diffnet$graph, attrs="real_threshold"),"is only valid for")

# Struct Equiv
se <- struct_equiv(diffnet)
se <- lapply(se, function(x) {
ans <- methods::as(x$SE, "dgCMatrix")
ans@x <- 1/(ans@x 1e-20)
ans
})
exp_1_diffnet <- exposure(diffnet, alt.graph = se, valued=TRUE)
se2 <- vector("list", length(se))
exp_1_manual <- as.matrix(do.call(cbind,lapply(diffnet$meta$pers, function(x) {
s <- methods::as(struct_equiv(diffnet$graph[[x]])$SE, "dgCMatrix")
s@x <- 1/(s@x 1e-20)
se2[[x]] <<- s
( s %*% diffnet$cumadopt[,x,drop=FALSE])/(Matrix::rowSums(s) 1e-20)
})))

expect_equivalent(unname(exp_1_diffnet), unname(exp_1_manual))

# Lagged exposure
ans0 <- exposure(diffnet)
ans1 <- exposure(diffnet, lags = 1)
ans2 <- exposure(diffnet, lags = 2)
ans3 <- exposure(diffnet, lags = -1)

expect_equivalent(ans0[,-5], ans1[,-1])
expect_equivalent(ans0[,-(4:5)], ans2[,-(1:2)])
expect_equivalent(ans0[,-1], ans3[,-5])

expect_error(exposure(diffnet, lags=5), "cannot be greater")
expect_error(exposure(diffnet, lags=NA))
expect_error(exposure(diffnet, lags=c(1:2)))

})

test_that("multidiffusion exposure calculations", {
# Generating data
set.seed(999)
diffnet <- rdiffnet(40,5, seed.p.adopt = .1)

# Default
ans0 <- exposure(diffnet)
ans1 <- as.matrix(do.call(cbind,lapply(diffnet$meta$pers, function(x) {
s <- diffnet$graph[[x]]
( s %*% diffnet$cumadopt[,x,drop=FALSE])/(1e-15 Matrix::rowSums(s))
})))
ans2 <- exposure(diffnet$graph, cumadopt = diffnet$cumadopt)
ans3 <- exposure(as.array(diffnet), cumadopt = diffnet$cumadopt)

expect_equivalent(ans0, ans1)
expect_equivalent(ans0, ans2)
expect_equivalent(ans0, ans3)

# With an attribute
X <- matrix(diffnet[["real_threshold"]], ncol=5, nrow=40, byrow = FALSE)
ans0 <- exposure(diffnet, attrs=X)
ans1 <- exposure(diffnet, attrs="real_threshold")
expect_equivalent(ans0, ans1)

expect_error(exposure(diffnet$graph, attrs="real_threshold"),"is only valid for")

# Struct Equiv
se <- struct_equiv(diffnet)
se <- lapply(se, function(x) {
ans <- methods::as(x$SE, "dgCMatrix")
ans@x <- 1/(ans@x 1e-20)
ans
})
exp_1_diffnet <- exposure(diffnet, alt.graph = se, valued=TRUE)
se2 <- vector("list", length(se))
exp_1_manual <- as.matrix(do.call(cbind,lapply(diffnet$meta$pers, function(x) {
s <- methods::as(struct_equiv(diffnet$graph[[x]])$SE, "dgCMatrix")
s@x <- 1/(s@x 1e-20)
se2[[x]] <<- s
( s %*% diffnet$cumadopt[,x,drop=FALSE])/(Matrix::rowSums(s) 1e-20)
})))

expect_equivalent(unname(exp_1_diffnet), unname(exp_1_manual))

# Lagged exposure
ans0 <- exposure(diffnet)
ans1 <- exposure(diffnet, lags = 1)
ans2 <- exposure(diffnet, lags = 2)
ans3 <- exposure(diffnet, lags = -1)

expect_equivalent(ans0[,-5], ans1[,-1])
expect_equivalent(ans0[,-(4:5)], ans2[,-(1:2)])
expect_equivalent(ans0[,-1], ans3[,-5])

expect_error(exposure(diffnet, lags=5), "cannot be greater")
expect_error(exposure(diffnet, lags=NA))
expect_error(exposure(diffnet, lags=c(1:2)))

})
Loading