Skip to content

Commit

Permalink
Dyads() constraint operator has been moved to InitErgmConstraint.oper…
Browse files Browse the repository at this point in the history
…ator.R.
  • Loading branch information
krivit committed Jan 2, 2025
1 parent b5ddb3a commit 6521938
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 642 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Collate:
'InitErgmConstraint.R'
'InitErgmConstraint.blockdiag.R'
'InitErgmConstraint.hints.R'
'InitErgmConstraint.operator.R'
'InitErgmProposal.R'
'InitErgmProposal.dyadnoise.R'
'InitErgmReference.R'
Expand Down
57 changes: 0 additions & 57 deletions R/InitErgmConstraint.R
Original file line number Diff line number Diff line change
Expand Up @@ -591,60 +591,3 @@ InitErgmConstraint.egocentric <- function(nw, arglist, ...){
dependence = FALSE
)
}

#' @templateVar name Dyads
#' @title Constrain fixed or varying dyad-independent terms
#' @description This is an "operator" constraint that takes one or two [`ergmTerm`] dyad-independent formulas. For the terms in the `vary=` formula, only those that change at least one of the terms will be allowed to vary, and all others will be fixed. If both formulas are given, the dyads that vary either for one or for the other will be allowed to vary. Note that a formula passed to `Dyads` without an argument name will default to `fix=` .
#'
#' @usage
#' # Dyads(fix=NULL, vary=NULL)
#' @param fix,vary formula with only dyad-independent terms
#'
#' @template ergmConstraint-general
#'
#' @concept dyad-independent
#' @concept operator
#' @concept directed
#' @concept undirected
InitErgmConstraint.Dyads<-function(nw, arglist, ...){
a <- check.ErgmTerm(nw, arglist,
varnames = c("fix", "vary"),
vartypes = c("formula", "formula"),
defaultvalues = list(NULL, NULL),
required = c(FALSE, FALSE))
fix <- a$fix; vary <- a$vary

if(is.null(fix) & is.null(vary))
ergm_Init_stop("Dyads constraint takes at least one argument, either ",sQuote("fix")," or ",sQuote("vary")," or both.")

for(f in c(fix, vary)){
f[[3]] <- f[[2]]
f[[2]] <- nw
if(!is.dyad.independent(f)) ergm_Init_stop("Terms passed to the Dyads constraint must be dyad-independent.")
}

list(
free_dyads = function(){
fd <- lapply(list(fix=fix,vary=vary),
function(f){
if(!is.null(f)){
f[[3]] <- f[[2]]
f[[2]] <- nw
m <- ergmMPLE(f, expand.bipartite=TRUE, output="array")$predictor
m <- m!=0
m[is.na(m)] <- FALSE
if(!is.directed(nw)){
m <- m | aperm(m, c(2L,1L,3L))
}
lapply(seq_len(dim(m)[3]), function(i) as.rlebdm(m[,,i]))
}
})
fd$fix <- if(length(fd$fix)) fd$fix %>% map(`!`) %>% reduce(`&`)
fd$vary <- if(length(fd$vary)) fd$vary %>% reduce(`|`)
fd <- Reduce(`|`, fd)

compress(fd)
},
dependence = FALSE
)
}
Loading

0 comments on commit 6521938

Please sign in to comment.