-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.R
226 lines (184 loc) · 5.07 KB
/
model.R
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
source("helpers.R")
##############################
# Different Models that were tried during the analysis
###############################
# Fixed for colony 1 and day 17
g <- get_graph(colony=1, day=17)
# Erdös-Renyi
set.seed(42)
e <- ecount(g)
n <- vcount(g)
p <- e/(n*(n-1)/2)
di.g <- diameter(g)
cl.g <- transitivity(g)
apl.g <- average.path.length(g)
er.graph=erdos.renyi.game(n,p)
plot(g)
plot(er.graph)
cl.p <- transitivity(er.graph)
apl.p <- average.path.length(er.graph)
di.p <- diameter(er.graph)
## Simulation
n_sim <- 1000
cl.sim <- numeric(n_sim)
apl.sim <- numeric(n_sim)
di.sim <- numeric(n_sim)
for (i in 1:n_sim){
g.temp=erdos.renyi.game(n,p)
cl.sim[i] <- transitivity(g.temp)
apl.sim[i] <- average.path.length(g.temp)
di.sim[i] <- diameter(g.temp)
}
mean(cl.sim > cl.g)
mean(apl.sim > apl.g)
mean(di.sim > di.g)
## Optimal p search
steps=seq(0,1,0.05)
len=length(steps)
cl=numeric(len)
apl=numeric(len)
di=numeric(len)
ntrials=100
for (i in 1:len){
cltemp=numeric(ntrials)
apltemp=numeric(ntrials)
ditemp=numeric(ntrials)
for (j in 1:ntrials) {
g_temp=erdos.renyi.game(n,steps[i])
cltemp[j]=transitivity(g_temp)
apltemp[j]=average.path.length(g_temp)
ditemp[j]=diameter(g_temp)
}
cl[i]=mean(cltemp)
apl[i]=mean(apltemp)
di[i]=mean(ditemp)
}
cl
apl
di
best_cl <- steps[which.min(abs(cl-cl.g))]
best_apl <- steps[which.min(abs(apl-apl.g))]
best_di <- steps[which.min(abs(di-di.g))]
cl.opt <- cl[which.min(abs(cl-cl.g))]
apl.opt <- apl[which.min(abs(apl-apl.g))]
di.opt <- di[which.min(abs(di-di.g))]
## Watts-Strogatz
ws.graph=watts.strogatz.game(1,n,10,p)
plot(ws.graph)
transitivity(ws.graph)
average.path.length(ws.graph)
steps=seq(-4,-0.5,0.1)
len=length(steps)
cl=numeric(len)
apl=numeric(len)
ntrials=100
for (i in 1:len){
cltemp=numeric(ntrials)
apltemp=numeric(ntrials)
for (j in 1:ntrials) {
g_temp=watts.strogatz.game(1,n,10,10^steps[i])
cltemp[j]=transitivity(g_temp)
apltemp[j]=average.path.length(g_temp)
}
cl[i]=mean(cltemp)
apl[i]=mean(apltemp)
}
cl
apl
## Correlation Networks
vertex.attributes(g)
g.size <- V(g)$body_size
g.age <- V(g)$`age(days)`
g.forage <- V(g)$nb_foraging_events
g.brood <- V(g)$visits_to_brood
g.nest <- V(g)$visits_to_nest_entrance
g.rubbish <- V(g)$visits_to_rubbishpile
g.queen <- V(g)$nb_interaction_queen
g.attr <- data.frame("size" = g.size, "age" = g.age, "forage" = g.forage,
"brood" = g.brood, "nest" = g.nest, "rubbish" = g.rubbish,
"queen" = g.queen)
plot(g.attr)
mycorr <- cor(g.attr, use = "complete.obs")
z=0.5*log((1+mycorr)/(1-mycorr))
z.vec=z[upper.tri(z)]
n=dim(g.attr)[1]
corr.pvals=2*pnorm(abs(z.vec),0,sqrt(1/(n-3)),lower.tail=FALSE)
length(corr.pvals) #tests simoultenously
# multiple correction testing
corr.pvals.adj=p.adjust(corr.pvals,"BH")
#number of final edges
length(corr.pvals.adj[corr.pvals.adj<0.05])
#check if normality is satisfied by the data
qqnorm(as.matrix(g.attr))
apply(g.attr, 2, shapiro.test)
#partial correlation network
pcorr=function(mycorr)
{
d1=dim(mycorr)[1]
d2=dim(mycorr)[2]
for (i in seq(1,d1)){
for (j in seq(1,d2)){
rowi=mycorr[i,-c(i,j)]
rowj=mycorr[j,-c(i,j)]
pcorr.out=(mycorr[i,j]-rowi*rowj)/sqrt((1-rowi^2)*(1-rowj^2))
}
}
return(pcorr.out)
}
tmp=pcorr(mycorr)
###############################
d1=dim(mycorr)[1]
d2=dim(mycorr)[2]
pcorr.pvals=matrix(0,d1,d2)
pcorr.pvals=matrix(0,d1,d2)
for (i in seq(1,d1)){
for (j in seq(1,d2)){
rowi=mycorr[i,-c(i,j)]
rowj=mycorr[j,-c(i,j)]
tmp=(mycorr[i,j]-rowi*rowj)/sqrt((1-rowi^2)*(1-rowj^2))
tmp.zvals=0.5*log((1+tmp)/(1-tmp))
tmp.s.zvals=sqrt(n-4)*tmp.zvals
tmp.pvals=2*pnorm(abs(tmp.s.zvals),0,1,lower.tail=FALSE)
pcorr.pvals[i,j]=max(tmp.pvals)
}
}
#apply for multiple testing
pcorr.pvals.vec=pcorr.pvals[lower.tri(pcorr.pvals)]
pcorr.pvals.adj=p.adjust(pcorr.pvals.vec, "BH")
#apply significant level gamma=0.05
pcorr.edges=pcorr.pvals.adj<0.05
#25 edges have been detected
length(pcorr.pvals.adj[pcorr.edges])
#create the final network
pcorr.A=matrix(0,153,153)
pcorr.A[lower.tri(pcorr.A)]=as.numeric(pcorr.edges)
g.pcorr=graph.adjacency(pcorr.A,"undirected")
plot(g.pcorr,vertex.size=3,vertex.color="lightblue",vertex.label=NA)
#compare it with original network
graph.intersection(g, g.pcorr, byname=FALSE)
## Gaussian-Graphical Model
library(huge)
set.seed(4)
huge.out <- huge(as.matrix(g.attr), lambda = seq(0.1, 10, 0.5))
# choice of penalty parameter
huge.opt <- huge.select(huge.out, criterion="ric")
huge.opt$opt.lambda
sum(huge.opt$refit)
graph.adjacency(huge.opt$refit) #returns an empty graph
### ERGM models
library(network)
library(ergm)
library(intergraph)
g.network <- asNetwork(g)
edge.indep <- ergm(g.network ~ edges)
summary(edge.indep)
edge.indep$coef[1]
plogis(coef(edge.indep))
# Nodes have a 68.9% chance of being connected
edge.group <- ergm(g.network ~ edges + nodematch("group"))
summary(edge.group)
plogis(coef(edge.group)[1] + coef(edge.group)[2])
# Nodes of the same group have a 88% chance of being connected
gof.edge.group=gof(edge.group)
par(mfrow=c(2,2))
plot(gof.edge.group)