rm(list=ls())

library(ergm)
## Loading required package: network
## network: Classes for Relational Data
## Version 1.15 created on 2019-04-01.
## copyright (c) 2005, Carter T. Butts, University of California-Irvine
##                     Mark S. Handcock, University of California -- Los Angeles
##                     David R. Hunter, Penn State University
##                     Martina Morris, University of Washington
##                     Skye Bender-deMoll, University of Washington
##  For citation information, type citation("network").
##  Type help("network-package") to get started.
## 
## ergm: version 3.10.4, created on 2019-06-10
## Copyright (c) 2019, Mark S. Handcock, University of California -- Los Angeles
##                     David R. Hunter, Penn State University
##                     Carter T. Butts, University of California -- Irvine
##                     Steven M. Goodreau, University of Washington
##                     Pavel N. Krivitsky, University of Wollongong
##                     Martina Morris, University of Washington
##                     with contributions from
##                     Li Wang
##                     Kirk Li, University of Washington
##                     Skye Bender-deMoll, University of Washington
##                     Chad Klumb
## Based on "statnet" project software (statnet.org).
## For license and citation information see statnet.org/attribution
## or type citation("ergm").
## NOTE: Versions before 3.6.1 had a bug in the implementation of the
## bd() constriant which distorted the sampled distribution somewhat.
## In addition, Sampson's Monks datasets had mislabeled vertices. See
## the NEWS and the documentation for more details.
## NOTE: Some common term arguments pertaining to vertex attribute
## and level selection have changed in 3.10.0. See terms help for
## more details. Use 'options(ergm.term=list(version="3.9.4"))' to
## use old behavior.
library(network)
library(networkDynamic)
## 
## networkDynamic: version 0.10.0, created on 2019-04-04
## Copyright (c) 2019, Carter T. Butts, University of California -- Irvine
##                     Ayn Leslie-Cook, University of Washington
##                     Pavel N. Krivitsky, University of Wollongong
##                     Skye Bender-deMoll, University of Washington
##                     with contributions from
##                     Zack Almquist, University of California -- Irvine
##                     David R. Hunter, Penn State University
##                     Li Wang
##                     Kirk Li, University of Washington
##                     Steven M. Goodreau, University of Washington
##                     Jeffrey Horner
##                     Martina Morris, University of Washington
## Based on "statnet" project software (statnet.org).
## For license and citation information see statnet.org/attribution
## or type citation("networkDynamic").
library(tergm)
## 
## tergm: version 3.6.1, created on 2019-06-12
## Copyright (c) 2019, Pavel N. Krivitsky, University of Wollongong
##                     Mark S. Handcock, University of California -- Los Angeles
##                     with contributions from
##                     David R. Hunter, Penn State University
##                     Steven M. Goodreau, University of Washington
##                     Martina Morris, University of Washington
##                     Nicole Bohme Carnegie, New York University
##                     Carter T. Butts, University of California -- Irvine
##                     Ayn Leslie-Cook, University of Washington
##                     Skye Bender-deMoll
##                     Li Wang
##                     Kirk Li, University of Washington
## Based on "statnet" project software (statnet.org).
## For license and citation information see statnet.org/attribution
## or type citation("tergm").
load("init-pop-atts.RData")

formation <- ~edges+degree(0:1)+
  nodemix("identity", levels=c("msm", "tgw", "hrh"))+
  nodemix("age_group", levels = c("18-24", "25-34", "35-44", "45-64"))+
  nodemix("race", levels=c("White", "Black", "Hispanic"))

dissolution <- ~offset(edges)
constraints <- ~.
formation.main.net <- update.formula(formation, main.net~.)

main.net <- net

main_mean_degree <- 0.453947358
main_mean_nedges <- n*main_mean_degree/2

main.degprop.0 <- 0.608552632 
main.degprop.1 <- 0.335526316
main.degprop.2 <- 0.055921053

main.deg.prop <- c(main.degprop.0, main.degprop.1, main.degprop.2)
main.deg.nodes <- n*main.deg.prop

main.pt.duration <- 1303.457818 #by estimation
min.age <- 18; max.age <- 64 #by design
d.rate <- 1/((max.age - min.age)*365) #derived
pg.main <- (main.pt.duration - 1)/main.pt.duration #derived
ps2 <- (1 - d.rate)^2 #derived
theta.main.diss <- log(pg.main/(ps2-pg.main)) #derived

