# Model differential dissolution rates between TGW and MSM
# Reference: https://rdrr.io/cran/EpiModel/man/dissolution_coefs.html


# Conceptual explanation -----------

## See model-differential-dissolution-rates.R for conceptual explanation.

rm(list=ls())

# Data and libraries -----------

library(EpiModel)
## Loading required package: deSolve
## Loading required package: networkDynamic
## Loading required package: network
## network: Classes for Relational Data
## Version 1.16.0 created on 2019-11-30.
## 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.
## 
## networkDynamic: version 0.10.1, created on 2020-01-16
## Copyright (c) 2020, 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").
## Loading required package: tergm
## Loading required package: ergm
## 
## 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.
## 
## 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(file="initial-casual-network.RData")


# Add differential dissolution structure -----------

durs.cas <- c(casual.pt.duration, casual.pt.duration, 217, 217)
dissolution.het.cas <- 
  ~offset(edges)+
  offset(nodefactor("identity", levels=c("MSM", "TGW", "HRH")))

theta.casual.hetdiss.coefs <- 
  dissolution_coefs(dissolution = dissolution.het.cas, 
                  duration = durs.cas,
                  d.rate = d.rate)

casual.fit <- ergm(
  formation.casual.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  46.704% of 8192 proposed steps.
## SAN summary statistics:
##                      edges                    degree0 
##                       3179                       1131 
##                    degree1       mix.identity.msm.msm 
##                       2197                          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                        103 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                        313                        329 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                        193                        398 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                        162                        271 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                        441                        442 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                        527                        550 
##       mix.race.White.Black       mix.race.Black.Black 
##                        625                        403 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                        689                        500 
## mix.race.Hispanic.Hispanic 
##                        412 
## Meanstats Goal:
##                      edges                    degree0 
##                 3325.00000                  838.81579 
##                    degree1       mix.identity.msm.msm 
##                 2154.60527                 2777.57688 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                  166.77518                   19.94456 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                    0.00000                  184.53928 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                    0.00000                  201.65729 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                  189.93298                  651.86886 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                    0.00000                  236.83002 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                  309.52045                    0.00000 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                    0.00000                  232.14036 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                  787.87025                  541.62899 
##       mix.race.White.Black       mix.race.Black.Black 
##                  245.73904                  857.57917 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                  378.63874                   90.27152 
## mix.race.Hispanic.Hispanic 
##                  496.49323 
## Difference: SAN target.stats - Goal target.stats =
##  [1]  -146.000000   292.184210    42.394735 -2777.576884  -166.775180
##  [6]   -19.944564     0.000000  -184.539276     0.000000   -98.657287
## [11]   123.067018  -322.868860   193.000000   161.169977  -147.520453
## [16]   271.000000   441.000000   209.859639  -260.870254     8.371009
## [21]   379.260964  -454.579172   310.361256   409.728476   -84.493229
## New statistics scaling =
##  [1] 0.02711156 0.05701316 0.01919618 0.01919618 0.01919618 0.01919618
##  [7] 0.01919618 0.01919618 0.01919618 0.14874690 0.04913386 0.06308055
## [13] 0.05963009 0.04098345 0.11069226 0.04126743 0.03137965 0.03443314
## [19] 0.03698899 0.02355875 0.02162201 0.03904761 0.02026959 0.02347713
## [25] 0.03719065
## Scaled Mahalanobis distance = 210180.357691096
## #2 of 4:
## SAN Metropolis-Hastings accepted  28.211% of 17408 proposed steps.
## SAN summary statistics:
##                      edges                    degree0 
##                       3303                        917 
##                    degree1       mix.identity.msm.msm 
##                       1952                          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                        235 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                        244                        682 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                         69                        302 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                        357                        135 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                        139                        304 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                        836                        613 
##       mix.race.White.Black       mix.race.Black.Black 
##                        407                        912 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                        556                        252 
## mix.race.Hispanic.Hispanic 
##                        563 
## Meanstats Goal:
##                      edges                    degree0 
##                 3325.00000                  838.81579 
##                    degree1       mix.identity.msm.msm 
##                 2154.60527                 2777.57688 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                  166.77518                   19.94456 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                    0.00000                  184.53928 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                    0.00000                  201.65729 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                  189.93298                  651.86886 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                    0.00000                  236.83002 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                  309.52045                    0.00000 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                    0.00000                  232.14036 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                  787.87025                  541.62899 
##       mix.race.White.Black       mix.race.Black.Black 
##                  245.73904                  857.57917 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                  378.63874                   90.27152 
## mix.race.Hispanic.Hispanic 
##                  496.49323 
## Difference: SAN target.stats - Goal target.stats =
##  [1]   -22.00000    78.18421  -202.60527 -2777.57688  -166.77518   -19.94456
##  [7]     0.00000  -184.53928     0.00000    33.34271    54.06702    30.13114
## [13]    69.00000    65.16998    47.47955   135.00000   139.00000    71.85964
## [19]    48.12975    71.37101   161.26096    54.42083   177.36126   161.72848
## [25]    66.50677
## New statistics scaling =
##  [1] 0.01945416 0.04867684 0.01396712 0.01396712 0.01396712 0.01396712
##  [7] 0.01396712 0.01396712 0.01396712 0.11764728 0.06316118 0.04184109
## [13] 0.10121790 0.05316420 0.07779761 0.06582862 0.04004395 0.04596360
## [19] 0.03564083 0.03216765 0.02880665 0.03491789 0.02548610 0.03151638
## [25] 0.03889824
## Scaled Mahalanobis distance = 110297.763030266
## #3 of 4:
## SAN Metropolis-Hastings accepted  20.896% of 34816 proposed steps.
## SAN summary statistics:
##                      edges                    degree0 
##                       3182                        841 
##                    degree1       mix.identity.msm.msm 
##                       2153                          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                        258 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                        246                        707 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                         57                        294 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                        367                         59 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                         59                        290 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                        845                        637 
##       mix.race.White.Black       mix.race.Black.Black 
##                        342                        948 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                        476                        188 
## mix.race.Hispanic.Hispanic 
##                        591 
## Meanstats Goal:
##                      edges                    degree0 
##                 3325.00000                  838.81579 
##                    degree1       mix.identity.msm.msm 
##                 2154.60527                 2777.57688 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                  166.77518                   19.94456 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                    0.00000                  184.53928 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                    0.00000                  201.65729 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                  189.93298                  651.86886 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                    0.00000                  236.83002 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                  309.52045                    0.00000 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                    0.00000                  232.14036 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                  787.87025                  541.62899 
##       mix.race.White.Black       mix.race.Black.Black 
##                  245.73904                  857.57917 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                  378.63874                   90.27152 
## mix.race.Hispanic.Hispanic 
##                  496.49323 
## Difference: SAN target.stats - Goal target.stats =
##  [1]  -143.000000     2.184210    -1.605265 -2777.576884  -166.775180
##  [6]   -19.944564     0.000000  -184.539276     0.000000    56.342713
## [11]    56.067018    55.131140    57.000000    57.169977    57.479547
## [16]    59.000000    59.000000    57.859639    57.129746    95.371009
## [21]    96.260964    90.420828    97.361256    97.728476    94.506771
## New statistics scaling =
##  [1] 0.02485258 0.06426584 0.01663728 0.01663728 0.01663728 0.01663728
##  [7] 0.01663728 0.01663728 0.01663728 0.09766307 0.06255694 0.03806564
## [13] 0.10643064 0.04631475 0.06092895 0.07899323 0.04711507 0.04577161
## [19] 0.02829239 0.03051258 0.02815892 0.02870592 0.02524595 0.03748772
## [25] 0.03217721
## Scaled Mahalanobis distance = 129398.171082153
## #4 of 4:
## SAN Metropolis-Hastings accepted   0.059% of 69632 proposed steps.
## SAN summary statistics:
##                      edges                    degree0 
##                       3176                        838 
##                    degree1       mix.identity.msm.msm 
##                       2154                          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                        258 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                        247                        708 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                         57                        293 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                        366                         57 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                         57                        289 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                        844                        636 
##       mix.race.White.Black       mix.race.Black.Black 
##                        340                        952 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                        473                        184 
## mix.race.Hispanic.Hispanic 
##                        591 
## Meanstats Goal:
##                      edges                    degree0 
##                 3325.00000                  838.81579 
##                    degree1       mix.identity.msm.msm 
##                 2154.60527                 2777.57688 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                  166.77518                   19.94456 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                    0.00000                  184.53928 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                    0.00000                  201.65729 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                  189.93298                  651.86886 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                    0.00000                  236.83002 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                  309.52045                    0.00000 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                    0.00000                  232.14036 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                  787.87025                  541.62899 
##       mix.race.White.Black       mix.race.Black.Black 
##                  245.73904                  857.57917 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                  378.63874                   90.27152 
## mix.race.Hispanic.Hispanic 
##                  496.49323 
## Difference: SAN target.stats - Goal target.stats =
##  [1]  -149.000000    -0.815790    -0.605265 -2777.576884  -166.775180
##  [6]   -19.944564     0.000000  -184.539276     0.000000    56.342713
## [11]    57.067018    56.131140    57.000000    56.169977    56.479547
## [16]    57.000000    57.000000    56.859639    56.129746    94.371009
## [21]    94.260964    94.420828    94.361256    93.728476    94.506771
## New statistics scaling =
##  [1] 0.02657542 0.06295376 0.01726501 0.01726501 0.01726501 0.01726501
##  [7] 0.01726501 0.01726501 0.01726501 0.09433818 0.06071501 0.04198055
## [13] 0.10961473 0.04665773 0.06239862 0.07054211 0.05184223 0.04523866
## [19] 0.02877757 0.03059569 0.02831088 0.02692607 0.02593316 0.03360490
## [25] 0.03213967
## Scaled Mahalanobis distance = 134273.58301984
## 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 609 rows.
## Maximizing the pseudolikelihood.
## Finished MPLE.
## Starting Monte Carlo maximum likelihood estimation (MCMLE):
## Density guard set to 63792 from an initial count of 3176 edges.
## 
## Iteration 1 of at most 500 with parameter:
##                      edges                    degree0 
##                -11.3154780                 -5.0401271 
##                    degree1       mix.identity.msm.msm 
##                 -2.2222592                  0.0000000 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                  0.0000000                  0.0000000 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                       -Inf                  0.0000000 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                       -Inf                  0.2621872 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                 -0.5715195                  1.0068788 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                       -Inf                 -0.4876294 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                  0.5002263                       -Inf 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                       -Inf                 -1.0593519 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                  0.0000000                 -0.4421731 
##       mix.race.White.Black       mix.race.Black.Black 
##                 -0.8122079                  1.6938801 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                 -1.1628621                 -1.1712807 
## mix.race.Hispanic.Hispanic 
##                  0.0000000 
## Starting unconstrained MCMC...
## Back from unconstrained MCMC.
## Average estimating function values:
##                      edges                    degree0 
##                -204.993164                  45.705694 
##                    degree1       mix.identity.msm.msm 
##                  -3.386515               -2777.576884 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                -166.775180                 -19.944564 
##       mix.identity.tgw.hrh  mix.age_group.18-24.18-24 
##                -184.539276                  85.737245 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                  63.926393                  66.772741 
##  mix.age_group.25-34.35-44  mix.age_group.35-44.35-44 
##                  66.861384                  70.717828 
##  mix.age_group.35-44.45-64  mix.age_group.45-64.45-64 
##                  75.029561                  81.103378 
##       mix.race.White.White       mix.race.White.Black 
##                  88.800697                  99.566628 
##       mix.race.Black.Black    mix.race.White.Hispanic 
##                  51.013601                  90.544850 
##    mix.race.Black.Hispanic mix.race.Hispanic.Hispanic 
##                  99.895468                  79.834896 
## Starting MCMLE Optimization...
## Optimizing with step length 0.00504583622842381.
## Using lognormal metric (see control.ergm function).
## Using log-normal approx (no optim)
## The log-likelihood did not improve.
## 
## Iteration 2 of at most 500 with parameter:
##                      edges                    degree0 
##               2.183209e+00              -4.989236e+00 
##                    degree1       mix.identity.msm.msm 
##              -2.177554e+00               4.313363e-12 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##               3.368542e-11              -9.767786e-12 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                       -Inf              -7.947813e-12 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                       -Inf              -1.541109e+01 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##              -1.626767e+01              -1.464320e+01 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                       -Inf              -1.619437e+01 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##              -1.517124e+01                       -Inf 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                       -Inf              -1.681680e+01 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##              -1.566018e+01               1.771524e+00 
##       mix.race.White.Black       mix.race.Black.Black 
##               1.480995e+00               4.020775e+00 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##               1.041032e+00               1.080455e+00 
## mix.race.Hispanic.Hispanic 
##               2.209263e+00 
## Starting unconstrained MCMC...
## Back from unconstrained MCMC.
## Average estimating function values:
##                      edges                    degree0 
##                -187.539062                  32.786749 
##                    degree1       mix.identity.msm.msm 
##                   2.152547               -2777.576884 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                -166.775180                 -19.944564 
##       mix.identity.tgw.hrh  mix.age_group.18-24.18-24 
##                -184.539276                  89.686463 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                  63.862916                  86.379187 
##  mix.age_group.25-34.35-44  mix.age_group.35-44.35-44 
##                  53.824274                  84.064508 
##  mix.age_group.35-44.45-64  mix.age_group.45-64.45-64 
##                  49.349873                 100.473496 
##       mix.race.White.White       mix.race.White.Black 
##                  83.575111                 113.703347 
##       mix.race.Black.Black    mix.race.White.Hispanic 
##                  70.511648                  85.450123 
##    mix.race.Black.Hispanic mix.race.Hispanic.Hispanic 
##                  96.474570                  77.395442 
## Starting MCMLE Optimization...
## Optimizing with step length 0.42646177944529.
## Using lognormal metric (see control.ergm function).
## Using log-normal approx (no optim)
## The log-likelihood improved by 2.569.
## 
## Iteration 3 of at most 500 with parameter:
##                      edges                    degree0 
##               2.018194e+00              -5.255862e+00 
##                    degree1       mix.identity.msm.msm 
##              -2.304729e+00               4.313571e-12 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##               3.368500e-11              -9.768123e-12 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                       -Inf              -7.946961e-12 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                       -Inf              -1.553071e+01 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##              -1.630178e+01              -1.465251e+01 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                       -Inf              -1.613676e+01 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##              -1.517574e+01                       -Inf 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                       -Inf              -1.678723e+01 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##              -1.574484e+01               1.708337e+00 
##       mix.race.White.Black       mix.race.Black.Black 
##               1.440536e+00               4.061665e+00 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##               9.828796e-01               1.050848e+00 
## mix.race.Hispanic.Hispanic 
##               2.194763e+00 
## Starting unconstrained MCMC...
## Back from unconstrained MCMC.
## Average estimating function values:
##                      edges                    degree0 
##                -180.162109                  22.642218 
##                    degree1       mix.identity.msm.msm 
##                  -7.045695               -2777.576884 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                -166.775180                 -19.944564 
##       mix.identity.tgw.hrh  mix.age_group.18-24.18-24 
##                -184.539276                  82.935487 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                  68.112916                  78.422156 
##  mix.age_group.25-34.35-44  mix.age_group.35-44.35-44 
##                  71.190485                  82.401422 
##  mix.age_group.35-44.45-64  mix.age_group.45-64.45-64 
##                  63.990498                  87.964706 
##       mix.race.White.White       mix.race.White.Black 
##                  86.294838                 102.844948 
##       mix.race.Black.Black    mix.race.White.Hispanic 
##                  80.566336                  78.263600 
##    mix.race.Black.Hispanic mix.race.Hispanic.Hispanic 
##                  93.377890                  93.139583 
## Starting MCMLE Optimization...
## Optimizing with step length 0.892761437522729.
## Using lognormal metric (see control.ergm function).
## Using log-normal approx (no optim)
## The log-likelihood improved by 3.012.
## 
## Iteration 4 of at most 500 with parameter:
##                      edges                    degree0 
##               1.825790e+00              -5.568277e+00 
##                    degree1       mix.identity.msm.msm 
##              -2.449548e+00               4.312988e-12 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##               3.368552e-11              -9.768014e-12 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                       -Inf              -7.947382e-12 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                       -Inf              -1.565512e+01 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##              -1.629262e+01              -1.463588e+01 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                       -Inf              -1.611640e+01 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##              -1.520153e+01                       -Inf 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                       -Inf              -1.678247e+01 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##              -1.583794e+01               1.629745e+00 
##       mix.race.White.Black       mix.race.Black.Black 
##               1.371653e+00               4.113583e+00 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##               9.622609e-01               1.009781e+00 
## mix.race.Hispanic.Hispanic 
##               2.159602e+00 
## Starting unconstrained MCMC...
## Back from unconstrained MCMC.
## Average estimating function values:
##                      edges                    degree0 
##                -161.114258                  -3.223017 
##                    degree1       mix.identity.msm.msm 
##                  -4.981242               -2777.576884 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                -166.775180                 -19.944564 
##       mix.identity.tgw.hrh  mix.age_group.18-24.18-24 
##                -184.539276                  78.624940 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                  79.285768                  80.705359 
##  mix.age_group.25-34.35-44  mix.age_group.35-44.35-44 
##                  80.917048                  74.586969 
##  mix.age_group.35-44.45-64  mix.age_group.45-64.45-64 
##                  88.572529                  71.372910 
##       mix.race.White.White       mix.race.White.Black 
##                  86.287025                  85.232644 
##       mix.race.Black.Black    mix.race.White.Hispanic 
##                 101.337820                  95.413990 
##    mix.race.Black.Hispanic mix.race.Hispanic.Hispanic 
##                  84.989218                 100.274349 
## 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 1.019.
## Step length converged once. Increasing MCMC sample size.
## 
## Iteration 5 of at most 500 with parameter:
##                      edges                    degree0 
##               1.823996e+00              -5.560942e+00 
##                    degree1       mix.identity.msm.msm 
##              -2.444630e+00               4.313010e-12 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##               3.368553e-11              -9.768009e-12 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                       -Inf              -7.947387e-12 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                       -Inf              -1.566710e+01 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##              -1.631796e+01              -1.465749e+01 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                       -Inf              -1.612012e+01 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##              -1.515284e+01                       -Inf 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                       -Inf              -1.681036e+01 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##              -1.579788e+01               1.672186e+00 
##       mix.race.White.Black       mix.race.Black.Black 
##               1.388800e+00               4.079808e+00 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##               9.605246e-01               1.025719e+00 
## mix.race.Hispanic.Hispanic 
##               2.117794e+00 
## Starting unconstrained MCMC...
## Back from unconstrained MCMC.
## Average estimating function values:
##                      edges                    degree0 
##                -170.345459                   4.120245 
##                    degree1       mix.identity.msm.msm 
##                  -4.099406               -2777.576884 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                -166.775180                 -19.944564 
##       mix.identity.tgw.hrh  mix.age_group.18-24.18-24 
##                -184.539276                  75.017030 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                  75.299195                  74.179724 
##  mix.age_group.25-34.35-44  mix.age_group.35-44.35-44 
##                  79.004206                  85.861627 
##  mix.age_group.35-44.45-64  mix.age_group.45-64.45-64 
##                  70.523457                  84.949081 
##       mix.race.White.White       mix.race.White.Black 
##                  94.508461                  91.655984 
##       mix.race.Black.Black    mix.race.White.Hispanic 
##                  90.916922                  94.268238 
##    mix.race.Black.Hispanic mix.race.Hispanic.Hispanic 
##                  92.333212                  80.621028 
## 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.
## The log-likelihood improved by 0.6957.
## 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.casual.form <- casual.fit$coef 
theta.casual.form[1] <- theta.casual.form[1] - theta.casual.hetdiss.coefs$coef.adj[1]

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


casual.net.xn <- network.collapse(sim.test, at=20000)
network.size(casual.net.xn)
## [1] 5000
network.edgecount(casual.net.xn)
## [1] 3073
degreedist(casual.net.xn)/network.size(casual.net.xn)
## degree0 degree1 degree2 degree3 degree4 
##     902    2180    1794     118       6
## degree0 degree1 degree2 degree3 degree4 
##  0.1804  0.4360  0.3588  0.0236  0.0012
mixingmatrix(casual.net.xn, "identity")
## Note:  Marginal totals can be misleading
##  for undirected mixing matrices.
##      HRH  MSM  TGW
## HRH    0   54    3
## MSM   54 2797  213
## TGW    3  213    6
mixingmatrix(casual.net.xn, "age_group")
## Note:  Marginal totals can be misleading
##  for undirected mixing matrices.
##       18-24 25-34 35-44 45-64
## 18-24   275   254     0     0
## 25-34   254   704   338     0
## 35-44     0   338   366   304
## 45-64     0     0   304   832
mixingmatrix(casual.net.xn, "race")
## Note:  Marginal totals can be misleading
##  for undirected mixing matrices.
##          Black Hispanic White
## Black      909      194   346
## Hispanic   194      539   470
## White      346      470   615
saveRDS(theta.casual.hetdiss.coefs, file="theta.casual.hetdiss.coefs.RDS")