Skip to content

Commit

Permalink
Merge branch 'master' of github.com:USCCANA/lergms-simulations
Browse files Browse the repository at this point in the history
  • Loading branch information
gvegayon committed Apr 27, 2021
2 parents dc0be89 + 349fefe commit 0e924e2
Show file tree
Hide file tree
Showing 12 changed files with 870 additions and 526 deletions.
121 changes: 118 additions & 3 deletions analysis/ci-ergm.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,62 @@ net <- NETWORKS$advice
net <- net[which(!(as.integer(names(net)) %in% as.integer(miss$Female)))]
net <- net[nvertex(net) > 3]

# Examples for the paper
f <- ergmito_formulae(
net ~ edges + ttriad,
model_update = ~. +
I(edges * (n == 5)) +
I(ttriple * (n == 5)) +
I(edges * log(1/n))
)

d <- data.table::data.table(f$target_stats)
d$n <- as.vector(nvertex(net))
d[, first:=1:.N < 4,by=n]
d <- d[first == TRUE]
d$first <- NULL
colnames(d) <- c("edges", "ttriad", "e*n", "t*n", "offset", "n")
d <- d[, c("n", "edges", "ttriad", "e*n", "t*n", "offset")]

print(
xtable::xtable(
d,
display = c("d", "d", "d", "d", "d", "d", "f")
),
include.rownames = FALSE
)

# Examples (2) for the paper
f <- ergmito_formulae(
net ~ edges + nodematch("Female") + nodeicov("Female") + nodeocov("Female"),
model_update = ~. +
I(sqrt(nodematch.Female)) +
I((nodematch.Female)^(1.25))
)

d <- data.table::data.table(f$target_stats)
d$n <- as.vector(nvertex(net))
d[, first:=1:.N < 4,by=n]
d <- d[first == TRUE]
d$first <- NULL
# colnames(d) <- c("edges", "ttriad", "e*n", "t*n", "offset", "n")
d<-data.frame(d, check.names = FALSE)
d <- cbind(n = d[,ncol(d)], d[,-c(1, ncol(d))])
colnames(d) <- c(
"$n$",
"Homophily (gender)", "Receiver (female)", "Sender (female)",
"$\\text{Homophily}^{1/2}$", "$\\text{Homophily}^{2}$"
)
d <- xtable::xtable(d, display = c("d", "d", "d", "d", "d", "f", "f"))
xtable::align(d) <- rep("\\m{.1\\linewidth}<\\centering", 7)
print(
d,
include.rownames = FALSE,
sanitize.text.function = function(i) i,
booktabs = TRUE
)

f$target_stats
# Part 1: Structural based models ----------------------------------------------
net_baseline00 <- ergmito(net ~ edges + ttriad)
net_baseline01 <- ergmito(net ~ edges + ttriad, model_update = ~ . + offset(I(edges * log(1/n))))
Expand Down Expand Up @@ -49,6 +103,8 @@ plot(gof_ergmito(net_baseline02), sub = "", main = "", xlab = "")
par(op)
dev.off()

plot(net_baseline02)

# Part 2: Main hypothesis testing ----------------------------------------------

# Arbitrary models: Interaction term with a function of size
Expand All @@ -69,8 +125,66 @@ net_struct_baseline02 <- ergmito(

net_struct_baseline03 <- ergmito(
net ~ edges + ttriad + nodematch("Female"),
model_update = ~ . + I(edges * (n == 5)) + I((nodematch.Female) ^ 2)
)
model_update = ~ . + I(edges * (n == 5)) + offset(I(ifelse(edges <= 4, -Inf, 0)))
) #;net_struct_baseline03$optim.out

# Calculating the CDF with and without the offset ------------------------------

edges_seq <- sort(unique(net_struct_baseline01$formulae$stats_statmat[[1]][, "edges"]))

# With the offset term
cdf_with_offset <- with(net_struct_baseline03$formulae, {

sapply(edges_seq, function(e) {

idx <- which(stats_statmat[[1]][, "edges"] == e)

if (length(idx) == 0)
return(0)

p <- exp(exact_loglik(
x = stats_statmat[[1]][idx, , drop = FALSE],
params = coef(net_struct_baseline03),
stats_weights = stats_weights[[1]],
stats_statmat = stats_statmat[[1]],
target_offset = stats_offset[[1]][idx],
stats_offset = stats_offset[[1]]
))

sum(p * stats_weights[[1]][idx])
})


})


cdf_without_offset <- with(net_struct_baseline01$formulae, {

sapply(edges_seq, function(e) {

idx <- which(stats_statmat[[1]][, "edges"] == e)

if (length(idx) == 0)
return(0)

p <- exp(exact_loglik(
x = stats_statmat[[1]][idx, , drop = FALSE],
params = coef(net_struct_baseline01),
stats_weights = stats_weights[[1]],
stats_statmat = stats_statmat[[1]],
target_offset = stats_offset[[1]][idx],
stats_offset = stats_offset[[1]]
))

sum(p * stats_weights[[1]][idx])
})


})

plot(cumsum(cdf_with_offset), type = "l", col = "red", lwd=2)
lines(cumsum(cdf_without_offset), col = "blue", lwd=2)


net_struct_baseline04 <- ergmito(
net ~ edges + ttriad + nodematch("Female") + nodeocov("Female"),
Expand Down Expand Up @@ -116,4 +230,5 @@ pdf("analysis/ci-ergmito-gof-full.pdf", width = 4, height = 7)
op <- par(mar = par("mar") * c(1, 1, .2, .5))
plot(gof_ergmito(net_struct_baseline04), sub = "", main = "")
par(op)
dev.off()
dev.off()

Loading

1 comment on commit 0e924e2

@gvegayon
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to update source file after publishing paper.

Please sign in to comment.