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.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  48.193% of 8192 proposed steps.
## SAN summary statistics:
##                      edges                    degree0 
##                       3195                       1113 
##                    degree1       mix.identity.msm.msm 
##                       2199                          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                        101 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                        278                        331 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                        218                        351 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                        172                        324 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                        481                        428 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                        511                        565 
##       mix.race.White.Black       mix.race.Black.Black 
##                        632                        426 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                        693                        509 
## mix.race.Hispanic.Hispanic 
##                        370 
## 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]  -130.00000   274.18421    44.39473 -2777.57688  -166.77518
##  [6]   -19.94456     0.00000  -184.53928     0.00000  -100.65729
## [11]    88.06702  -320.86886   218.00000   114.16998  -137.52045
## [16]   324.00000   481.00000   195.85964  -276.87025    23.37101
## [21]   386.26096  -431.57917   314.36126   418.72848  -126.49323
## New statistics scaling =
##  [1] 0.02869715 0.06040529 0.02007849 0.01957396 0.01957396 0.01957396
##  [7] 0.01957396 0.01957396 0.01957396 0.15884464 0.05511463 0.04857241
## [13] 0.05767377 0.04504002 0.10257355 0.03563637 0.02622357 0.03402426
## [19] 0.03496066 0.02832223 0.01957396 0.03919900 0.02159292 0.02655125
## [25] 0.03947204
## Scaled Mahalanobis distance = 211598.315923159
## #2 of 4:
## SAN Metropolis-Hastings accepted  27.700% of 17408 proposed steps.
## SAN summary statistics:
##                      edges                    degree0 
##                       3339                        795 
##                    degree1       mix.identity.msm.msm 
##                       2097                          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                        247 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                        246                        688 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                         91                        305 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                        366                        132 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                        137                        305 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                        822                        649 
##       mix.race.White.Black       mix.race.Black.Black 
##                        405                        909 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                        533                        250 
## mix.race.Hispanic.Hispanic 
##                        593 
## 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]    14.00000   -43.81579   -57.60527 -2777.57688  -166.77518
##  [6]   -19.94456     0.00000  -184.53928     0.00000    45.34271
## [11]    56.06702    36.13114    91.00000    68.16998    56.47955
## [16]   132.00000   137.00000    72.85964    34.12975   107.37101
## [21]   159.26096    51.42083   154.36126   159.72848    96.50677
## New statistics scaling =
##  [1] 0.01887423 0.05110824 0.01395073 0.01395073 0.01395073 0.01395073
##  [7] 0.01395073 0.01395073 0.01395073 0.12390522 0.06666908 0.04507374
## [13] 0.09433830 0.05040252 0.07294739 0.05989546 0.04414356 0.04448050
## [19] 0.03976380 0.03131724 0.02720901 0.03217704 0.02758583 0.03316524
## [25] 0.03928851
## Scaled Mahalanobis distance = 109703.29317582
## #3 of 4:
## SAN Metropolis-Hastings accepted  21.892% of 34816 proposed steps.
## SAN summary statistics:
##                      edges                    degree0 
##                       3177                        841 
##                    degree1       mix.identity.msm.msm 
##                       2160                          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                        256 
##  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 
##                         57                        293 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                        365                         58 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                         60                        290 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                        845                        634 
##       mix.race.White.Black       mix.race.Black.Black 
##                        343                        949 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                        475                        187 
## mix.race.Hispanic.Hispanic 
##                        589 
## 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     2.184210     5.394735 -2777.576884  -166.775180
##  [6]   -19.944564     0.000000  -184.539276     0.000000    54.342713
## [11]    57.067018    54.131140    57.000000    56.169977    55.479547
## [16]    58.000000    60.000000    57.859639    57.129746    92.371009
## [21]    97.260964    91.420828    96.361256    96.728476    92.506771
## New statistics scaling =
##  [1] 0.02699081 0.06842869 0.01751413 0.01751413 0.01751413 0.01751413
##  [7] 0.01751413 0.01751413 0.01751413 0.09025133 0.06055240 0.03847262
## [13] 0.09973926 0.04714080 0.06818121 0.07215723 0.04841551 0.04063017
## [19] 0.03060359 0.03115669 0.02944298 0.02978256 0.02706391 0.03493857
## [25] 0.03345279
## Scaled Mahalanobis distance = 136217.187690217
## #4 of 4:
## SAN Metropolis-Hastings accepted   0.056% of 69632 proposed steps.
## SAN summary statistics:
##                      edges                    degree0 
##                       3174                        839 
##                    degree1       mix.identity.msm.msm 
##                       2155                          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                        708 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                         56                        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                        635 
##       mix.race.White.Black       mix.race.Black.Black 
##                        340                        952 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                        473                        184 
## 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]  -151.000000     0.184210     0.394735 -2777.576884  -166.775180
##  [6]   -19.944564     0.000000  -184.539276     0.000000    56.342713
## [11]    56.067018    56.131140    56.000000    56.169977    56.479547
## [16]    57.000000    57.000000    56.859639    56.129746    93.371009
## [21]    94.260964    94.420828    94.361256    93.728476    93.506771
## New statistics scaling =
##  [1] 0.02523285 0.05906940 0.01696120 0.01696120 0.01696120 0.01696120
##  [7] 0.01696120 0.01696120 0.01696120 0.09402871 0.05643041 0.03766932
## [13] 0.10649135 0.05083558 0.06099647 0.07561060 0.05173740 0.04600315
## [19] 0.03114238 0.03268255 0.02580873 0.02592061 0.02920382 0.03857442
## [25] 0.03383383
## Scaled Mahalanobis distance = 131910.788011507
## 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 618 rows.
## Maximizing the pseudolikelihood.
## Finished MPLE.
## Starting Monte Carlo maximum likelihood estimation (MCMLE):
## Density guard set to 63751 from an initial count of 3174 edges.
## 
## Iteration 1 of at most 500 with parameter:
##                      edges                    degree0 
##                -10.9524149                 -4.8897242 
##                    degree1       mix.identity.msm.msm 
##                 -2.1350850                  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.1981090 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                 -0.8303186                  0.9785100 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                       -Inf                 -0.5435985 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##                  0.4040808                       -Inf 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                       -Inf                 -1.1033704 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##                  0.0000000                 -0.5541800 
##       mix.race.White.Black       mix.race.Black.Black 
##                 -0.9856249                  1.4634982 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##                 -1.2180623                 -1.3143495 
## mix.race.Hispanic.Hispanic 
##                  0.0000000 
## Starting unconstrained MCMC...
## Back from unconstrained MCMC.
## Average estimating function values:
##                      edges                    degree0 
##                 -198.45801                   39.84534 
##                    degree1       mix.identity.msm.msm 
##                    6.50704                -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                   78.23139 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                   72.99573                   62.88505 
##  mix.age_group.25-34.35-44  mix.age_group.35-44.35-44 
##                   66.62213                   74.26080 
##  mix.age_group.35-44.45-64  mix.age_group.45-64.45-64 
##                   79.00417                   82.64830 
##       mix.race.White.White       mix.race.White.Black 
##                   86.25675                  106.99827 
##       mix.race.Black.Black    mix.race.White.Hispanic 
##                   53.43059                   89.82805 
##    mix.race.Black.Hispanic mix.race.Hispanic.Hispanic 
##                   93.66891                   86.00872 
## Starting MCMLE Optimization...
## Optimizing with step length 0.00574419914642821.
## Using lognormal metric (see control.ergm function).
## Using log-normal approx (no optim)
## The log-likelihood improved by 12.23.
## 
## Iteration 2 of at most 500 with parameter:
##                      edges                    degree0 
##              -4.974382e+00              -4.839849e+00 
##                    degree1       mix.identity.msm.msm 
##              -2.111492e+00              -8.962179e-13 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##               8.290435e-12               5.776338e-12 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                       -Inf              -1.528315e-12 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                       -Inf              -7.142393e+00 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##              -7.831376e+00              -5.975633e+00 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                       -Inf              -7.483627e+00 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##              -6.502442e+00                       -Inf 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                       -Inf              -8.030991e+00 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##              -6.920371e+00               4.399702e-01 
##       mix.race.White.Black       mix.race.Black.Black 
##              -2.361523e-02               2.494891e+00 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##              -2.043396e-01              -3.353282e-01 
## mix.race.Hispanic.Hispanic 
##               9.977370e-01 
## Starting unconstrained MCMC...
## Back from unconstrained MCMC.
## Average estimating function values:
##                      edges                    degree0 
##                -198.132812                  46.652960 
##                    degree1       mix.identity.msm.msm 
##                  -2.150187               -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                  87.207948 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                  51.116822                  66.768835 
##  mix.age_group.25-34.35-44  mix.age_group.35-44.35-44 
##                  64.225641                  81.087946 
##  mix.age_group.35-44.45-64  mix.age_group.45-64.45-64 
##                  71.328389                  95.311386 
##       mix.race.White.White       mix.race.White.Black 
##                  88.519447                  86.317605 
##       mix.race.Black.Black    mix.race.White.Hispanic 
##                  75.404226                  91.290943 
##    mix.race.Black.Hispanic mix.race.Hispanic.Hispanic 
##                  89.525351                  85.458919 
## Starting MCMLE Optimization...
## Optimizing with step length 0.44024249746706.
## Using lognormal metric (see control.ergm function).
## Using log-normal approx (no optim)
## The log-likelihood improved by 3.057.
## 
## Iteration 3 of at most 500 with parameter:
##                      edges                    degree0 
##              -5.176573e+00              -5.178702e+00 
##                    degree1       mix.identity.msm.msm 
##              -2.255027e+00              -8.964750e-13 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##               8.290047e-12               5.776833e-12 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                       -Inf              -1.528748e-12 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                       -Inf              -7.276748e+00 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##              -7.803094e+00              -5.912920e+00 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                       -Inf              -7.474048e+00 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##              -6.537929e+00                       -Inf 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                       -Inf              -8.075933e+00 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##              -7.008353e+00               3.645399e-01 
##       mix.race.White.Black       mix.race.Black.Black 
##              -1.982514e-02               2.537183e+00 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##              -2.917777e-01              -3.525799e-01 
## mix.race.Hispanic.Hispanic 
##               9.295829e-01 
## Starting unconstrained MCMC...
## Back from unconstrained MCMC.
## Average estimating function values:
##                      edges                    degree0 
##                 -182.36719                   18.34827 
##                    degree1       mix.identity.msm.msm 
##                   10.64473                -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                   79.84271 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                   68.83655                   77.04911 
##  mix.age_group.25-34.35-44  mix.age_group.35-44.35-44 
##                   72.24224                   74.58404 
##  mix.age_group.35-44.45-64  mix.age_group.45-64.45-64 
##                   78.68190                   81.57603 
##       mix.race.White.White       mix.race.White.Black 
##                   88.06828                  101.31663 
##       mix.race.Black.Black    mix.race.White.Hispanic 
##                   75.61907                   88.94817 
##    mix.race.Black.Hispanic mix.race.Hispanic.Hispanic 
##                   89.81637                   88.51361 
## Starting MCMLE Optimization...
## Optimizing with step length 0.806201977527474.
## Using lognormal metric (see control.ergm function).
## Using log-normal approx (no optim)
## The log-likelihood improved by 2.555.
## 
## Iteration 4 of at most 500 with parameter:
##                      edges                    degree0 
##              -5.399374e+00              -5.529908e+00 
##                    degree1       mix.identity.msm.msm 
##              -2.425656e+00              -8.969096e-13 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##               8.289876e-12               5.777460e-12 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                       -Inf              -1.528525e-12 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                       -Inf              -7.380850e+00 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##              -7.781753e+00              -5.885590e+00 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                       -Inf              -7.476746e+00 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##              -6.579381e+00                       -Inf 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                       -Inf              -8.132155e+00 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##              -7.075352e+00               2.922044e-01 
##       mix.race.White.Black       mix.race.Black.Black 
##              -7.321493e-02               2.566574e+00 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##              -3.519415e-01              -3.678120e-01 
## mix.race.Hispanic.Hispanic 
##               8.785117e-01 
## Starting unconstrained MCMC...
## Back from unconstrained MCMC.
## Average estimating function values:
##                      edges                    degree0 
##                -175.078125                   9.926398 
##                    degree1       mix.identity.msm.msm 
##                  -7.596476               -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                  69.997987 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                  79.527955                  82.323523 
##  mix.age_group.25-34.35-44  mix.age_group.35-44.35-44 
##                  71.529352                  77.294977 
##  mix.age_group.35-44.45-64  mix.age_group.45-64.45-64 
##                  77.877217                  81.550644 
##       mix.race.White.White       mix.race.White.Black 
##                  92.515541                  85.259987 
##       mix.race.Black.Black    mix.race.White.Hispanic 
##                  90.591726                  96.351490 
##    mix.race.Black.Hispanic mix.race.Hispanic.Hispanic 
##                  91.086875                  83.765560 
## 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.636.
## Step length converged once. Increasing MCMC sample size.
## 
## Iteration 5 of at most 500 with parameter:
##                      edges                    degree0 
##              -5.376750e+00              -5.517903e+00 
##                    degree1       mix.identity.msm.msm 
##              -2.412497e+00              -8.970059e-13 
##       mix.identity.msm.tgw       mix.identity.tgw.tgw 
##               8.289914e-12               5.777451e-12 
##       mix.identity.msm.hrh       mix.identity.tgw.hrh 
##                       -Inf              -1.528556e-12 
##       mix.identity.hrh.hrh  mix.age_group.18-24.18-24 
##                       -Inf              -7.321663e+00 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##              -7.787995e+00              -5.909175e+00 
##  mix.age_group.18-24.35-44  mix.age_group.25-34.35-44 
##                       -Inf              -7.450932e+00 
##  mix.age_group.35-44.35-44  mix.age_group.18-24.45-64 
##              -6.569966e+00                       -Inf 
##  mix.age_group.25-34.45-64  mix.age_group.35-44.45-64 
##                       -Inf              -8.144472e+00 
##  mix.age_group.45-64.45-64       mix.race.White.White 
##              -7.104998e+00               2.746534e-01 
##       mix.race.White.Black       mix.race.Black.Black 
##              -6.294477e-02               2.583394e+00 
##    mix.race.White.Hispanic    mix.race.Black.Hispanic 
##              -3.630103e-01              -3.629922e-01 
## mix.race.Hispanic.Hispanic 
##               8.978471e-01 
## Starting unconstrained MCMC...
## Back from unconstrained MCMC.
## Average estimating function values:
##                      edges                    degree0 
##                -164.216797                  -4.098993 
##                    degree1       mix.identity.msm.msm 
##                   1.847860               -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                  83.866395 
##  mix.age_group.18-24.25-34  mix.age_group.25-34.25-34 
##                  79.424195                  77.230993 
##  mix.age_group.25-34.35-44  mix.age_group.35-44.35-44 
##                  76.732477                  81.102106 
##  mix.age_group.35-44.45-64  mix.age_group.45-64.45-64 
##                  74.156025                  78.450790 
##       mix.race.White.White       mix.race.White.Black 
##                  90.709388                  89.732155 
##       mix.race.Black.Black    mix.race.White.Hispanic 
##                  94.901541                  89.945729 
##    mix.race.Black.Hispanic mix.race.Hispanic.Hispanic 
##                  89.303427                  95.840267 
## 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.3318.
## 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] 3135
degreedist(casual.net.xn) /network.size(casual.net.xn)
## degree0 degree1 degree2 degree3 degree4 
##     869    2139    1849     139       4
## degree0 degree1 degree2 degree3 degree4 
##  0.1738  0.4278  0.3698  0.0278  0.0008
mixingmatrix(casual.net.xn, "identity")
## Note:  Marginal totals can be misleading
##  for undirected mixing matrices.
##      HRH  MSM  TGW
## HRH    0   54    5
## MSM   54 2780  288
## TGW    5  288    8
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   270   280     0     0
## 25-34   280   687   329     0
## 35-44     0   329   396   315
## 45-64     0     0   315   858
mixingmatrix(casual.net.xn, "race")
## Note:  Marginal totals can be misleading
##  for undirected mixing matrices.
##          Black Hispanic White
## Black      920      184   353
## Hispanic   184      578   487
## White      353      487   613
save.image(file="initial-casual-network.RData")