Skip to content

Commit 4829569

Browse files
committed
Coied the b*.size() and *_along() functions from 'ergm' and made use of them.
1 parent 417f1c6 commit 4829569

8 files changed

Lines changed: 32 additions & 17 deletions

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: ergm.multi
22
Version: 0.3.0-4181
3-
Date: 2025-06-10
3+
Date: 2025-06-12
44
Title: Fit, Simulate and Diagnose Exponential-Family Models for Multiple or Multilayer Networks
55
Authors@R: c(person(c("Pavel", "N."), "Krivitsky", role=c("aut","cre"), email="pavel@statnet.org", comment=c(ORCID="0000-0002-9101-3362")),
66
person(c("Mark", "S."), "Handcock", role=c("ctb"), email="handcock@stat.ucla.edu"),

R/InitErgmConstraint.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@
5858
## n <- network.size(lhs.nw)
5959
## storage.mode(n) <- "integer"
6060
## a <- c(ergm_get_vattr(attr, lhs.nw)) # Strip attributes, which confuse rle().
61-
## if(NVL(lhs.nw%n%"bipartite",0)){
62-
## bip <- lhs.nw %n% "bipartite"
61+
## if(NVL(lhs.b1.size(nw),0)){
62+
## bip <- lhs.b1.size(nw)
6363
## ea <- a[seq_len(bip)]
6464
## aa <- a[bip+seq_len(n-bip)]
6565
## if(length(rle(ea)$lengths)!=length(unique(rle(ea)$values)) || length(rle(aa)$lengths)!=length(unique(rle(aa)$values))) stop("Current implementation of block-diagonal sampling requires that the blocks of the egos and the alters be contiguous. See ", sQuote("ergmConstraint?blockdiag"), " for more information.")

R/InitErgmProposal.blockdiag.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
#' @keywords internal
110110
#' @export
111111
ergm_block_diag_samp_info <- function(nw, a){
112-
bip <- nw %n% "bipartite"
112+
bip <- b1.size(nw)
113113
if(bip){
114114
ea <- a[seq_len(bip)]
115115
aa <- a[bip+seq_len(network.size(nw)-bip)]
@@ -135,7 +135,9 @@ ergm_block_diag_samp_info <- function(nw, a){
135135
w <- cumsum(a$lengths*(a$lengths-1)) # cumulative block weights ~ # dyads in the block
136136
w <- w/max(w)
137137
# Note that this automagically takes care of singleton blocks by giving them weight 0.
138-
out <- list(nblk=as.integer(length(w)), pos=as.integer(b), cumwt=as.double(w), ndyads=sum(as.double(a$lengths*(a$lengths-1)/(if(is.directed(nw)) 1 else 2))))
138+
out <- list(nblk = as.integer(length(w)), pos = as.integer(b),
139+
cumwt = as.double(w),
140+
ndyads = sum(choose(a$lengths, 2L) * (1L + is.directed(nw))))
139141
}
140142
structure(out, class="ergm_block_diag_samp_info")
141143
}

R/InitErgmTerm.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ InitErgmTerm.b1degreeL <- function(nw, arglist, ...) {
163163
defaultvalues = list(NULL, NULL, NULL, NULL),
164164
required = c(TRUE, FALSE, FALSE, FALSE))
165165
### Process the arguments
166-
nb1 <- get.network.attribute(nw, "bipartite")
166+
nb1 <- b1.size(nw)
167167
if (!is.null(a$by)) { # CASE 1: a$by GIVEN
168168
nodecov <- get.node.attr(nw, a$by)[seq_len(nb1)]
169169
u <- NVL(a$levels, sort(unique(nodecov)))
@@ -236,7 +236,7 @@ InitErgmTerm.b2degreeL <- function(nw, arglist, ...) {
236236
defaultvalues = list(NULL, NULL, NULL, NULL),
237237
required = c(TRUE, FALSE, FALSE, FALSE))
238238
### Process the arguments
239-
nb1 <- get.network.attribute(nw, "bipartite")
239+
nb1 <- b1.size(nw)
240240
n <- network.size(nw)
241241
if (!is.null(a$by)) { # CASE 1: a$by GIVEN
242242
nodecov <- get.node.attr(nw, a$by)[-seq_len(nb1)]
@@ -332,7 +332,7 @@ InitErgmTerm.degreeL<-function(nw, arglist, ...) {
332332
}
333333
} else {
334334
if (any(d==0)) {
335-
emptynwstats <- rep(0, length(d))
335+
emptynwstats <- dbl_along(d)
336336
emptynwstats[d==0] <- network.size(nw)
337337
}
338338
}
@@ -409,7 +409,7 @@ InitErgmTerm.gwb1degreeL<-function(nw, arglist, gw.cutoff=30, ...) {
409409
required = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
410410
decay<-a$decay; fixed<-a$fixed; attrname<-a$attrname
411411
cutoff<-a$cutoff
412-
nb1 <- get.network.attribute(nw,"bipartite")
412+
nb1 <- b1.size(nw)
413413
# d <- 1:(network.size(nw) - nb1)
414414
maxesp <- min(cutoff, network.size(nw)-nb1)
415415

@@ -502,7 +502,7 @@ InitErgmTerm.gwb2degreeL<-function(nw, arglist, gw.cutoff=30, ...) {
502502
required = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
503503
decay<-a$decay; fixed<-a$fixed; attrname<-a$attrname
504504
cutoff<-a$cutoff
505-
nb1 <- get.network.attribute(nw,"bipartite")
505+
nb1 <- b1.size(nw)
506506
# d <- 1:nb1
507507
maxesp <- min(cutoff,nb1)
508508
d <- 1:maxesp
@@ -845,7 +845,7 @@ InitErgmTerm.idegreeL<-function(nw, arglist, ...) {
845845
}
846846
} else {
847847
if (any(d==0)) {
848-
emptynwstats <- rep(0, length(d))
848+
emptynwstats <- dbl_along(d)
849849
emptynwstats[d==0] <- network.size(nw)
850850
}
851851
}
@@ -1057,7 +1057,7 @@ InitErgmTerm.odegreeL<-function(nw, arglist, ...) {
10571057
}
10581058
} else {
10591059
if (any(d==0)) {
1060-
emptynwstats <- rep(0, length(d))
1060+
emptynwstats <- dbl_along(d)
10611061
emptynwstats[d==0] <- network.size(nw)
10621062
}
10631063
}

R/InitErgmTerm.dgw_sp.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ wrap_ergm_sp_call <- function(ergm_name, nw, a, has_base, d0 = FALSE, cache.sp =
8787
.emptynwstats <-
8888
if (d0 && any(a$d == 0)) {
8989
if (is.bipartite(nw)) {
90-
nb1 <- get.network.attribute(nw, "bipartite")
90+
nb1 <- b1.size(nw)
9191
nb2 <- network.size(nw) - nb1
9292
replace(numeric(length(a$d)), a$d == 0, choose(nb1, 2) + choose(nb2, 2))
9393
} else {

R/InitErgmTerm.multilayer.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,9 @@ direct.network <- function(x, rule=c("both", "upper", "lower")){
217217
upper = cbind(pmin(el[,1],el[,2]),pmax(el[,1],el[,2])),
218218
lower = cbind(pmax(el[,1],el[,2]),pmin(el[,1],el[,2])))
219219

220-
o <- network.initialize(network.size(x), directed=TRUE, bipartite=x%n%"bipartite", loops=has.loops(x), hyper=is.hyper(x), multiple=is.multiplex(x))
220+
o <- network.initialize(network.size(x), directed = TRUE,
221+
bipartite = b1.size(x), loops = has.loops(x),
222+
hyper = is.hyper(x), multiple = is.multiplex(x))
221223
o <- network.edgelist(el, o)
222224
nvattr.copy.network(o, x)
223225
}
@@ -482,7 +484,7 @@ Layer <- function(..., .symmetric=NULL, .bipartite=NULL, .active=NULL){
482484
}
483485

484486
# nwl may now be a list with networks of heterogeneous bipartitedness.
485-
bip <- sapply(nwl, `%n%`, "bipartite") %>% sapply(NVL, 0L)
487+
bip <- sapply(nwl, b1.size) %>% sapply(NVL, 0L)
486488
blockout <- if(all_identical(bip)) rep(FALSE, length(nwl)) else bip
487489

488490
nwl <- Map(function(nw, b){

R/combine.networks.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ combine_networks <- function(nwl, ignore.nattr=c("mnext"), ignore.vattr=c(), ign
234234
attrset <- if(keep.unshared.attr) union else intersect
235235

236236
ns <- sapply(nwl, network.size)
237-
es <- sapply(nwl, "%n%", "bipartite")
237+
es <- sapply(nwl, b1.size)
238238
eblks <- c(0, cumsum(es))
239239
bip <- eblks[length(eblks)]
240240
ablks <- cumsum(c(bip, ns-es))
@@ -537,7 +537,7 @@ subnetwork_templates <- function(nw, split.vattr=nw%n%".blockID.vattr", names.va
537537
.block_vertexmap <- function(nw, by=nw %n% ".blockID.vattr", same_dim=FALSE){
538538
a <- .peek_vattrv(nw, by)
539539
n <- length(a)
540-
bip <- nw %n% "bipartite"
540+
bip <- b1.size(nw)
541541
if(NVL(bip,0)){
542542
ea <- a[seq_len(bip)]
543543
aa <- a[bip+seq_len(n-bip)]

R/util.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## These will eventually be moved to 'statnet.common'.
2+
3+
dbl_along <- function(x) numeric(length(x))
4+
int_along <- function(x) integer(length(x))
5+
chr_along <- function(x) character(length(x))
6+
lgl_along <- function(x) logical(length(x))
7+
8+
## This should be in 'network'.
9+
10+
b1.size <- function(x) if(is.bipartite(x)) x %n% "bipartite" else FALSE
11+
b2.size <- function(x) if(is.bipartite(x)) network.size(x) - b1.size(x) else FALSE

0 commit comments

Comments
 (0)