if (FALSE) {
# Network initialization and model parameterization
nw <- network_initialize(n = 500)
nw <- set_vertex_attribute(nw, "sex", rbinom(500, 1, 0.5))
formation <- ~edges + nodematch("sex")
target.stats <- c(500, 300)
coef.diss <- dissolution_coefs(dissolution = ~offset(edges) +
offset(nodematch("sex")), duration = c(50, 40))
# Estimate the model
est <- netest(nw, formation, target.stats, coef.diss, verbose = FALSE)
# Static diagnostics
dx1 <- netdx(est, nsims = 1e4, dynamic = FALSE,
nwstats.formula = ~edges + meandeg + concurrent +
nodefactor("sex", levels = NULL) +
nodematch("sex"))
dx1
# Plot diagnostics
plot(dx1)
plot(dx1, stats = c("edges", "concurrent"), mean.col = "black",
sim.lines = TRUE, plots.joined = FALSE)
plot(dx1, stats = "edges", method = "b",
col = "seagreen3", grid = TRUE)
# Dynamic diagnostics
dx2 <- netdx(est, nsims = 10, nsteps = 500,
nwstats.formula = ~edges + meandeg + concurrent +
nodefactor("sex", levels = NULL) +
nodematch("sex"))
dx2
# Formation statistics plots, joined and separate
plot(dx2, grid = TRUE)
plot(dx2, type = "formation", plots.joined = TRUE)
plot(dx2, type = "formation", sims = 1, plots.joined = TRUE,
qnts = FALSE, sim.lines = TRUE, mean.line = FALSE)
plot(dx2, type = "formation", plots.joined = FALSE,
stats = c("edges", "concurrent"), grid = TRUE)
plot(dx2, method = "b", col = "bisque", grid = TRUE)
plot(dx2, method = "b", stats = "meandeg", col = "dodgerblue")
# Duration statistics plot
par(mfrow = c(1, 2))
# With duration imputed
plot(dx2, type = "duration", sim.line = TRUE, sim.lwd = 0.3,
targ.lty = 1, targ.lwd = 0.5)
# Without duration imputed
plot(dx2, type = "duration", sim.line = TRUE, sim.lwd = 0.3,
targ.lty = 1, targ.lwd = 0.5, duration.imputed = FALSE)
# Dissolution statistics plot
plot(dx2, type = "dissolution", qnts = 0.25, grid = TRUE)
plot(dx2, type = "dissolution", method = "b", col = "pink1")
# Cumulative degree distribution: two models with the same mean degree and
# the same mean partnership duration, differing only in how much
# concurrency (overlapping partnerships) they allow
nw <- network_initialize(n = 500)
coef.diss <- dissolution_coefs(dissolution = ~offset(edges), duration = 25)
est.hi <- netest(nw, formation = ~edges + concurrent,
target.stats = c(200, 150), coef.diss = coef.diss,
verbose = FALSE)
est.lo <- netest(nw, formation = ~edges + concurrent,
target.stats = c(200, 40), coef.diss = coef.diss,
verbose = FALSE)
dx.hi <- netdx(est.hi, nsims = 5, nsteps = 250, keep.tedgelist = TRUE,
nwstats.formula = ~edges + concurrent + degree(0:3))
dx.lo <- netdx(est.lo, nsims = 5, nsteps = 250, keep.tedgelist = TRUE,
nwstats.formula = ~edges + concurrent + degree(0:3))
# The momentary degree distributions are far apart: about half the nodes in
# the high-concurrency model have no partner at any given time and a quarter
# have two, while most nodes in the low-concurrency model have exactly one.
# Over 250 time steps both accumulate about 8.5 partners per node.
par(mfrow = c(1, 2))
plot(dx.hi, type = "cumldeg", momentary = TRUE, xlim = c(0, 25),
main = "High concurrency")
plot(dx.lo, type = "cumldeg", momentary = TRUE, xlim = c(0, 25),
main = "Low concurrency")
# As bars, with the tail of the distribution collapsed into one category
# and the inter-simulation range shown
par(mfrow = c(1, 1))
plot(dx.hi, type = "cumldeg", method = "b", maxdeg = 15, qnts = 0.95)
# Partners accumulated over the last 100 steps only
plot(dx.hi, type = "cumldeg", window = c(150, 250), momentary = TRUE)
# The plotted distributions are returned invisibly
dd <- plot(dx.hi, type = "cumldeg")
tapply(dd$degree * dd$prop, dd$sim, sum)
}
Run the code above in your browser using DataLab