main.msm.msm.prop <- 0.841347695
main.tgw.tgw.prop <- 0.004420998
main.msm.tgw.prop <- (0.026310186+0.063390315)/2
main.msm.hrh.prop <- 0
main.tgw.hrh.prop <- 0.055500534
main.hrh.hrh.prop <- 0

main.18.24.18.24 <- 0.060648808
main.25.34.25.34 <- 0.196050785
main.35.44.35.44 <- 0.093088858
main.45.64.45.64 <- 0.23695346
main.25.34.18.24 <- (0.084626222+0.029619181)/2
main.35.44.18.24 <- 0
main.45.64.18.24 <- 0
main.35.44.25.34 <- (0.080394922+0.062059227)/2
main.45.64.25.34 <- 0
main.45.64.35.44 <- (0.050775745+0.088857555)/2

main.white.white <- 0.162895937
main.black.white <- (0.095022614 + 0.05279034)/2
main.hispanic.white <- (0.122171947 + 0.105580681)/2
main.black.black <- 0.257918548
main.hispanic.black <- mean(c(0.01809956, 0.036199101))
main.hispanic.hispanic <- 0.149321272

main.nodemix.identity <- main_mean_nedges*(c(main.msm.msm.prop, 
                                             main.msm.tgw.prop, main.tgw.tgw.prop,
                                             main.msm.hrh.prop, main.tgw.hrh.prop, main.hrh.hrh.prop
                                             ))

main.nodemix.age <- main_mean_nedges*c(
                                       main.18.24.18.24,
                                       main.25.34.18.24, main.25.34.25.34,
                                       main.35.44.18.24, main.35.44.25.34, main.35.44.35.44,
                                       main.45.64.18.24, main.45.64.25.34, main.45.64.35.44, main.45.64.45.64
                                      )

main.nodemix.race <- main_mean_nedges*c(main.white.white,
                                        main.black.white, main.black.black,
                                        main.hispanic.white, main.hispanic.black, main.hispanic.hispanic)
  
target.stats <- c(main_mean_nedges, main.deg.nodes[1:2], main.nodemix.identity,
                  main.nodemix.age, main.nodemix.race)  

