library(RCurl)
dl.data<-getURL("http://pastebin.com/raw.php?i=WdjebzzW", ssl.verifypeer=FALSE)
dl.data<-textConnection(dl.data)
source(dl.data)
rm(dl.data)
sapply(list.vertex.attributes(snaspb.net), function(x) return(get.vertex.attribute(snaspb.net, x)))
plot(snaspb.net, vertex.cex=.75*log(snaspb.net %v% "statusesCount"), vertex.col=rev(rainbow(2, s=.75))[1+(snaspb.net %v% "attended"==T)], edge.col="light grey", bg="transparent", usecurve=TRUE, edge.curve=.01, layout.par=list(niter=5000, area=(snaspb.net %n% "n")^3))
basemod<-ergm(snaspb.net~edges)
summary(basemod)
library(sna)
gden(snaspb.net)
base.sim <- simulate(basemod)
plot(base.sim, vertex.cex=.75, vertex.col=rev(rainbow(2, s=.75))[1+(base.sim %v% "attended"==T)], edge.col="light grey", bg="transparent", usecurve=TRUE, edge.curve=.01, layout.par=list(niter=5000, area=(base.sim %n% "n")^3))
base.gof<-gof(basemod)
summary(base.gof)
par(mfrow=c(2,2)); plot(base.gof)
mod1<-ergm(snaspb.net ~ edges + nodematch("attended", diff=TRUE))
summary(mod1)
mod1.sim <- simulate(mod1)
plot(mod1.sim, vertex.cex=.75, vertex.col=rev(rainbow(2, s=.75))[1+(mod1.sim %v% "attended"==T)], edge.col="light grey", bg="transparent", usecurve=TRUE, edge.curve=.01, layout.par=list(niter=5000, area=(mod1.sim %n% "n")^3))
mod1.gof<-gof(mod1)
summary(mod1.gof)
par(mfrow=c(2,2)); plot(mod1.gof)
mod2<-ergm(snaspb.net~edges+nodematch("attended", diff=TRUE)+gwesp(.5))
mod2.mcmc.diag<-mcmc.diagnostics.ergm(mod2)
Are sample statistics significantly different from observed?
Sample statistics auto-correlation:...
Sample statistics burn-in diagnostic (Geweke):...
#If we had trouble with the sample statistics, we would try something like
#mod2.1<-ergm(snaspb.net ~ edges+nodematch("attended", diff = TRUE)+gwesp(.5), control = control.ergm( MCMC.burnin = 50000, MCMC.interval = 5000, MCMC.samplesize=50000))
#Instead, we'll use mod2.1<-ergm(snaspb.net ~ edges+nodematch("attended", diff = TRUE)+gwesp(.5), control = control.ergm( MCMC.burnin = 50000, MCMC.interval = 5000))
mod2.1.mcmc.diag<-mcmc.diagnostics.ergm(mod2.1)
#The sample statistics aren't great, but everything else looks okay.
summary(mod2.1)
mod2.1.sim <- simulate(mod2.1)
plot(mod2.1.sim, vertex.cex=.75, vertex.col=rev(rainbow(2, s=.75))[1+(mod2.1.sim %v% "attended"==T)], edge.col="light grey", bg="transparent", usecurve=TRUE, edge.curve=.01, layout.par=list(niter=5000, area=(mod2.1.sim %n% "n")^3))
mod1.gof<-gof(mod1)
summary(mod1.gof)
par(mfrow=c(2,2)); plot(mod1.gof)
#What if those attending are just more popular?
mod1.1<-ergm(snaspb.net~edges+nodefactor("attended"))
summary(mod1.1)
mod1.1.sim<-simulate(mod1.1)
plot(mod1.1.sim, vertex.cex=.75, vertex.col=rev(rainbow(2, s=.75))[1+(mod1.1.sim %v% "attended"==T)], edge.col="light grey", bg="transparent", usecurve=TRUE, edge.curve=.01, layout.par=list(niter=5000, area=(mod1.1.sim %n% "n")^3))
mod1.1.gof<-gof(mod1.1)
summary(mod1.1.gof)
par(mfrow=c(2,2)); plot(mod1.1.gof)
#Compare to the findings from mod1. Which is the best model?
library(igraph); library(intergraph)
polblogs<-nexus.get(id="polblogs") #0=liberal, 1=conservative
polblogs<-asNetwork(polblogs)
detach("package:igraph", unload=TRUE)
pol.baseline<-ergm(polblogs~edges)
pol.nodecov<-ergm(polblogs~edges+nodecov("LeftRight")) #Is side more popular?
pol.nodematch<-ergm(polblogs~edges+nodematch("LeftRight")) #General homophily?
pol.nodematch.diff<-ergm(polblogs~edges+nodematch("LeftRight", diff=TRUE)) #Is one side more homophilous than the other?
pol.nodemix<-ergm(polblogs~edges+nodemix("LeftRight")) #Is one side more likely to link to the other?
follower.mod<-ergm(snaspb.net~ edges + nodefactor("attended") + nodecov("followersCount"))
#Consider recoding the skewed attributes on a log scale and reassigning the attribute
snaspb.net %v% "followersCount.log" <- log(snaspb.net %v% "followersCount")
follower.log.mod<-ergm(snaspb.net~ edges + nodefactor("attended") + nodecov(log("followersCount.log")))
gwesp.mod<-ergm(snaspb.net~ edges + nodefactor("attended") + gwesp(.25))
cohort.mod<-ergm(snaspb.net~ edges + nodefactor("attended") + absdiff("created"))