Skip to content

Commit

Permalink
fixallbut() constraint now also accepts an rlebdm as input.
Browse files Browse the repository at this point in the history
  • Loading branch information
krivit committed Dec 22, 2024
1 parent 6724034 commit e71e4fc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: ergm
Version: 4.8.0-7504
Version: 4.8.0-7505
Date: 2024-12-22
Title: Fit, Simulate and Diagnose Exponential-Family Models for Networks
Authors@R: c(
Expand Down
25 changes: 13 additions & 12 deletions R/InitErgmConstraint.R
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ InitErgmConstraint.observed <- function(nw, arglist, ...){
}

warn_netsize <- function(.n, ...){
mismatch <- vapply(list(...), function(x) is.network(x) && network.size(x) != .n, logical(1))
mismatch <- map_lgl(list(...), function(x) (is.network(x) && network.size(x) != .n) || (is(x, "rlebdm") && nrow(x) != .n))
if(any(mismatch))
ergm_Init_warning("Network size of argument(s) ", paste.and(sQuote(...names()[mismatch])), " differs from that of the response network.")
}
Expand Down Expand Up @@ -477,7 +477,7 @@ InitErgmConstraint.fixedas<-function(nw, arglist,...){
#'
#' @usage
#' # fixallbut(free.dyads)
#' @param free.dyads edgelist or network. Networks will be converted to the corresponding edgelist.
#' @param free.dyads a two-column edge list, a [`network`], or an [`rlebdm`]. Networks will be converted to the corresponding edgelist.
#'
#' @template ergmConstraint-general
#'
Expand All @@ -487,23 +487,24 @@ InitErgmConstraint.fixedas<-function(nw, arglist,...){
InitErgmConstraint.fixallbut<-function(nw, arglist,...){
a <- check.ErgmTerm(nw, arglist,
varnames = c("free.dyads"),
vartypes = c("network,matrix"),
vartypes = c("network,matrix,rlebdm"),
defaultvalues = list(NULL),
required = c(TRUE))
free.dyads <- a$free.dyads

warn_netsize(network.size(nw), free.dyads = free.dyads)

list(
free_dyads = function(){
if(is.network(free.dyads)) free.dyads <- as.edgelist(free.dyads)
else free.dyads <- as.edgelist(free.dyads,
n=nw%n%"n",
directed=nw%n%"directed",
bipartite=nw%n%"bipartite",
loops=nw%n%"loops")
as.rlebdm(free.dyads)
},
free_dyads =
if(is(free.dyads, "rlebdm")) free.dyads
else function()
as.rlebdm(if(is.network(free.dyads)) as.edgelist(free.dyads)
else as.edgelist(free.dyads,
n=nw%n%"n",
directed=nw%n%"directed",
bipartite=nw%n%"bipartite",
loops=nw%n%"loops")
),
dependence = FALSE)
}

Expand Down
2 changes: 1 addition & 1 deletion man/fixallbut-ergmConstraint-ea96b2e0.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 16 additions & 9 deletions tests/testthat/test-constraints.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,25 @@ test_that("fixedas with network input", {
expect_true(all(!sapply(s1,function(x)as.data.frame(t(as.edgelist(absent))) %in% as.data.frame(t(as.edgelist(x))))))
})

test_that("fixallbut with network input", {
net1 <- network(10,directed=FALSE,density=0.5)
free.dyads <- matrix(sample(2:9,8,replace=FALSE),4,2)
net1 <- network(10,directed=FALSE,density=0.5)
fdel <- matrix(sample(2:9,8,replace=FALSE),4,2)

t1 <- ergm(net1~edges, constraint = ~fixallbut(free.dyads = free.dyads))
s1 <- simulate(t1, 100)
for(free.dyads in list(
fdel,
fdnw <- as.network(structure(fdel, n = 10), directed = FALSE),
fd <- as.rlebdm(fdnw)
)){
test_that(sprintf("fixallbut with %s input", class(free.dyads)[1]), {
t1 <- ergm(net1~edges, constraint = ~fixallbut(free.dyads = free.dyads))
s1 <- simulate(t1, 100)

fixed.dyads <- as.edgelist(!update(net1,free.dyads,matrix.type="edgelist"))
fixed.dyads.state <- net1[fixed.dyads]
fixed.dyads <- as.edgelist(!update(net1,fdel,matrix.type="edgelist"))
fixed.dyads.state <- net1[fixed.dyads]

expect_true(all(sapply(s1,function(x) all.equal(x[fixed.dyads],fixed.dyads.state))))
})
}

expect_true(all(sapply(s1,function(x) all.equal(x[fixed.dyads],fixed.dyads.state))))
})

test_that("constraint conflict is detected", {
data(florentine)
Expand Down

0 comments on commit e71e4fc

Please sign in to comment.