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

Refactor rdiffnet #46

merged 35 commits into from
Nov 22, 2024

Conversation

gvegayon
Copy link
Member

@gvegayon gvegayon commented Nov 6, 2024

No description provided.

…ication following the merge of the 41... branch
…net_make_threshold. Some modification in rdiffnet too. Not expecting to work yet.
R/rdiffnet.r Outdated
Comment on lines 565 to 568
} else if (class(seed.nodes) == "character") {

stop("-character- class not supported for multi-diffusion. It must be a -list-.")
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following code is causing an error here:

z <- rdiffnet(100,10, seed.p.adopt = list(.1, .05), seed.nodes=c(1,2,3,4))
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.TRUE
Message: Multi-diffusion behavior simulation selected. Number of behaviors:  2
Error in rdiffnet_validate_args(seed.p.adopt, seed.nodes, behavior) : 
  -character- class not supported for multi-diffusion. It must be a -list-.
> traceback()
3: stop("-character- class not supported for multi-diffusion. It must be a -list-.") at rdiffnet.r#567
2: rdiffnet_validate_args(seed.p.adopt, seed.nodes, behavior) at rdiffnet.r#381
1: rdiffnet(100, 10, seed.p.adopt = list(0.01, 0.03))

@gvegayon
Copy link
Member Author

gvegayon commented Nov 12, 2024

Tagging @twvalente for a potential model of adoption/disadoption:

$$ P(adopt_i) = \text{logit}^{-1}(\beta_{adopt} exposure_i \beta^{adopt} x) $$

$$ P(disadopt_i) = \text{logit}^{-1}(\beta_{disadopt} exposure_i \beta^{disadopt} x) $$

We could also make disadoption to happen randomly.

aoliveram and others added 2 commits November 12, 2024 19:06
… from -list-. For example: rdiffnet(100,10, seed.p.adopt = list(.1, .05)), or adding seed.nodes=c(1,2,3,4), seed.nodes=random, or seed.nodes=c(random,central). Respective tests added.
@aoliveram aoliveram marked this pull request as ready for review November 13, 2024 02:21
@aoliveram aoliveram marked this pull request as draft November 13, 2024 02:30
… work to be done to display a line saying 'number of behaviors', and to fix the summary() function.
… work to be done to display a line saying 'number of behaviors', and to fix the summary() function.
@gvegayon gvegayon marked this pull request as ready for review November 18, 2024 18:22
R/adjmat.r Outdated
@@ -462,31 462,52 @@ adjmat_to_edgelist.list <- function(graph, undirected, keep.isolates) {
#' @keywords manip
#' @include graph_data.r
#' @author George G. Vega Yon & Thomas W. Valente
toa_mat <- function(obj, labels=NULL, t0=NULL, t1=NULL) {
toa_mat <- function(obj, num_of_behaviors=1, labels=NULL, t0=NULL, t1=NULL) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The number of behaviors should be implicit (so you can figure that out from diffnet object). So don't ask the user of it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just deleted the 'num_of_behaviors' dependency in toa_mat and other functions.

R/adjmat.r Show resolved Hide resolved
@@ -547,7 547,8 @@ new_diffnet <- function(graph, toa, t0=min(toa, na.rm = TRUE), t1=max(toa, na.rm
self = getOption("diffnet.self"),
multiple = getOption("diffnet.multiple"),
name = "Diffusion Network",
behavior = "Unspecified"
behavior = "Unspecified",
num_of_behaviors = 1
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The num of behaviors should be guessed from the data.

"using -t0- and -t- (see ?toa_mat).")
} else {

# This should be reviewed !! (here the graph becomes 'dynamic')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me a while realize that, at Step 1.1 in new_diffnet, a graph will never be 'static'. This is because, if I give as an input a single 'slice' of graph ('static'), that will be converted to a list of graph before this step anyway, so it become a 'dynamic' graph whatever the circumstance. This implies that the type of graph is shown as 'dynamic':

n <- 100
nper <- 5
static_graph <- rgraph_er(n = n, t = 1, p = 0.1) 

diffnet <- rdiffnet(
  seed.graph = static_graph,
  t = 5,
  seed.p.adopt = 0.1,
  rewire = FALSE,
  seed.nodes = 'central',
  threshold.dist = runif(n, .1, .4)
)

str(diffnet) # always meta$type == dinamic !!

Since the code never really uses those lines (neither single diff nor multi diff), it's "working."

R/diffnet-class.r Show resolved Hide resolved
R/stats.R Show resolved Hide resolved
Comment on lines 14 to 16
class(rdiffnet_args$seed.p.adopt) == "list"
class(rdiffnet_args$seed.nodes) == "list"
class(rdiffnet_args$behavior) == "list"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class(rdiffnet_args$seed.p.adopt) == "list"
class(rdiffnet_args$seed.nodes) == "list"
class(rdiffnet_args$behavior) == "list"

Or add an expectation from testthat

Comment on lines 23 to 25
class(rdiffnet_args$seed.p.adopt) == "list"
class(rdiffnet_args$seed.nodes) == "list"
class(rdiffnet_args$behavior) == "list"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class(rdiffnet_args$seed.p.adopt) == "list"
class(rdiffnet_args$seed.nodes) == "list"
class(rdiffnet_args$behavior) == "list"

Comment on lines 118 to 120
class(rdiffnet_args$seed.p.adopt) == "list"
class(rdiffnet_args$seed.nodes) == "list"
class(rdiffnet_args$behavior) == "list"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Comment on lines 134 to 152
#single
rdiffnet(100, 5)
rdiffnet(100, 5, seed.p.adopt = 0.1)
rdiffnet(100, 5, seed.p.adopt = 0.1, seed.nodes = 'random')
rdiffnet(100, 5, seed.nodes = c(1,3,5))
net_1 <- rdiffnet(100, 5, seed.nodes = c(1,3,5))
summary(net_1)

#multi
rdiffnet(100, 5, seed.p.adopt = list(0.1,0.08))
rdiffnet(100, 5, seed.p.adopt = list(0.1,0.08), behavior = c('tabacco', 'alcohol'))
rdiffnet(100, 5, seed.p.adopt = list(0.1,0.08), seed.nodes = 'random')
rdiffnet(100, 5, seed.p.adopt = list(0.1,0.08), seed.nodes = c('random', 'central'))
rdiffnet(100, 5, seed.p.adopt = list(0.1,0.08), threshold.dist = 0.3)
rdiffnet(100, 5, seed.p.adopt = list(0.1,0.08), threshold.dist = list(0.1,0.2))
rdiffnet(100, 5, seed.p.adopt = list(0.1,0.08), threshold.dist = rexp(100))
rdiffnet(100, 5, seed.p.adopt = list(0.1,0.08), threshold.dist = list(rexp(100),runif(100)))
rdiffnet(100, 5, seed.p.adopt = list(0.1,0.08), threshold.dist = function(x) 0.3)
rdiffnet(100, 5, seed.p.adopt = list(0.1,0.08), threshold.dist = list(function(x) 0.3, function(x) 0.2))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, add expectations of of the sort of "expect class" something like that so you ensure it returns diffnet objects.

@gvegayon gvegayon linked an issue Nov 19, 2024 that may be closed by this pull request
@gvegayon gvegayon mentioned this pull request Nov 19, 2024
@gvegayon gvegayon merged commit 78e2a44 into master Nov 22, 2024
4 checks passed
@gvegayon gvegayon deleted the refactor-rdiffnet branch November 22, 2024 21:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor rdiffnet
2 participants