# \dontshow{
require(ergm)
set.seed(21093)
a1<-network::as.network(matrix(c(rbinom(10, 1,.3),
rbinom(10, 1,.3),
rbinom(10, 1,.3),
rbinom(10, 1,.3),
rbinom(10, 1,.3),
rbinom(10, 1,.3),
rbinom(10, 1,.3),
rbinom(10, 1,.3),
rbinom(10, 1,.3),
rbinom(10, 1,.3)),
nrow=10,ncol=10))
network::set.vertex.attribute(a1,"var.1",rbinom(10,1,.3))
a<-ergm(a1~edges+nodeifactor("var.1"))
ab<-ergm(a1~edges+nodeifactor("var.1")+nodeofactor("var.1"))
ergm.mma(a,ab,mediator="nodeofactor.var.1.1",
direct.effect="nodeifactor.var.1.1")
# }
# \donttest{
library(ergm)
data("faux.mesa.high")
faux.mesa.high
set.seed(21093)
my.ergm1<-ergm(faux.mesa.high~edges+
nodecov("Grade")+
nodefactor("Race")+
nodefactor("Sex")+
nodematch("Grade")+
nodematch("Sex")+
nodematch("Race"),
estimate = "MLE")
set.seed(21093)
my.ergm2<-ergm(faux.mesa.high~edges+
nodecov("Grade")+
nodefactor("Race")+
nodefactor("Sex")+
nodematch("Grade")+
nodematch("Sex")+
nodematch("Race")+
gwesp(.5,fixed=TRUE))
###gwesp as single mediator
ergm.mma(my.ergm1,my.ergm2,mediator="gwesp.fixed.0.5",
direct.effect="nodecov.Grade")
###multiple mediators
set.seed(21093)
my.ergm3<-ergm(faux.mesa.high~edges+
nodecov("Grade")+
nodefactor("Race")+
nodefactor("Sex")+
nodematch("Grade")+
nodematch("Sex")+
nodematch("Race")+
gwdsp(1,fixed=TRUE)+
gwesp(.5,fixed=TRUE),
#increase MC sample to facilitate convergence
control = control.ergm(MCMC.samplesize=5000)
)
##joint mediation for gwdsp and gwesp
ergm.mma(my.ergm2,my.ergm3,mediator=c("gwdsp.fixed.1","gwesp.fixed.0.5"),
direct.effect=names(my.ergm3$coef[8]))
# }
Run the code above in your browser using DataLab