forked from statnet/Workshops----OBSOLETE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathergm_tutorial.R
More file actions
175 lines (136 loc) · 6.72 KB
/
Copy pathergm_tutorial.R
File metadata and controls
175 lines (136 loc) · 6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
## ----setup, include=FALSE------------------------------------------------
library(knitr)
knitr::opts_chunk$set(cache=T, comment=NA)
## ----eval=FALSE----------------------------------------------------------
## install.packages(c('statnet', 'sna'))
## library(statnet)
## ----eval=FALSE----------------------------------------------------------
## install.packages('latticeExtra')
## require(latticeExtra)
## ------------------------------------------------------------------------
sessionInfo()
## ------------------------------------------------------------------------
set.seed(0)
## ------------------------------------------------------------------------
data(package='ergm') # tells us the datasets in our packages
## ------------------------------------------------------------------------
data(florentine) # loads flomarriage and flobusiness data
flomarriage # Look at the flomarriage network properties (uses `network`)
par(mfrow=c(1,2)) # Setup a 2 panel plot
plot(flomarriage, main="Florentine Marriage",
cex.main=0.8,
label = network.vertex.names(flomarriage)) # Plot the network
wealth <- flomarriage %v% 'wealth' # %v% references vertex attributes
wealth
plot(flomarriage, vertex.cex=wealth/25, main="Florentine marriage by wealth", cex.main=0.8) # Plot the network with vertex size proportional to wealth
## ------------------------------------------------------------------------
summary(flomarriage ~ edges) # Look at the $g(y)$ statistic for this model
flomodel.01 <- ergm(flomarriage ~ edges) # Estimate the model
summary(flomodel.01) # Look at the fitted model object
## ---- message = FALSE----------------------------------------------------
summary(flomarriage~edges+triangle) # Look at the g(y) stats for this model
flomodel.02 <- ergm(flomarriage~edges+triangle)
summary(flomodel.02)
## ------------------------------------------------------------------------
class(flomodel.02) # this has the class ergm
names(flomodel.02) # the ERGM object contains lots of components.
## ------------------------------------------------------------------------
flomodel.02$coef # you can extract/inspect individual components
## ------------------------------------------------------------------------
summary(wealth) # summarize the distribution of wealth
plot(flomarriage, vertex.cex=wealth/25, main="Florentine marriage by wealth", cex.main=0.8) # network plot with vertex size proportional to wealth
summary(flomarriage~edges+nodecov('wealth')) # observed statistics for the model
flomodel.03 <- ergm(flomarriage~edges+nodecov('wealth'))
summary(flomodel.03)
## ------------------------------------------------------------------------
data(faux.mesa.high)
mesa <- faux.mesa.high
## ------------------------------------------------------------------------
mesa
par(mfrow=c(1,1)) # Back to 1-panel plots
plot(mesa, vertex.col='Grade')
legend('bottomleft',fill=7:12,legend=paste('Grade',7:12),cex=0.75)
## ------------------------------------------------------------------------
fauxmodel.01 <- ergm(mesa ~edges + nodematch('Grade',diff=T) + nodematch('Race',diff=T))
summary(fauxmodel.01)
## ------------------------------------------------------------------------
table(mesa %v% 'Race') # Frequencies of race
mixingmatrix(mesa, "Race")
## ------------------------------------------------------------------------
summary(mesa ~edges + nodematch('Grade',diff=T) + nodematch('Race',diff=T))
## ------------------------------------------------------------------------
data(samplk)
ls() # directed data: Sampson's Monks
samplk3
plot(samplk3)
summary(samplk3~edges+mutual)
## ---- message = F--------------------------------------------------------
sampmodel.01 <- ergm(samplk3~edges+mutual)
summary(sampmodel.01)
## ------------------------------------------------------------------------
missnet <- network.initialize(10,directed=F)
missnet[1,2] <- missnet[2,7] <- missnet[3,6] <- 1
missnet[4,6] <- missnet[4,9] <- missnet[5,6] <- NA
summary(missnet)
# plot missnet with missing edge colored red.
tempnet <- missnet
tempnet[4,6] <- tempnet[4,9] <- tempnet[5,6] <- 1
missnetmat <- as.matrix(missnet)
missnetmat[is.na(missnetmat)] <- 2
plot(tempnet,label = network.vertex.names(tempnet),edge.col = missnetmat)
summary(missnet~edges)
summary(ergm(missnet~edges))
## ------------------------------------------------------------------------
missnet_bad <- missnet
missnet_bad[4,6] <- missnet_bad[4,9] <- missnet_bad[5,6] <- 0
summary(missnet_bad)
summary(ergm(missnet_bad~edges))
## ----eval=FALSE----------------------------------------------------------
## help('ergm-terms')
## ------------------------------------------------------------------------
flomodel.03.sim <- simulate(flomodel.03,nsim=10)
class(flomodel.03.sim)
summary(flomodel.03.sim)
length(flomodel.03.sim)
flomodel.03.sim[[1]]
plot(flomodel.03.sim[[1]], label= flomodel.03.sim[[1]] %v% "vertex.names")
## ------------------------------------------------------------------------
flomodel.03.gof <- gof(flomodel.03~degree + esp + distance)
flomodel.03.gof
plot(flomodel.03.gof)
## ------------------------------------------------------------------------
mesamodel.02 <- ergm(mesa~edges)
mesamodel.02.gof <- gof(mesamodel.02~degree + esp + distance,
control.gof.formula(nsim=10))
plot(mesamodel.02.gof)
## ---- message = F--------------------------------------------------------
summary(flobusiness~edges+degree(1))
fit <- ergm(flobusiness~edges+degree(1))
mcmc.diagnostics(fit)
## ---- eval=FALSE---------------------------------------------------------
## fit <- ergm(flobusiness~edges+degree(1),
## control=control.ergm(MCMC.interval=1))
## ------------------------------------------------------------------------
data('faux.magnolia.high')
magnolia <- faux.magnolia.high
plot(magnolia, vertex.cex=.5)
summary(magnolia~edges+triangle)
## ---- eval=F-------------------------------------------------------------
## fit <- ergm(magnolia~edges+triangle)
## ---- eval=T, message=F, warning = F-------------------------------------
fit <- ergm(magnolia~edges+triangle,
control=control.ergm(MCMLE.maxit=2))
## ---- eval=T, results='hide', fig.show='asis'----------------------------
mcmc.diagnostics(fit)
## ---- message = F, warning = F-------------------------------------------
fit <- ergm(magnolia~edges+gwesp(0.25,fixed=T),
verbose=T)
mcmc.diagnostics(fit)
## ------------------------------------------------------------------------
fit <- ergm(magnolia~edges+gwesp(0.25,fixed=T)+nodematch('Grade')+
nodematch('Race')+nodematch('Sex'),
control = control.ergm(MCMC.samplesize=50000,
MCMC.interval=1000),
verbose=T)
## ------------------------------------------------------------------------
mcmc.diagnostics(fit)