# File tests/testthat/test-term-valued.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 ################################################################################ tst <- function(truth, fmla){ test <- summary(fmla, response="w") expect_equal(test, truth, ignore_attr=TRUE) } n <- 129 n1 <- 100 n2 <- n-n1 nz <- 60 pint <- .5 f <- rep(letters[1:3], length.out=n) f1 <- f[1:n1] f2 <- f[-(1:n1)] q <- rep(3:1, length.out=n) q1 <- q[1:n1] q2 <- q[-(1:n1)] # a bipartite nw set.seed(143) b1 <- floor(runif(nz, 1,n1+1)) b2 <- floor(runif(nz, n1+1, n+1)) exbip.el <- unique(cbind(b1,b2)) v <- runif(nrow(exbip.el), -4, 4) v <- ifelse(rbinom(length(v),1,pint), round(v), v) exbip.el <- cbind(exbip.el,v) attr(exbip.el, "n") <- n bipnw <- as.network(exbip.el, matrix.type="edgelist", bipartite=n1, directed=FALSE, ignore.eval=FALSE, names.eval="w") bipnw %v% "f" <- f bipnw %v% "q" <- q bipnw %n% "e" <- bipe <- matrix(rnorm(n1*n2), n1,n2) bipm <- as.matrix(bipnw, a="w") bipvt <- c(0, sort(v)[6], sort(v)[length(v)-6], runif(1, -4, 4)) bippnw <- bipnw bippnw %e% "w" <- abs(bippnw %e% "w") bippm <- as.matrix(bippnw, a="w") # a directed nw set.seed(143) t <- floor(runif(nz, 1, n+1)) h <- floor(runif(nz, 1, n+1)) exdir.el <- unique(cbind(t,h)) v <- runif(nrow(exdir.el), -4, 4) v <- ifelse(rbinom(length(v),1,pint), round(v), v) exdir.el <- cbind(exdir.el,v) attr(exdir.el, "n") <- n dirnw <- as.network(exdir.el, matrix.type="edgelist", directed=TRUE, ignore.eval=FALSE, names.eval="w") dirnw %v% "f" <- f dirnw %v% "q" <- q dirnw %n% "e" <- dire <- matrix(rnorm(n*n), n,n) dirm <- as.matrix(dirnw, a="w") diag(dirm) <- NA dirvt <- c(0, sort(v)[6], sort(v)[length(v)-6], runif(1, -4, 4)) dirpnw <- dirnw dirpnw %e% "w" <- abs(dirpnw %e% "w") dirpm <- as.matrix(dirpnw, a="w") diag(dirpm) <- NA # an undirected nw set.seed(143) t <- floor(runif(nz, 1, n+1)) h <- floor(runif(nz, 1, n+1)) exund.el <- t(apply(unique(cbind(t,h)),1,range)) v <- runif(nrow(exund.el), -4, 4) v <- ifelse(rbinom(length(v),1,pint), round(v), v) exund.el <- cbind(exund.el,v) attr(exund.el, "n") <- n undnw <- as.network(exund.el, matrix.type="edgelist", directed=FALSE, ignore.eval=FALSE, names.eval="w") undnw %v% "f" <- f undnw %v% "q" <- q undnw %n% "e" <- matrix(rnorm(n*n), n,n) undnw %n% "e" <- unde <- undnw %n% "e" + t(undnw %n% "e") undm <- as.matrix(undnw, a="w") diag(undm) <- NA undvt <- c(0, sort(v)[6], sort(v)[length(v)-6], runif(1, -4, 4)) undpnw <- undnw undpnw %e% "w" <- abs(undpnw %e% "w") undpm <- as.matrix(undpnw, a="w") diag(undpm) <- NA test_that("absdiff", { tst(sum(abs(outer(q,q,"-"))*dirm,na.rm=TRUE), dirnw ~ absdiff("q")) tst(sum(abs(outer(q,q,"-")^2)*dirm,na.rm=TRUE), dirnw ~ absdiff(~q,pow=2)) tst(sum(abs(outer(q,q,"-"))*(dirm!=0),na.rm=TRUE), dirnw ~ absdiff(function(x) x %v% "q", form="nonzero")) tst(sum(abs(outer(q,q,"-")^2)*(dirm!=0),na.rm=TRUE), dirnw ~ absdiff("q",pow=2, form="nonzero")) tst(sum(abs(outer(q,q,"-"))*dirm,na.rm=TRUE), dirnw ~ B(~absdiff("q"), form="sum")) tst(sum(abs(outer(q,q,"-")^2)*dirm,na.rm=TRUE), dirnw ~ B(~absdiff("q",pow=2), form="sum")) tst(sum(abs(outer(q,q,"-"))*(dirm!=0),na.rm=TRUE), dirnw ~ B(~absdiff("q"), form="nonzero")) tst(sum(abs(outer(q,q,"-")^2)*(dirm!=0),na.rm=TRUE), dirnw ~ B(~absdiff("q",pow=2), form="nonzero")) tst(sum(abs(outer(q,q,"-"))*(dirm>.5 & dirm<1),na.rm=TRUE), dirnw ~ B(~absdiff("q"), form=~ininterval(.5,1))) tst(sum(abs(outer(q,q,"-")^2)*(dirm>.5 & dirm<1),na.rm=TRUE), dirnw ~ B(~absdiff("q",pow=2), form=~ininterval(.5,1))) tst(sum(abs(outer(q,q,"-"))*undm,na.rm=TRUE)/2, undnw ~ absdiff("q")) tst(sum(abs(outer(q,q,"-")^2)*undm,na.rm=TRUE)/2, undnw ~ absdiff(~q,pow=2)) tst(sum(abs(outer(q,q,"-"))*(undm!=0),na.rm=TRUE)/2, undnw ~ absdiff(function(x) x %v% "q", form="nonzero")) tst(sum(abs(outer(q,q,"-")^2)*(undm!=0),na.rm=TRUE)/2, undnw ~ absdiff("q",pow=2, form="nonzero")) tst(sum(abs(outer(q,q,"-"))*undm,na.rm=TRUE)/2, undnw ~ B(~absdiff("q"), form="sum")) tst(sum(abs(outer(q,q,"-")^2)*undm,na.rm=TRUE)/2, undnw ~ B(~absdiff("q",pow=2), form="sum")) tst(sum(abs(outer(q,q,"-"))*(undm!=0),na.rm=TRUE)/2, undnw ~ B(~absdiff("q"), form="nonzero")) tst(sum(abs(outer(q,q,"-")^2)*(undm!=0),na.rm=TRUE)/2, undnw ~ B(~absdiff("q",pow=2), form="nonzero")) tst(sum(abs(outer(q,q,"-"))*(undm>.5 & undm<1),na.rm=TRUE)/2, undnw ~ B(~absdiff("q"), form=~ininterval(.5,1))) tst(sum(abs(outer(q,q,"-")^2)*(undm>.5 & undm<1),na.rm=TRUE)/2, undnw ~ B(~absdiff("q",pow=2), form=~ininterval(.5,1))) tst(sum(abs(outer(q1,q2,"-"))*bipm,na.rm=TRUE), bipnw ~ absdiff("q")) tst(sum(abs(outer(q1,q2,"-")^2)*bipm,na.rm=TRUE), bipnw ~ absdiff(~q,pow=2)) tst(sum(abs(outer(q1,q2,"-"))*(bipm!=0),na.rm=TRUE), bipnw ~ absdiff(function(x) x %v% "q", form="nonzero")) tst(sum(abs(outer(q1,q2,"-")^2)*(bipm!=0),na.rm=TRUE), bipnw ~ absdiff("q",pow=2, form="nonzero")) tst(sum(abs(outer(q1,q2,"-"))*bipm,na.rm=TRUE), bipnw ~ B(~absdiff("q"), form="sum")) tst(sum(abs(outer(q1,q2,"-")^2)*bipm,na.rm=TRUE), bipnw ~ B(~absdiff("q",pow=2), form="sum")) tst(sum(abs(outer(q1,q2,"-"))*(bipm!=0),na.rm=TRUE), bipnw ~ B(~absdiff("q"), form="nonzero")) tst(sum(abs(outer(q1,q2,"-")^2)*(bipm!=0),na.rm=TRUE), bipnw ~ B(~absdiff("q",pow=2), form="nonzero")) tst(sum(abs(outer(q1,q2,"-"))*(bipm>.5 & bipm<1),na.rm=TRUE), bipnw ~ B(~absdiff("q"), form=~ininterval(.5,1))) tst(sum(abs(outer(q1,q2,"-")^2)*(bipm>.5 & bipm<1),na.rm=TRUE), bipnw ~ B(~absdiff("q",pow=2), form=~ininterval(.5,1))) }) test_that("absdiffcat", { diffs <- sort(unique(c(abs(outer(q,q,"-"))))) diffs <- diffs[diffs!=0] for(base in c(0, seq_along(diffs))){ keep <- if(all(base==0)) seq_along(diffs) else seq_along(diffs)[-base] tst(sapply(diffs[keep], function(x) sum((abs(outer(q,q,"-"))==x)*dirm,na.rm=TRUE)), dirnw ~ absdiffcat("q",levels=keep)) tst(sapply(diffs[keep], function(x) sum((abs(outer(q,q,"-"))==x)*(dirm!=0),na.rm=TRUE)), dirnw ~ absdiffcat(~q,base=base, form="nonzero")) tst(sapply(diffs[keep], function(x) sum((abs(outer(q,q,"-"))==x)*undm,na.rm=TRUE))/2, undnw ~ absdiffcat("q",levels=keep)) tst(sapply(diffs[keep], function(x) sum((abs(outer(q,q,"-"))==x)*(undm!=0),na.rm=TRUE))/2, undnw ~ absdiffcat(function(x) x %v% "q",levels=keep, form="nonzero")) tst(sapply(diffs[keep], function(x) sum((abs(outer(q1,q2,"-"))==x)*bipm,na.rm=TRUE)), bipnw ~ absdiffcat("q", levels=keep)) tst(sapply(diffs[keep], function(x) sum((abs(outer(q1,q2,"-"))==x)*(bipm!=0),na.rm=TRUE)), bipnw ~ absdiffcat(~q, base=base, form="nonzero")) } }) test_that("atleast", { for(v in dirvt) tst(sum(dirm >= v,na.rm=TRUE), dirnw ~ atleast(v)) tst(sapply(dirvt, function(v) sum(dirm >= v,na.rm=TRUE)), dirnw ~ atleast(dirvt)) for(v in undvt) tst(sum(undm >= v,na.rm=TRUE)/2, undnw ~ atleast(v)) tst(sapply(undvt, function(v) sum(undm >= v,na.rm=TRUE)/2), undnw ~ atleast(undvt)) for(v in bipvt) tst(sum(bipm >= v,na.rm=TRUE), bipnw ~ atleast(v)) tst(sapply(bipvt, function(v) sum(bipm >= v,na.rm=TRUE)), bipnw ~ atleast(bipvt)) }) test_that("atmost", { for(v in dirvt) tst(sum(dirm <= v,na.rm=TRUE), dirnw ~ atmost(v)) tst(sapply(dirvt, function(v) sum(dirm <= v,na.rm=TRUE)), dirnw ~ atmost(dirvt)) for(v in undvt) tst(sum(undm <= v,na.rm=TRUE)/2, undnw ~ atmost(v)) tst(sapply(undvt, function(v) sum(undm <= v,na.rm=TRUE)/2), undnw ~ atmost(undvt)) for(v in bipvt) tst(sum(bipm <= v,na.rm=TRUE), bipnw ~ atmost(v)) tst(sapply(bipvt, function(v) sum(bipm <= v,na.rm=TRUE)), bipnw ~ atmost(bipvt)) }) test_that("b1cov", { tst(sum(q1*bipm,na.rm=TRUE), bipnw ~ b1cov("q")) tst(c(sum(q1*bipm,na.rm=TRUE),sum(q1^2*bipm,na.rm=TRUE)), bipnw ~ b1cov(~poly(q,2,raw=TRUE))) tst(sum(q1*(bipm!=0),na.rm=TRUE), bipnw ~ b1cov(~q, form="nonzero")) tst(c(sum(q1*(bipm!=0),na.rm=TRUE),sum(q1^2*(bipm!=0),na.rm=TRUE)), bipnw ~ b1cov(~poly(q,2,raw=TRUE), form="nonzero")) }) test_that("b1factor", { for(base in list(0, 1, 2, 1:2, 3)){ keep <- if(all(base==0)) 1:3 else (1:3)[-base] tst(sapply(sort(unique(f1))[keep], function(x) sum((f1==x)*bipm,na.rm=TRUE)), bipnw ~ b1factor("f", levels=keep)) tst(sapply(sort(unique(f1))[keep], function(x) sum((f1==x)*(bipm!=0),na.rm=TRUE)), bipnw ~ b1factor(~f, base=base, form="nonzero")) } }) test_that("b1sociality", { for(base in list(0, 1, 2, 1:2, 3)){ keep <- if(all(base==0)) 1:3 else (1:3)[-base] tst(apply(bipm, 1, sum)[keep], bipnw ~ b1sociality(nodes=keep)) tst(apply(bipm!=0, 1, sum)[keep], bipnw ~ b1sociality(nodes=keep, form="nonzero")) } }) test_that("b2cov", { tst(sum(q2*t(bipm),na.rm=TRUE), bipnw ~ b2cov("q")) tst(c(sum(q2*t(bipm),na.rm=TRUE),sum(q2^2*t(bipm),na.rm=TRUE)), bipnw ~ b2cov(~poly(q,2,raw=TRUE))) tst(sum(q2*t(bipm!=0),na.rm=TRUE), bipnw ~ b2cov(function(x) x %v% "q", form="nonzero")) tst(c(sum(q2*t(bipm!=0),na.rm=TRUE),sum(q2^2*t(bipm!=0),na.rm=TRUE)), bipnw ~ b2cov(~poly(q,2,raw=TRUE), form="nonzero")) }) test_that("b2factor", { for(base in list(0, 1, 2, 1:2, 3)){ keep <- if(all(base==0)) 1:3 else (1:3)[-base] tst(sapply(sort(unique(f2))[keep], function(x) sum((f2==x)*t(bipm),na.rm=TRUE)), bipnw ~ b2factor("f", levels=keep)) tst(sapply(sort(unique(f2))[keep], function(x) sum((f2==x)*t(bipm!=0),na.rm=TRUE)), bipnw ~ b2factor(~f, base=base, form="nonzero")) } }) test_that("b2sociality", { for(base in list(0, 1, 2, 1:2, 3)){ keep <- if(all(base==0)) 1:3 else (1:3)[-base] tst(apply(bipm, 2, sum)[keep], bipnw ~ b2sociality(nodes=keep)) tst(apply(bipm!=0, 2, sum)[keep], bipnw ~ b2sociality(nodes=keep, form="nonzero")) } }) test_that("edgecov", { tst(sum(dire*dirm,na.rm=TRUE), dirnw ~ edgecov("e")) tst(sum(dire*(dirm!=0),na.rm=TRUE), dirnw ~ edgecov("e", form="nonzero")) tst(sum(unde*undm,na.rm=TRUE)/2, undnw ~ edgecov("e")) tst(sum(unde*(undm!=0),na.rm=TRUE)/2, undnw ~ edgecov("e", form="nonzero")) tst(sum(bipe*bipm,na.rm=TRUE), bipnw ~ edgecov("e")) tst(sum(bipe*(bipm!=0),na.rm=TRUE), bipnw ~ edgecov("e", form="nonzero")) }) test_that("edges", { tst(sum((dirm!=0),na.rm=TRUE), dirnw ~ edges) tst(sum((undm!=0),na.rm=TRUE)/2, undnw ~ edges) tst(sum((bipm!=0),na.rm=TRUE), bipnw ~ edges) }) test_that("nonzero", { tst(sum((dirm!=0),na.rm=TRUE), dirnw ~ nonzero) tst(sum((undm!=0),na.rm=TRUE)/2, undnw ~ nonzero) tst(sum((bipm!=0),na.rm=TRUE), bipnw ~ nonzero) }) test_that("diff", { posonly <- function(x) pmax(x, 0) negonly <- function(x) pmin(x, 0) for(dd in c("t-h", "h-t")){ for(sa in c("identity", "abs", "posonly", "negonly")){ saf <- get(sa) ddf <- switch(dd, `t-h`=identity, `h-t`=function(x) -x) df <- function(x) saf(ddf(x)) tst(sum(df(outer(q,q,"-"))*dirm,na.rm=TRUE), dirnw ~ diff("q", dir=dd, sign.action=sa)) tst(sum(df(outer(q,q,"-"))^2*dirm,na.rm=TRUE), dirnw ~ diff(~q,pow=2, dir=dd, sign.action=sa)) tst(sum(df(outer(q,q,"-"))*(dirm!=0),na.rm=TRUE), dirnw ~ diff(function(x) x %v% "q", dir=dd, sign.action=sa, form="nonzero")) tst(sum(df(outer(q,q,"-"))^2*(dirm!=0),na.rm=TRUE), dirnw ~ diff("q",pow=2, dir=dd, sign.action=sa, form="nonzero")) tst(sum(df(outer(q,q,"-"))*dirm,na.rm=TRUE), dirnw ~ B(~diff("q", dir=dd, sign.action=sa), form="sum")) tst(sum(df(outer(q,q,"-"))^2*dirm,na.rm=TRUE), dirnw ~ B(~diff("q",pow=2, dir=dd, sign.action=sa), form="sum")) tst(sum(df(outer(q,q,"-"))*(dirm!=0),na.rm=TRUE), dirnw ~ B(~diff("q", dir=dd, sign.action=sa), form="nonzero")) tst(sum(df(outer(q,q,"-"))^2*(dirm!=0),na.rm=TRUE), dirnw ~ B(~diff("q",pow=2, dir=dd, sign.action=sa), form="nonzero")) } } }) test_that("greaterthan", { for(v in dirvt) tst(sum(dirm > v,na.rm=TRUE), dirnw ~ greaterthan(v)) tst(sapply(dirvt, function(v) sum(dirm > v,na.rm=TRUE)), dirnw ~ greaterthan(dirvt)) for(v in undvt) tst(sum(undm > v,na.rm=TRUE)/2, undnw ~ greaterthan(v)) tst(sapply(undvt, function(v) sum(undm > v,na.rm=TRUE)/2), undnw ~ greaterthan(undvt)) for(v in bipvt) tst(sum(bipm > v,na.rm=TRUE), bipnw ~ greaterthan(v)) tst(sapply(bipvt, function(v) sum(bipm > v,na.rm=TRUE)), bipnw ~ greaterthan(bipvt)) }) test_that("equalto", { for(v in dirvt) tst(sum(dirm == v,na.rm=TRUE), dirnw ~ equalto(v)) for(v in undvt) tst(sum(undm == v,na.rm=TRUE)/2, undnw ~ equalto(v)) for(v in bipvt) tst(sum(bipm == v,na.rm=TRUE), bipnw ~ equalto(v)) set.seed(123) for(tol in unique(c(runif(1,0,2), dirvt, undvt, bipvt))){ for(v in dirvt) tst(sum(abs(dirm-v)<=tol,na.rm=TRUE), dirnw ~ equalto(v,tol)) for(v in undvt) tst(sum(abs(undm-v)<=tol,na.rm=TRUE)/2, undnw ~ equalto(v,tol)) for(v in bipvt) tst(sum(abs(bipm-v)<=tol,na.rm=TRUE), bipnw ~ equalto(v,tol)) } }) test_that("ininterval", { charospec <- function(o1, o2) paste0(if(o1)'('else'[',if(o2)')'else']') for(o1 in c(FALSE, TRUE)){ for(o2 in c(FALSE, TRUE)){ for(lv in c(-Inf,dirvt, Inf)) for(uv in c(-Inf,dirvt, Inf)){ truth <- sum(((o1 & dirm>lv) | (!o1 & dirm>=lv)) & ((o2 & dirmlv) | (!o1 & undm>=lv)) & ((o2 & undmlv) | (!o1 & bipm>=lv)) & ((o2 & bipm