# Generating a small graph
set.seed(12)
n <- 4
net <- rbernoulli(n, p = .3)
model <- net ~ edges + mutual
library(ergm)
ans_ergmito <- ergmito(model)
ans_ergm <- ergm(model)
# The ergmito should have a larger value
ergm.exact(ans_ergmito$coef, model)
ergm.exact(ans_ergm$coef, model)
summary(ans_ergmito)
summary(ans_ergm)
# Example 2: Estimating an ERGMito using data with know DGP parameters -----
data(fivenets)
model1 <- ergmito(fivenets ~ edges + nodematch("female"))
summary(model1) # This data has know parameters equal to -2.0 and 2.0
# Example 3: Likelihood ratio test using the lmtest R package
if (require(lmtest)) {
data(fivenets)
model1 <- ergmito(fivenets ~ edges + nodematch("female"))
model2 <- ergmito(fivenets ~ edges + nodematch("female") + mutual)
lrtest(model1, model2)
# Likelihood ratio test
#
# Model 1: fivenets ~ edges + nodematch("female")
# Model 2: fivenets ~ edges + nodematch("female") + mutual
# #Df LogLik Df Chisq Pr(>Chisq)
# 1 2 -34.671
# 2 3 -34.205 1 0.9312 0.3346
}
# Example 4: Adding an reference term for edge-count ----------------------
# Simulating networks of different sizes
set.seed(12344)
nets <- rbernoulli(c(rep(4, 10), rep(5, 10)), c(rep(.2, 10), rep(.1, 10)))
# Fitting an ergmito under the Bernoulli model
ans0 <- ergmito(nets ~ edges)
summary(ans0)
#
# ERGMito estimates
#
# formula:
# nets ~ edges
#
# Estimate Std. Error z value Pr(>|z|)
# edges -1.68640 0.15396 -10.954 < 2.2e-16 ***
# ---
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# AIC: 279.3753 BIC: 283.1436 (Smaller is better.)
# Fitting the model including a reference term for networks of size 5.
# Notice that the variable -n- and other graph attributes can be used
# with -model_update-.
ans1 <- ergmito(nets ~ edges, model_update = ~ I(edges * (n == 5)))
summary(ans1)
#
# ERGMito estimates
#
# formula:
# nets ~ edges + I(edges * (n == 5))
#
# Estimate Std. Error z value Pr(>|z|)
# edges -1.18958 0.21583 -5.5116 3.556e-08 ***
# I(edges * (n == 5)) -0.90116 0.31250 -2.8837 0.00393 **
# ---
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# AIC: 272.9916 BIC: 280.5282 (Smaller is better.)
# The resulting parameter for the edge-count is smaller for networks
# of size five
plogis(coef(ans1)[1]) # 0.23
plogis(sum(coef(ans1))) # 0.11
# We can see that in this case the difference in edge-count matters.
if (require(lmtest)) {
lrtest(ans0, ans1)
# Likelihood ratio test
#
# Model 1: nets ~ edges
# Model 2: nets ~ edges + I(edges * (n == 5))
# #Df LogLik Df Chisq Pr(>Chisq)
# 1 1 -138.69
# 2 2 -134.50 1 8.3837 0.003786 **
# ---
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
}
Run the code above in your browser using DataLab