tests/testthat/test-drop.R

#  File tests/testthat/test-drop.R in package ergm, part of the
#  Statnet suite of packages for network analysis, https://statnet.org .
#
#  This software is distributed under the GPL-3 license.  It is free,
#  open source, and has the attribution requirements (GPL Section 7) at
#  https://statnet.org/attribution .
#
#  Copyright 2003-2023 Statnet Commons
################################################################################


library(statnet.common)
opttest({
data(sampson)

# Just one covariate. Note that the .mcmc tests mainly test detection
# and overriding of control$force.main. Note that 1/2 has been subtracted from the "maxed" matrices. This is to test detection of non-0-1 extremeness.
test_that("one covariate", {
  samplike.m <- as.matrix(samplike, matrix.type="adjacency")

  maxed.mple <- ergm(samplike~edgecov(samplike.m-1/2))
  expect_equal(coef(maxed.mple), Inf)

  maxed.mcmc <- ergm(samplike~edgecov(samplike.m-1/2),control=control.ergm(force.main=TRUE))
  expect_equal(coef(maxed.mcmc), Inf)

  mined.mple <- ergm(samplike~edgecov(-samplike.m))
  expect_equal(coef(mined.mple), -Inf)

  mined.mcmc <- ergm(samplike~edgecov(-samplike.m),control=control.ergm(force.main=TRUE))
  expect_equal(coef(mined.mcmc), -Inf)
})

# Now, blank out some of the 1s in the matrix so that you still have a
# dropped term, but now multiple parameters are meaningful.
test_that("multiple covariates", {
  samplike.m <- as.matrix(samplike, matrix.type="adjacency")
  samplike.m[4:10,4:10] <- 0

  truth <- c(logit((network.edgecount(samplike)-sum(samplike.m))/(network.dyadcount(samplike)-sum(samplike.m))),Inf)

  maxed.mple <- ergm(samplike~edges+edgecov(samplike.m))
  expect_true(all.equal(truth, coef(maxed.mple),check.attributes=FALSE))

  maxed.mcmc <- ergm(samplike~edges+edgecov(samplike.m), control=control.ergm(force.main=TRUE, MCMLE.maxit=10))
  expect_true(all.equal(truth, coef(maxed.mcmc), check.attributes=FALSE,tolerance=0.1))


  truth <- c(logit((network.edgecount(samplike)-sum(samplike.m))/(network.dyadcount(samplike)-sum(samplike.m))),-Inf)

  mined.mple <- ergm(samplike~edges+edgecov(-samplike.m))
  expect_true(all.equal(truth, coef(mined.mple),check.attributes=FALSE))

  mined.mcmc <- ergm(samplike~edges+edgecov(-samplike.m), control=control.ergm(force.main=TRUE, MCMLE.maxit=10))
  expect_true(all.equal(truth, coef(mined.mcmc), check.attributes=FALSE, tolerance=0.1))
})

# This is mainly to make sure it doesn't crash for dyad-dependent
# and curved terms.
crash.test <- function() {
  set.seed(1)
  y <- network.initialize(10, directed=FALSE)
  y[1,2]<-y[2,3]<-y[3,4]<-1
  dummy <- ergm(y~edges+triangles+degree(2)+kstar(2)+kstar(5)+gwdegree,
                control=control.ergm(MCMLE.maxit=3), verbose=TRUE) # It doesn't seem to stop for a while.
}

test_that("doesn't crash for dyad-dependent and curved terms", {
  capture_warnings(expect_error(crash.test(), NA))
})

}, "drop")
statnet/ergm documentation built on April 17, 2024, 12:21 p.m.