main.fit <- ergm(
  formation.main.net, 
  target.stats=target.stats, 
  constraints=constraints,
  eval.loglik=FALSE,
  verbose=TRUE,
  control=control.ergm(MCMLE.maxit=500)
)
## Evaluating network in model.
## Initializing Metropolis-Hastings proposal(s): ergm:MH_TNT
## Initializing model.
## Warning: `set_attrs()` is deprecated as of rlang 0.3.0
## This warning is displayed once per session.
## Using initial method 'MPLE'.
## Constructing an approximate response network.
## Starting 4 MCMC iterations of 131072 steps each.
## #1 of 4:
## SAN Metropolis-Hastings accepted  41.150% of 8192 proposed steps.
## SAN summary statistics:
##                      edges                    degree0 
##                       1105                       3067 
##                    degree1       mix.identity.msm.msm 
##                       1684                          0 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                          0                          0 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                          0                          0 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                          0                         67 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                         88                        219 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                         38                        106 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                        104                         46 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                         49                        109 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                        279                        218 
##       mix.race.White.Black       mix.race.Black.Black 
##                        129                        304 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                        177                         84 
## mix.race.Hispanic.Hispanic 
##                        193 
## Meanstats Goal:
##                      edges                    degree0 
##                1134.868395                3042.763160 
##                    degree1       mix.identity.msm.msm 
##                1677.631580                 954.818908 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                  50.899132                   5.017251 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                   0.000000                  62.985802 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                   0.000000                  68.828415 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                  64.826749                 222.491840 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                   0.000000                  80.833356 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                 105.643603                   0.000000 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                   0.000000                  79.232710 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                 268.910993                 184.865451 
##       mix.race.White.Black       mix.race.Black.Black 
##                  83.874125                 292.703609 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                 129.234630                  30.810917 
## mix.race.Hispanic.Hispanic 
##                 169.459992 
## Difference: SAN target.stats - Goal target.stats =
##  [1]  -29.868395   24.236840    6.368420 -954.818908  -50.899132
##  [6]   -5.017251    0.000000  -62.985802    0.000000   -1.828415
## [11]   23.173251   -3.491840   38.000000   25.166644   -1.643603
## [16]   46.000000   49.000000   29.767290   10.089007   33.134549
## [21]   45.125875   11.296391   47.765370   53.189083   23.540008
## New statistics scaling =
##  [1] 0.11040407 0.19164752 0.05385711 0.01758843 0.01758843 0.01758843
##  [7] 0.01758843 0.01758843 0.01758843 0.09386251 0.03865531 0.02995053
## [13] 0.05396400 0.03076792 0.05683925 0.03011711 0.02188609 0.02746131
## [19] 0.02507601 0.02212653 0.01758843 0.02494171 0.01898450 0.02086558
## [25] 0.02547396
## Scaled Mahalanobis distance = 16733.5348711232
## #2 of 4:
## SAN Metropolis-Hastings accepted  44.342% of 17408 proposed steps.
## SAN summary statistics:
##                      edges                    degree0 
##                       1166                       2986 
##                    degree1       mix.identity.msm.msm 
##                       1696                          0 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                          0                          0 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                          0                          0 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                          0                         87 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                         90                        240 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                         27                        106 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                        125                         35 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                         47                        112 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                        297                        225 
##       mix.race.White.Black       mix.race.Black.Black 
##                        147                        326 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                        178                         86 
## mix.race.Hispanic.Hispanic 
##                        204 
## Meanstats Goal:
##                      edges                    degree0 
##                1134.868395                3042.763160 
##                    degree1       mix.identity.msm.msm 
##                1677.631580                 954.818908 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                  50.899132                   5.017251 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                   0.000000                  62.985802 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                   0.000000                  68.828415 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                  64.826749                 222.491840 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                   0.000000                  80.833356 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                 105.643603                   0.000000 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                   0.000000                  79.232710 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                 268.910993                 184.865451 
##       mix.race.White.Black       mix.race.Black.Black 
##                  83.874125                 292.703609 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                 129.234630                  30.810917 
## mix.race.Hispanic.Hispanic 
##                 169.459992 
## Difference: SAN target.stats - Goal target.stats =
##  [1]   31.131605  -56.763160   18.368420 -954.818908  -50.899132
##  [6]   -5.017251    0.000000  -62.985802    0.000000   18.171585
## [11]   25.173251   17.508160   27.000000   25.166644   19.356397
## [16]   35.000000   47.000000   32.767290   28.089007   40.134549
## [21]   63.125875   33.296391   48.765370   55.189083   34.540008
## New statistics scaling =
##  [1] 0.12527726 0.20893089 0.05584602 0.01853752 0.01853752 0.01853752
##  [7] 0.01853752 0.01853752 0.01853752 0.05813524 0.03626135 0.02188732
## [13] 0.06700810 0.03123859 0.03946191 0.04222764 0.03013070 0.02689518
## [19] 0.01917693 0.02114623 0.01890109 0.01857471 0.01853752 0.02515435
## [25] 0.02398383
## Scaled Mahalanobis distance = 17311.2978067577
## #3 of 4:
## SAN Metropolis-Hastings accepted  38.445% of 34816 proposed steps.
## SAN summary statistics:
##                      edges                    degree0 
##                       1142                       3013 
##                    degree1       mix.identity.msm.msm 
##                       1690                          0 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                          0                          0 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                          0                          0 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                          0                         91 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                         91                        247 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                         24                        104 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                        124                         26 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                         30                        105 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                        300                        221 
##       mix.race.White.Black       mix.race.Black.Black 
##                        131                        340 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                        171                         72 
## mix.race.Hispanic.Hispanic 
##                        207 
## Meanstats Goal:
##                      edges                    degree0 
##                1134.868395                3042.763160 
##                    degree1       mix.identity.msm.msm 
##                1677.631580                 954.818908 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                  50.899132                   5.017251 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                   0.000000                  62.985802 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                   0.000000                  68.828415 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                  64.826749                 222.491840 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                   0.000000                  80.833356 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                 105.643603                   0.000000 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                   0.000000                  79.232710 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                 268.910993                 184.865451 
##       mix.race.White.Black       mix.race.Black.Black 
##                  83.874125                 292.703609 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                 129.234630                  30.810917 
## mix.race.Hispanic.Hispanic 
##                 169.459992 
## Difference: SAN target.stats - Goal target.stats =
##  [1]    7.131605  -29.763160   12.368420 -954.818908  -50.899132
##  [6]   -5.017251    0.000000  -62.985802    0.000000   22.171585
## [11]   26.173251   24.508160   24.000000   23.166644   18.356397
## [16]   26.000000   30.000000   25.767290   31.089007   36.134549
## [21]   47.125875   47.296391   41.765370   41.189083   37.540008
## New statistics scaling =
##  [1] 0.13245284 0.22435805 0.05996417 0.01680060 0.01680060 0.01680060
##  [7] 0.01680060 0.01680060 0.01680060 0.05918920 0.03380300 0.02246296
## [13] 0.05580900 0.02907355 0.04050898 0.04198943 0.02922202 0.02596659
## [19] 0.02010495 0.02073484 0.01836913 0.01923969 0.01680060 0.02538577
## [25] 0.02376162
## Scaled Mahalanobis distance = 15682.2885552589
## #4 of 4:
## SAN Metropolis-Hastings accepted   0.014% of 69632 proposed steps.
## SAN summary statistics:
##                      edges                    degree0 
##                       1158                       2989 
##                    degree1       mix.identity.msm.msm 
##                       1706                          0 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                          0                          0 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                          0                          0 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                          0                         91 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                         88                        250 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                         21                        113 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                        129                         23 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                         35                        111 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                        297                        230 
##       mix.race.White.Black       mix.race.Black.Black 
##                        132                        336 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                        182                         69 
## mix.race.Hispanic.Hispanic 
##                        209 
## Meanstats Goal:
##                      edges                    degree0 
##                1134.868395                3042.763160 
##                    degree1       mix.identity.msm.msm 
##                1677.631580                 954.818908 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                  50.899132                   5.017251 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                   0.000000                  62.985802 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                   0.000000                  68.828415 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                  64.826749                 222.491840 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                   0.000000                  80.833356 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                 105.643603                   0.000000 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                   0.000000                  79.232710 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                 268.910993                 184.865451 
##       mix.race.White.Black       mix.race.Black.Black 
##                  83.874125                 292.703609 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                 129.234630                  30.810917 
## mix.race.Hispanic.Hispanic 
##                 169.459992 
## Difference: SAN target.stats - Goal target.stats =
##  [1]   23.131605  -53.763160   28.368420 -954.818908  -50.899132
##  [6]   -5.017251    0.000000  -62.985802    0.000000   22.171585
## [11]   23.173251   27.508160   21.000000   32.166644   23.356397
## [16]   23.000000   35.000000   31.767290   28.089007   45.134549
## [21]   48.125875   43.296391   52.765370   38.189083   39.540008
## New statistics scaling =
##  [1] 0.12657416 0.21467785 0.05802342 0.01749891 0.01749891 0.01749891
##  [7] 0.01749891 0.01749891 0.01749891 0.05660119 0.03721288 0.02445139
## [13] 0.06345884 0.03049870 0.03946175 0.04869060 0.02857828 0.02651336
## [19] 0.02034864 0.02039561 0.01749891 0.01829971 0.01796302 0.02175843
## [25] 0.02399978
## Scaled Mahalanobis distance = 16312.2641313
## Finished SAN run.
## Observed statistic(s) mix.identity.msm.hrh, mix.identity.hrh.hrh, mix.age_group.18-24.35-44, mix.age_group.18-24.45-64, and mix.age_group.25-34.45-64 are at their smallest attainable values. Their coefficients will be fixed at -Inf.
## Fitting initial model.
## Unable to match target stats. Using MCMLE estimation.
## Starting maximum pseudolikelihood estimation (MPLE):
## Evaluating the predictor and response matrix.
## MPLE covariate matrix has 503 rows.
## Maximizing the pseudolikelihood.
## Warning in ergm.mple(nw, fd, m, MPLEtype = MPLEtype, init = init, control =
## control, : glm.fit: fitted probabilities numerically 0 or 1 occurred
## Finished MPLE.
## Starting Monte Carlo maximum likelihood estimation (MCMLE):
## Density guard set to 23259 from an initial count of 1158 edges.
## 
## Iteration 1 of at most 500 with parameter:
##                      edges                    degree0 
##               -38.61306926               -30.52349509 
##                    degree1       mix.identity.msm.msm 
##               -15.00224019                 0.00000000 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                 0.00000000                 0.00000000 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                       -Inf                 0.00000000 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                       -Inf                 0.08655923 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                -0.90767435                 0.49193510 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                       -Inf                -0.80512762 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                 0.20811676                       -Inf 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                       -Inf                -1.13245071 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                 0.00000000                -0.37837207 
##       mix.race.White.Black       mix.race.Black.Black 
##                -1.19990330                 0.77339863 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                -1.07652404                -1.70836751 
## mix.race.Hispanic.Hispanic 
##                 0.00000000 
## Starting unconstrained MCMC...
## Back from unconstrained MCMC.
## Average estimating function values:
##                      edges                    degree0 
##                 -32.520739                  36.173363 
##                    degree1       mix.identity.msm.msm 
##                 -40.199939                -954.818908 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                 -50.899132                  -5.017251 
##       mix.identity.tgw.hrh  mix.age_group.18-24.18-24 
##                 -62.985802                  24.403030 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                  25.744540                  32.473981 
##  mix.age_group.25-34.35-44  mix.age_group.35-44.35-44 
##                  35.920551                  24.055616 
##  mix.age_group.35-44.45-64  mix.age_group.45-64.45-64 
##                  35.299517                  33.682757 
##       mix.race.White.White       mix.race.White.Black 
##                  35.550565                  44.285055 
##       mix.race.Black.Black    mix.race.White.Hispanic 
##                  27.161626                  42.512441 
##    mix.race.Black.Hispanic mix.race.Hispanic.Hispanic 
##                  31.645138                  30.244109 
## Starting MCMLE Optimization...
## Optimizing with step length 0.804254898619941.
## Using lognormal metric (see control.ergm function).
## Using log-normal approx (no optim)
## The log-likelihood improved by 2.96.
## 
## Iteration 2 of at most 500 with parameter:
##                      edges                    degree0 
##              -3.867261e+01              -3.050661e+01 
##                    degree1       mix.identity.msm.msm 
##              -1.491694e+01              -5.700756e-18 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##              -3.796334e-17               1.271376e-17 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                       -Inf              -6.115015e-18 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                       -Inf               9.917056e-02 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##              -8.653248e-01               5.005248e-01 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                       -Inf              -8.641688e-01 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##               2.429071e-01                       -Inf 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                       -Inf              -1.207096e+00 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##              -2.419334e-02              -4.139466e-01 
##       mix.race.White.Black       mix.race.Black.Black 
##              -1.256023e+00               8.202344e-01 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##              -1.120522e+00              -1.679528e+00 
## mix.race.Hispanic.Hispanic 
##               4.784169e-04 
## Starting unconstrained MCMC...
## Back from unconstrained MCMC.
## Average estimating function values:
##                      edges                    degree0 
##                 -38.437731                  20.559106 
##                    degree1       mix.identity.msm.msm 
##                   2.862561                -954.818908 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                 -50.899132                  -5.017251 
##       mix.identity.tgw.hrh  mix.age_group.18-24.18-24 
##                 -62.985802                  28.316116 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                  28.781650                  32.014996 
##  mix.age_group.25-34.35-44  mix.age_group.35-44.35-44 
##                  26.204730                  29.785108 
##  mix.age_group.35-44.45-64  mix.age_group.45-64.45-64 
##                  26.366900                  34.193499 
##       mix.race.White.White       mix.race.White.Black 
##                  31.455838                  36.628805 
##       mix.race.Black.Black    mix.race.White.Hispanic 
##                  37.365727                  38.506581 
##    mix.race.Black.Hispanic mix.race.Hispanic.Hispanic 
##                  32.282833                  29.242156 
## Starting MCMLE Optimization...
## Optimizing with step length 1.
## Using lognormal metric (see control.ergm function).
## Using log-normal approx (no optim)
## The log-likelihood improved by 0.5203.
## Step length converged once. Increasing MCMC sample size.
## 
## Iteration 3 of at most 500 with parameter:
##                      edges                    degree0 
##              -3.868600e+01              -3.050297e+01 
##                    degree1       mix.identity.msm.msm 
##              -1.489742e+01              -6.694784e-18 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##              -4.731440e-17               1.720988e-17 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                       -Inf              -2.104258e-17 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                       -Inf               1.017160e-01 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##              -8.617807e-01               4.894919e-01 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                       -Inf              -8.414678e-01 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##               2.236171e-01                       -Inf 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                       -Inf              -1.189303e+00 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##              -5.384762e-02              -4.038720e-01 
##       mix.race.White.Black       mix.race.Black.Black 
##              -1.286936e+00               8.040664e-01 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##              -1.156221e+00              -1.654046e+00 
## mix.race.Hispanic.Hispanic 
##               3.430845e-02 
## Starting unconstrained MCMC...
## Back from unconstrained MCMC.
## Average estimating function values:
##                      edges                    degree0 
##                 -45.480700                  26.184838 
##                    degree1       mix.identity.msm.msm 
##                   5.697033                -954.818908 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                 -50.899132                  -5.017251 
##       mix.identity.tgw.hrh  mix.age_group.18-24.18-24 
##                 -62.985802                  27.995803 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                  29.956943                  27.656842 
##  mix.age_group.25-34.35-44  mix.age_group.35-44.35-44 
##                  29.513080                  27.768751 
##  mix.age_group.35-44.45-64  mix.age_group.45-64.45-64 
##                  29.299517                  26.429095 
##       mix.race.White.White       mix.race.White.Black 
##                  34.049833                  33.352926 
##       mix.race.Black.Black    mix.race.White.Hispanic 
##                  30.799565                  30.643056 
##    mix.race.Black.Hispanic mix.race.Hispanic.Hispanic 
##                  33.294796                  36.298797 
## Starting MCMLE Optimization...
## Optimizing with step length 1.
## Using lognormal metric (see control.ergm function).
## Using log-normal approx (no optim)
## Starting MCMC s.e. computation.
## Error in solve.default(H, tol = 1e-20) : 
##   Lapack routine dgesv: system is exactly singular: U[16,16] = 0
## Warning in ergm.MCMCse.lognormal(theta = theta, init = init, statsmatrix =
## statsmatrix0, : Approximate Hessian matrix is singular. Standard errors due
## to MCMC approximation of the likelihood cannot be evaluated. This is likely
## due to insufficient MCMC sample size or highly correlated model terms.
## The log-likelihood improved by 0.09839.
## Step length converged twice. Stopping.
## Finished MCMLE.
## This model was fit using MCMC.  To examine model diagnostics and
## check for degeneracy, use the mcmc.diagnostics() function.
theta.main.form <- main.fit$coef 
theta.main.form[1] <- theta.main.form[1] - theta.main.diss

