rm(list=ls())

library(ergm)
## 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.
## 
## 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.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").
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.casual.net <- update.formula(formation, casual.net~.)

casual.net <- net

casual_mean_degree <- 1.33
casual_mean_nedges <- n*casual_mean_degree/2

casual.degprop.0 <- 0.167763158 
casual.degprop.1 <- 0.430921053
casual.degprop.2 <- 0.401315789

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

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

casual.msm.msm.prop <- 0.835361469
casual.tgw.tgw.prop <- 0.005998365
casual.msm.tgw.prop <- (0.054821125+0.045494773)/2
casual.msm.hrh.prop <- 0
casual.tgw.hrh.prop <- 0.055500534
casual.hrh.hrh.prop <- 0

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

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

casual.nodemix.identity <- casual_mean_nedges*(c(casual.msm.msm.prop, 
                                             casual.msm.tgw.prop, casual.tgw.tgw.prop,
                                             casual.msm.hrh.prop, casual.tgw.hrh.prop, casual.hrh.hrh.prop
                                             ))


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

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



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  47.302% of 8192 proposed steps.
## SAN summary statistics:
##                      edges                    degree0 
##                       3173                       1154 
##                    degree1       mix.identity.msm.msm 
##                       2167                          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                         96 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                        304                        322 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                        200                        391 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                        165                        304 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                        453                        430 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                        508                        560 
##       mix.race.White.Black       mix.race.Black.Black 
##                        605                        397 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                        692                        490 
## mix.race.Hispanic.Hispanic 
##                        429 
## 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]  -152.00000   315.18421    12.39473 -2777.57688  -166.77518   -19.94456
##  [7]     0.00000  -184.53928     0.00000  -105.65729   114.06702  -329.86886
## [13]   200.00000   154.16998  -144.52045   304.00000   453.00000   197.85964
## [19]  -279.87025    18.37101   359.26096  -460.57917   313.36126   399.72848
## [25]   -67.49323
## New statistics scaling =
##  [1] 0.02795049 0.06022544 0.02069044 0.01930735 0.01930735 0.01930735
##  [7] 0.01930735 0.01930735 0.01930735 0.14246354 0.04290364 0.06238788
## [13] 0.06049941 0.04172072 0.10385497 0.03993485 0.02521813 0.03357416
## [19] 0.04517837 0.03019194 0.02001020 0.04743611 0.01930735 0.02539464
## [25] 0.03521364
## Scaled Mahalanobis distance = 210709.326380314
## #2 of 4:
## SAN Metropolis-Hastings accepted  28.125% of 17408 proposed steps.
## SAN summary statistics:
##                      edges                    degree0 
##                       3295                        879 
##                    degree1       mix.identity.msm.msm 
##                       2016                          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                        248 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                        264                        682 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                         78                        295 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                        357                         97 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                        151                        317 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                        806                        658 
##       mix.race.White.Black       mix.race.Black.Black 
##                        400                        907 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                        509                        235 
## mix.race.Hispanic.Hispanic 
##                        586 
## 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]   -30.00000    40.18421  -138.60527 -2777.57688  -166.77518   -19.94456
##  [7]     0.00000  -184.53928     0.00000    46.34271    74.06702    30.13114
## [13]    78.00000    58.16998    47.47955    97.00000   151.00000    84.85964
## [19]    18.12975   116.37101   154.26096    49.42083   130.36126   144.72848
## [25]    89.50677
## New statistics scaling =
##  [1] 0.01942945 0.05266556 0.01461267 0.01461267 0.01461267 0.01461267
##  [7] 0.01461267 0.01461267 0.01461267 0.12435834 0.05830459 0.04429824
## [13] 0.10601657 0.04935205 0.07746370 0.06640937 0.04093393 0.04277005
## [19] 0.03292543 0.03173893 0.02476718 0.03172956 0.02349641 0.03490258
## [25] 0.03614939
## Scaled Mahalanobis distance = 114905.569095983
## #3 of 4:
## SAN Metropolis-Hastings accepted  22.418% of 34816 proposed steps.
## SAN summary statistics:
##                      edges                    degree0 
##                       3185                        843 
##                    degree1       mix.identity.msm.msm 
##                       2147                          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                        259 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                        247                        706 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                         58                        296 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                        365                         57 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                         61                        292 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                        844                        636 
##       mix.race.White.Black       mix.race.Black.Black 
##                        344                        952 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                        477                        186 
## mix.race.Hispanic.Hispanic 
##                        590 
## 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]  -140.000000     4.184210    -7.605265 -2777.576884  -166.775180
##  [6]   -19.944564     0.000000  -184.539276     0.000000    57.342713
## [11]    57.067018    54.131140    58.000000    59.169977    55.479547
## [16]    57.000000    61.000000    59.859639    56.129746    94.371009
## [21]    98.260964    94.420828    98.361256    95.728476    93.506771
## New statistics scaling =
##  [1] 0.02478205 0.06637811 0.01715840 0.01715840 0.01715840 0.01715840
##  [7] 0.01715840 0.01715840 0.01715840 0.08960174 0.05580076 0.04123883
## [13] 0.10733328 0.04803185 0.06621328 0.07287780 0.04731961 0.04644026
## [19] 0.02906566 0.02933677 0.02958717 0.02589605 0.02708918 0.03740103
## [25] 0.03549775
## Scaled Mahalanobis distance = 133452.145232984
## #4 of 4:
## SAN Metropolis-Hastings accepted   0.080% of 69632 proposed steps.
## SAN summary statistics:
##                      edges                    degree0 
##                       3177                        838 
##                    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 
##                        247                        709 
##  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                        185 
## 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]  -148.000000    -0.815790    -1.605265 -2777.576884  -166.775180
##  [6]   -19.944564     0.000000  -184.539276     0.000000    56.342713
## [11]    57.067018    57.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    94.728476    94.506771
## New statistics scaling =
##  [1] 0.02424008 0.05765795 0.01568054 0.01568054 0.01568054 0.01568054
##  [7] 0.01568054 0.01568054 0.01568054 0.09812965 0.05404560 0.03628285
## [13] 0.11823285 0.04831788 0.06335544 0.08639861 0.04983349 0.04685578
## [19] 0.03006900 0.02884023 0.02641301 0.02530161 0.02698596 0.03638204
## [25] 0.03289421
## Scaled Mahalanobis distance = 121950.818834369
## 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 616 rows.
## Maximizing the pseudolikelihood.
## Finished MPLE.
## Starting Monte Carlo maximum likelihood estimation (MCMLE):
## Density guard set to 63812 from an initial count of 3177 edges.
## 
## Iteration 1 of at most 500 with parameter:
##                      edges                    degree0 
##                -11.2444274                 -4.9804771 
##                    degree1       mix.identity.msm.msm 
##                 -2.1769630                  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.2797174 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                 -0.5410597                  1.0423672 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                       -Inf                 -0.4750810 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                  0.4965671                       -Inf 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                       -Inf                 -1.0729202 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                  0.0000000                 -0.4850644 
##       mix.race.White.Black       mix.race.Black.Black 
##                 -0.8173001                  1.6945548 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                 -1.1861896                 -1.1841609 
## mix.race.Hispanic.Hispanic 
##                  0.0000000 
## Starting unconstrained MCMC...
## Back from unconstrained MCMC.
## Average estimating function values:
##                      edges                    degree0 
##                 -194.64355                   32.30628 
##                    degree1       mix.identity.msm.msm 
##                   10.22970                -2777.57688 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                 -166.77518                  -19.94456 
##       mix.identity.tgw.hrh  mix.age_group.18-24.18-24 
##                 -184.53928                   80.57709 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                   80.01624                   69.61649 
##  mix.age_group.25-34.35-44  mix.age_group.35-44.35-44 
##                   67.66802                   73.51861 
##  mix.age_group.35-44.45-64  mix.age_group.45-64.45-64 
##                   68.66237                   80.41978 
##       mix.race.White.White       mix.race.White.Black 
##                   86.38566                  110.77757 
##       mix.race.Black.Black    mix.race.White.Hispanic 
##                   49.79387                   86.30657 
##    mix.race.Black.Hispanic mix.race.Hispanic.Hispanic 
##                  100.40328                   86.33880 
## Starting MCMLE Optimization...
## Optimizing with step length 0.00291778672914711.
## Using lognormal metric (see control.ergm function).
## Using log-normal approx (no optim)
## The log-likelihood improved by 6.305.
## 
## Iteration 2 of at most 500 with parameter:
##                      edges                    degree0 
##              -5.238568e+00              -5.055319e+00 
##                    degree1       mix.identity.msm.msm 
##              -2.213793e+00               1.645232e-11 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##              -4.155624e-12              -1.403690e-11 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                       -Inf               2.299613e-12 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                       -Inf              -6.750397e+00 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##              -7.595490e+00              -5.991722e+00 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                       -Inf              -7.532948e+00 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##              -6.539588e+00                       -Inf 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                       -Inf              -8.148936e+00 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##              -7.061978e+00               4.948895e-01 
##       mix.race.White.Black       mix.race.Black.Black 
##               1.505691e-01               2.733270e+00 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##              -2.121935e-01              -1.342766e-01 
## mix.race.Hispanic.Hispanic 
##               9.954401e-01 
## Starting unconstrained MCMC...
## Back from unconstrained MCMC.
## Average estimating function values:
##                      edges                    degree0 
##                -184.952148                  29.263312 
##                    degree1       mix.identity.msm.msm 
##                  -2.477335               -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                  86.892518 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                  75.848268                  78.299109 
##  mix.age_group.25-34.35-44  mix.age_group.35-44.35-44 
##                  60.044977                  78.396539 
##  mix.age_group.35-44.45-64  mix.age_group.45-64.45-64 
##                  66.735615                  84.010605 
##       mix.race.White.White       mix.race.White.Black 
##                  88.103431                  97.419167 
##       mix.race.Black.Black    mix.race.White.Hispanic 
##                  63.275320                  84.559498 
##    mix.race.Black.Hispanic mix.race.Hispanic.Hispanic 
##                 109.041953                  87.297786 
## Starting MCMLE Optimization...
## Optimizing with step length 0.541391739794384.
## Using lognormal metric (see control.ergm function).
## Using log-normal approx (no optim)
## The log-likelihood improved by 2.871.
## 
## Iteration 3 of at most 500 with parameter:
##                      edges                    degree0 
##              -5.424469e+00              -5.361894e+00 
##                    degree1       mix.identity.msm.msm 
##              -2.351425e+00               1.645247e-11 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##              -4.156796e-12              -1.403741e-11 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                       -Inf               2.299567e-12 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                       -Inf              -6.830755e+00 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##              -7.644278e+00              -5.993680e+00 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                       -Inf              -7.495168e+00 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##              -6.571939e+00                       -Inf 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                       -Inf              -8.143465e+00 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##              -7.127675e+00               4.255018e-01 
##       mix.race.White.Black       mix.race.Black.Black 
##               1.357972e-01               2.827302e+00 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##              -2.763322e-01              -2.070847e-01 
## mix.race.Hispanic.Hispanic 
##               9.366145e-01 
## Starting unconstrained MCMC...
## Back from unconstrained MCMC.
## Average estimating function values:
##                      edges                    degree0 
##                 -169.51562                    9.18128 
##                    degree1       mix.identity.msm.msm 
##                   -2.65507                -2777.57688 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                 -166.77518                  -19.94456 
##       mix.identity.tgw.hrh  mix.age_group.18-24.18-24 
##                 -184.53928                   93.60639 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                   65.44104                   80.01981 
##  mix.age_group.25-34.35-44  mix.age_group.35-44.35-44 
##                   78.17193                   72.80670 
##  mix.age_group.35-44.45-64  mix.age_group.45-64.45-64 
##                   78.11940                   77.49889 
##       mix.race.White.White       mix.race.White.Black 
##                   83.44035                   88.79905 
##       mix.race.Black.Black    mix.race.White.Hispanic 
##                   90.69329                   92.66497 
##    mix.race.Black.Hispanic mix.race.Hispanic.Hispanic 
##                   94.92184                   94.61419 
## Starting MCMLE Optimization...
## Optimizing with step length 0.89004800392375.
## Using lognormal metric (see control.ergm function).
## Using log-normal approx (no optim)
## The log-likelihood improved by 2.344.
## 
## Iteration 4 of at most 500 with parameter:
##                      edges                    degree0 
##              -5.613076e+00              -5.644588e+00 
##                    degree1       mix.identity.msm.msm 
##              -2.487381e+00               1.645146e-11 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##              -4.156485e-12              -1.403749e-11 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                       -Inf               2.299319e-12 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                       -Inf              -6.983313e+00 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##              -7.617396e+00              -5.990856e+00 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                       -Inf              -7.514274e+00 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##              -6.540646e+00                       -Inf 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                       -Inf              -8.179536e+00 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##              -7.169547e+00               3.942995e-01 
##       mix.race.White.Black       mix.race.Black.Black 
##               1.244117e-01               2.846921e+00 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##              -3.218882e-01              -2.507479e-01 
## mix.race.Hispanic.Hispanic 
##               8.601945e-01 
## Starting unconstrained MCMC...
## Back from unconstrained MCMC.
## Average estimating function values:
##                      edges                    degree0 
##                 -153.24414                  -11.16638 
##                    degree1       mix.identity.msm.msm 
##                  -10.29765                -2777.57688 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##                 -166.77518                  -19.94456 
##       mix.identity.tgw.hrh  mix.age_group.18-24.18-24 
##                 -184.53928                   77.88764 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                   90.84827                   78.38016 
##  mix.age_group.25-34.35-44  mix.age_group.35-44.35-44 
##                   66.10259                   86.62213 
##  mix.age_group.35-44.45-64  mix.age_group.45-64.45-64 
##                   75.54519                   86.54967 
##       mix.race.White.White       mix.race.White.Black 
##                  101.43839                   85.08225 
##       mix.race.Black.Black    mix.race.White.Hispanic 
##                  105.69036                   96.71770 
##    mix.race.Black.Hispanic mix.race.Hispanic.Hispanic 
##                   84.37301                   88.10345 
## 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.867.
## Step length converged once. Increasing MCMC sample size.
## 
## Iteration 5 of at most 500 with parameter:
##                      edges                    degree0 
##              -5.549398e+00              -5.538510e+00 
##                    degree1       mix.identity.msm.msm 
##              -2.423052e+00               1.645103e-11 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##              -4.156437e-12              -1.403809e-11 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                       -Inf               2.298949e-12 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                       -Inf              -7.003308e+00 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##              -7.661142e+00              -5.963969e+00 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                       -Inf              -7.452498e+00 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##              -6.557898e+00                       -Inf 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                       -Inf              -8.118675e+00 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##              -7.174400e+00               3.748003e-01 
##       mix.race.White.Black       mix.race.Black.Black 
##               1.501285e-01               2.797430e+00 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##              -2.907448e-01              -2.125147e-01 
## mix.race.Hispanic.Hispanic 
##               8.977704e-01 
## Starting unconstrained MCMC...
## Back from unconstrained MCMC.
## Average estimating function values:
##                      edges                    degree0 
##                -178.008545                   5.554815 
##                    degree1       mix.identity.msm.msm 
##                   8.687215               -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.907167 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                  71.607301                  81.162390 
##  mix.age_group.25-34.35-44  mix.age_group.35-44.35-44 
##                  79.853327                  70.762262 
##  mix.age_group.35-44.45-64  mix.age_group.45-64.45-64 
##                  87.711201                  70.167587 
##       mix.race.White.White       mix.race.White.Black 
##                  77.796058                 107.645974 
##       mix.race.Black.Black    mix.race.White.Hispanic 
##                  74.488943                  93.871998 
##    mix.race.Black.Hispanic mix.race.Hispanic.Hispanic 
##                  93.278281                  89.559505 
## 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 1.541.
## 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.diss

sim.test <- simulate(casual.net,
                            formation=formation.casual.net,
                            dissolution=dissolution,
                            coef.form=theta.casual.form,
                            coef.diss=theta.casual.diss,
                            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] 3130
degreedist(casual.net.xn) /network.size(casual.net.xn)
## degree0 degree1 degree2 degree3 degree4 
##     842    2196    1828     128       6
## degree0 degree1 degree2 degree3 degree4 
##  0.1684  0.4392  0.3656  0.0256  0.0012
mixingmatrix(casual.net.xn, "identity")
## Note:  Marginal totals can be misleading
##  for undirected mixing matrices.
##      HRH  MSM  TGW
## HRH    1   61    5
## MSM   61 2813  244
## TGW    5  244    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   254   276     0     0
## 25-34   276   734   290     0
## 35-44     0   290   391   319
## 45-64     0     0   319   866
mixingmatrix(casual.net.xn, "race")
## Note:  Marginal totals can be misleading
##  for undirected mixing matrices.
##          Black Hispanic White
## Black      931      175   328
## Hispanic   175      592   474
## White      328      474   630
save.image(file="initial-casual-network.RData")