R Under development (unstable) (2025-06-14 r88315 ucrt) -- "Unsuffered Consequences" Copyright (C) 2025 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > # File tests/sim_gf_sum.R in package tergm, 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 2008-2025 Statnet Commons > ################################################################################ > library(tergm) Loading required package: ergm Loading required package: network 'network' 1.19.0 (2024-12-08), part of the Statnet Project * 'news(package="network")' for changes since last version * 'citation("network")' for citation information * 'https://statnet.org' for help, support, and other information 'ergm' 4.9.0 (2025-06-09), part of the Statnet Project * 'news(package="ergm")' for changes since last version * 'citation("ergm")' for citation information * 'https://statnet.org' for help, support, and other information 'ergm' 4 is a major update that introduces some backwards-incompatible changes. Please type 'news(package="ergm")' for a list of major changes. Loading required package: networkDynamic 'networkDynamic' 0.11.5 (2024-11-21), part of the Statnet Project * 'news(package="networkDynamic")' for changes since last version * 'citation("networkDynamic")' for citation information * 'https://statnet.org' for help, support, and other information Registered S3 method overwritten by 'tergm': method from simulate_formula.network ergm 'tergm' 4.2.2 (2025-06-15), part of the Statnet Project * 'news(package="tergm")' for changes since last version * 'citation("tergm")' for citation information * 'https://statnet.org' for help, support, and other information Attaching package: 'tergm' The following object is masked from 'package:ergm': snctrl > > logit<-function(p)log(p/(1-p)) > > coef.form.f<-function(coef.diss,density) -log(((1+exp(coef.diss))/(density/(1-density)))-1) > > simtest <- function(S, edges, dur, n, dir=FALSE, bip=0){ + target.stats<-edges + coef.diss<-logit(1-1/dur) + + dc <- suppressWarnings(matrix(rnorm(n*n),if(bip) bip else n, if(bip) n-bip else n)) + if(!dir && !bip) dc <- dc+t(dc) + # Note that if nw1!=nw2, then + # summary(nw1~edgecov(dc)+edgecov.ages(dc))!=summary(nw2~edgecov(dc)+edgecov.ages(dc)) + # with probability 1. Thus, they offer a sort of a checksum of a network. + + g0<-network.initialize(n, dir=dir, bipartite=bip) + g0 %n% "dc" <- dc + g0 %n% "time" <- 0 + + dyads <- network.dyadcount(g0) + density<-edges/dyads + + coef.form<-coef.form.f(coef.diss,density) + + # Get a reasonably close starting network. + g1<-san(g0~edges,target.stats=target.stats,verbose=TRUE) + + g1 %n% "lasttoggle" <- cbind(as.edgelist(g1), -rgeom(network.edgecount(g1),1/4)) + + print(coef.form) + print(coef.diss) + + # Simulate. Starting from an ordinary network: + dynsim<-simulate(g1 ~ Form(~edges) + Persist(~edges),coef=c(coef.form,coef.diss),time.slices=S,verbose=TRUE,monitor=~edgecov("dc")+edgecov.ages("dc"), dynamic=TRUE) + + sim.stats <- attr(dynsim, "stats") + + # Resuming from a networkDynamic: + dynsim2<-simulate(dynsim ~ Form(~edges) + Persist(~edges),coef=c(coef.form,coef.diss),time.slices=S,verbose=TRUE,monitor=~edgecov("dc")+edgecov.ages("dc"), dynamic=TRUE) + + sim.stats <- rbind(sim.stats, attr(dynsim2, "stats")) + + # Resuming from a resumed networkDynamic: + dynsim3<-simulate(dynsim2 ~ Form(~edges) + Persist(~edges),coef=c(coef.form,coef.diss),time.slices=S,verbose=TRUE,monitor=~edgecov("dc")+edgecov.ages("dc"), dynamic=TRUE) + + sim.stats <- rbind(sim.stats, attr(dynsim3, "stats")) + + # Replay the simulation using a networkDynamic: + gf1.stats <- as.matrix(tergm.godfather(dynsim3~edgecov("dc")+edgecov.ages("dc"), start=0, end=S*3)) + + # Replay the sim using networkDynamic with no explicit time params (should be using net.obs.period) + gf1b.stats <- as.matrix(tergm.godfather(dynsim3~edgecov("dc")+edgecov.ages("dc"))) + + # Replay the simulation using a network + list of changes: + gf2.stats <- as.matrix(tergm.godfather(g1~edgecov("dc")+edgecov.ages("dc"), start=0, end=S*3, changes=attr(dynsim3,"changes"))) + + # Evaluate summary statistics directly: + summ.stats <- summary(dynsim3~edgecov("dc")+edgecov.ages("dc"), at=1:(S*3)) + + # If they aren't all identical, we are in trouble. + stopifnot(all.equal(sim.stats,gf1.stats), + all.equal(sim.stats,gf1b.stats), + all.equal(sim.stats,gf2.stats), + all.equal(sim.stats,summ.stats)) + } > > set.seed(1) > cat("Undirected, unipartite:\n") Undirected, unipartite: > simtest(20, 10, 10, 10, FALSE, FALSE) Initializing unconstrained Metropolis-Hastings proposal: 'ergm:MH_SPDyad'. Initializing model... Model initialized. Starting 4 SAN iterations of 524288 steps each. #1 of 4: Exact match found. SAN Metropolis-Hastings accepted 0.000% of 32768 proposed steps. SAN summary statistics: edges 10 Meanstats Goal: edges 10 Difference: SAN target.stats - Goal target.stats = edges 0 New statistics scaling = [1] 1 Scaled Mahalanobis distance = 0 Target statistics matched exactly. [1] -3.526361 [1] 2.197225 Returned from STERGM burnin extracting state of networkDynamic at time 20 Returned from STERGM burnin Updating networkDynamic with simulated time: ( 2141 ). extracting state of networkDynamic at time 40 Returned from STERGM burnin Updating networkDynamic with simulated time: ( 4161 ). > cat("Directed, unipartite:\n") Directed, unipartite: > simtest(20, 10, 10, 10, TRUE, FALSE) Initializing unconstrained Metropolis-Hastings proposal: 'ergm:MH_SPDyad'. Initializing model... Model initialized. Starting 4 SAN iterations of 524288 steps each. #1 of 4: Exact match found. SAN Metropolis-Hastings accepted 0.000% of 32768 proposed steps. SAN summary statistics: edges 10 Meanstats Goal: edges 10 Difference: SAN target.stats - Goal target.stats = edges 0 New statistics scaling = [1] 1 Scaled Mahalanobis distance = 0 Target statistics matched exactly. [1] -4.369448 [1] 2.197225 Returned from STERGM burnin extracting state of networkDynamic at time 20 Returned from STERGM burnin Updating networkDynamic with simulated time: ( 2141 ). extracting state of networkDynamic at time 40 Returned from STERGM burnin Updating networkDynamic with simulated time: ( 4161 ). > cat("Undirected, biipartite:\n") Undirected, biipartite: > simtest(20, 10, 10, 10, FALSE, 3) Initializing unconstrained Metropolis-Hastings proposal: 'ergm:MH_TNT'. Initializing model... Model initialized. Starting 4 SAN iterations of 524288 steps each. #1 of 4: Exact match found. SAN Metropolis-Hastings accepted 0.000% of 32768 proposed steps. SAN summary statistics: edges 10 Meanstats Goal: edges 10 Difference: SAN target.stats - Goal target.stats = edges 0 New statistics scaling = [1] 1 Scaled Mahalanobis distance = 0 Target statistics matched exactly. [1] -2.302585 [1] 2.197225 Returned from STERGM burnin extracting state of networkDynamic at time 20 Returned from STERGM burnin Updating networkDynamic with simulated time: ( 2141 ). extracting state of networkDynamic at time 40 Returned from STERGM burnin Updating networkDynamic with simulated time: ( 4161 ). > > proc.time() user system elapsed 6.42 0.46 6.87