sim.test <- simulate(main.net,
                            formation=formation.main.net,
                            dissolution=dissolution,
                            coef.form=theta.main.form,
                            coef.diss=theta.main.diss,
                            time.slices=2e4,
                            #time.slices=1e2,
                            constraints=constraints,
                            monitor=~edges+degree(0:5)
                            )


main.net.xn <- network.collapse(sim.test, at=20000)
network.size(main.net.xn)
## [1] 5000
network.edgecount(main.net.xn)
## [1] 1072
degreedist(main.net.xn)/network.size(main.net.xn)
## degree0 degree1 degree2 
##    3078    1700     222
## degree0 degree1 degree2 
##  0.6156  0.3400  0.0444
mixingmatrix(main.net.xn, "identity")
## Note:  Marginal totals can be misleading
##  for undirected mixing matrices.
##     HRH MSM TGW
## HRH   0  16   1
## MSM  16 951  99
## TGW   1  99   5
mixingmatrix(main.net.xn, "age_group")
## Note:  Marginal totals can be misleading
##  for undirected mixing matrices.
##       18-24 25-34 35-44 45-64
## 18-24   109    97     0     0
## 25-34    97   229   101     0
## 35-44     0   101   137   107
## 45-64     0     0   107   292
mixingmatrix(main.net.xn, "race")
## Note:  Marginal totals can be misleading
##  for undirected mixing matrices.
##          Black Hispanic White
## Black      330       62   121
## Hispanic    62      206   159
## White      121      159   194
save.image(file="initial-main-network.RData")