-
Notifications
You must be signed in to change notification settings - Fork 7
/
.Rhistory
512 lines (512 loc) · 23 KB
/
.Rhistory
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
news(package="ergm")
library(sand)
library(robustbase)
library(ergm) # Will load package 'network' as well.
library(igraph)
library(ggplot2)
library(dplyr)
library(maditr)
library(textshape)
library(VIF)
library(bipartite)
library(sjstats)
library(Hmisc)
library(network)
library(nlme)
library(ergm.count)
install.packages("ergm")
install.packages("ergm")
library(sand)
library(robustbase)
library(ergm) # Will load package 'network' as well.
library(igraph)
library(ggplot2)
library(dplyr)
library(maditr)
library(textshape)
library(VIF)
library(bipartite)
library(sjstats)
library(Hmisc)
library(network)
library(nlme)
library(ergm.count)
library(ergm)
library(ergm.count)
library(ergm.count)
library(sand)
library(robustbase)
library(ergm) # Will load package 'network' as well.
library(igraph)
library(ggplot2)
library(dplyr)
library(maditr)
library(textshape)
library(VIF)
library(bipartite)
library(sjstats)
library(Hmisc)
library(network)
library(nlme)
library(ergm.count)
# read interaction data
int.data <- read.table("Data/rocina_halimium_09072019.csv", sep=";", header=T)
View(int.data)
# read interaction data
int.data <- read.table("Data/rocina_halimium_09072019.csv", sep=";", header=T)
View(int.data)
str(int.data)
int.data$N_visits <- as.numeric(int.data$N_visits)
pol.fg <- int.data %>% dplyr::select(Pollinator_sp, Pollinator_fg) # data on pollinator functional groups, I will use it when incorporating pollinator traits into the network object
View(pol.fg)
# read interaction data
int.data <- read.table("Data/rocina_halimium_09072019.csv", sep=";", header=T)
str(int.data)
int.data$N_visits <- as.numeric(int.data$N_visits)
pol.fg <- int.data %>% dplyr::select(Pollinator_sp, Pollinator_fg) # data on pollinator functional groups, I will use it when incorporating pollinator traits into the network object
int.matrix <- dcast(dplyr::select(Plant_ID, Pollinator_sp, N_visits),
Plant_ID ~ Pollinator_sp, value.var="N_visits", fun.aggregate = sum)
int.matrix <- dcast(int.data %>%dplyr::select(Plant_ID, Pollinator_sp, N_visits),
Plant_ID ~ Pollinator_sp, value.var="N_visits", fun.aggregate = sum)
View(int.matrix)
int.data %>%dplyr::select(Plant_ID, Pollinator_sp, N_visits)
# Chunk 1: setup
knitr::opts_chunk$set(echo = TRUE)
# Chunk 2: initialize
library(sand)
library(robustbase)
library(ergm) # Will load package 'network' as well.
library(igraph)
library(ggplot2)
library(dplyr)
library(maditr)
library(textshape)
library(VIF)
library(bipartite)
library(sjstats)
library(Hmisc)
library(network)
library(nlme)
library(ergm.count)
# Chunk 3: collinearity
# read interaction data
int.data <- read.table("Data/rocina_halimium_09072019.csv", sep=";", header=T)
str(int.data)
int.data$N_visits <- as.numeric(int.data$N_visits)
# create interaction matrix (individual plants x pollinator species)
int.matrix <- dcast(int.data %>%dplyr::select(Plant_ID, Pollinator_sp, N_visits),
Plant_ID ~ Pollinator_sp, value.var="N_visits", fun.aggregate = sum)
# read plant attributes and merge with the interaction matrix
trait.data <- read.table("Data/individual_data_halimium_20190714.csv", sep=";", header=T)
complete.df <- merge(int.matrix, trait.data, by="Plant_ID")
# Chunk 4: bi.nodes
# Subset interaction data
pol.int <- complete.df %>%
dplyr::select(2:22)
bi.g <- graph_from_incidence_matrix(pol.int, weighted= T, add.names=NULL) # create the igraph object
# Subset the covariates from the main dataset (phenotypic and microsite variation among plant individuals)
attr <- complete.df %>%
dplyr::select(c(1, 25:51))
# Scale the predictor variables I will use in the models
attr$Max_height_m <- scale(attr$Max_height_m)
attr$Flower_guide <- scale(attr$Flower_guide)
attr$Flower_size <- scale(attr$Flower_size)
attr$Sum_flower_number <- scale(attr$Sum_flower_number)
attr$Synchrony_marquis <- scale(attr$Synchrony_marquis)
attr$Distance_to_stream <- scale(attr$Distance_to_stream)
attr$Distance_to_tree <- scale(attr$Distance_to_tree)
attr$Distance_to_edge <- scale(attr$Distance_to_edge)
attr$Neighbour_intra_1m_m2 <- scale(attr$Neighbour_intra_1m_m2)
attr$Neighbour_inter_1m_m2 <- scale(attr$Neighbour_inter_1m_m2)
attr$Neighbour_intra_2m_m2 <- scale(attr$Neighbour_intra_2m_m2)
attr$Neighbour_inter_2m_m2 <- scale(attr$Neighbour_inter_2m_m2)
# Associate plant attributes to plant nodes in the igraph object
V(bi.g)$Plant_ID <- c(attr$Plant_ID[1:60], rep(NA, 21))
V(bi.g)$Max_height_m <- c(attr$Max_height_m[1:60], rep(NA, 21))
V(bi.g)$Flower_guide <- c(attr$Flower_guide[1:60], rep(NA, 21))
V(bi.g)$Flower_size <- c(attr$Flower_size[1:60], rep(NA, 21))
V(bi.g)$Sum_flower_number <- c(attr$Sum_flower_number[1:60], rep(NA, 21))
V(bi.g)$Synchrony_marquis <- c(attr$Synchrony_marquis[1:60], rep(NA, 21))
V(bi.g)$Distance_to_stream <- c(attr$Distance_to_stream[1:60], rep(NA, 21))
V(bi.g)$Distance_to_tree <- c(attr$Distance_to_tree[1:60], rep(NA, 21))
V(bi.g)$Distance_to_edge <- c(attr$Distance_to_edge[1:60], rep(NA, 21))
V(bi.g)$Neighbour_intra_1m_m2 <- c(attr$Neighbour_intra_1m_m2[1:60], rep(NA, 21))
V(bi.g)$Neighbour_inter_1m_m2 <- c(attr$Neighbour_inter_1m_m2[1:60], rep(NA, 21))
V(bi.g)$Neighbour_intra_2m_m2 <- c(attr$Neighbour_intra_2m_m2[1:60], rep(NA, 21))
V(bi.g)$Neighbour_inter_2m_m2 <- c(attr$Neighbour_inter_2m_m2[1:60], rep(NA, 21))
## List of pollinator species appearing in the interaction matrix
pol.sp <- as.data.frame(colnames(int.matrix[, 2:22]))
pol.sp
colnames(pol.sp)[1] <- "Pollinator_sp"
## Merge this list with data on pollinator functional groups, the same list has now a second column specifying the functional group of each species
pol.fg <- int.data %>% dplyr::select(Pollinator_sp, Pollinator_fg)
pol.fg
unique(pol.fg)
## Merge this list with data on pollinator functional groups, the same list has now a second column specifying the functional group of each species
pol.fg <- int.data %>% dplyr::select(Pollinator_sp, Pollinator_fg)
pol.sp.fg <- merge(pol.sp, pol.fg, by = "Pollinator_sp", all.y = F, all.x = T)
pol.sp.fg
pol.sp.fg <- unique(pol.sp.fg)
pol.sp.fg
## Merge this list with data on pollinator functional groups, the same list has now a second column specifying the functional group of each species
pol.fg <- int.data %>% dplyr::select(Pollinator_sp, Pollinator_fg)
pol.fg <- unique(pol.fg)
pol.sp.fg <- merge(pol.sp, pol.fg, by = "Pollinator_sp", all.y = F, all.x = T)
pol.sp.fg
pol.fg
## Merge this list with data on pollinator functional groups, the same list has now a second column specifying the functional group of each species
pol.fg <- int.data %>% dplyr::select(Pollinator_sp, Pollinator_fg)
pol.fg <- unique(pol.fg)
join(pol.sp, pol.fg)
library(plyr)
join(pol.sp, pol.fg)
# read interaction data
int.data <- read.table("Data/rocina_halimium_09072019.csv", sep=";", header=T)
str(int.data)
int.data$N_visits <- as.numeric(int.data$N_visits)
# create interaction matrix (individual plants x pollinator species)
int.matrix <- dcast(int.data %>%dplyr::select(Plant_ID, Pollinator_sp, N_visits),
Plant_ID ~ Pollinator_sp, value.var="N_visits", fun.aggregate = sum)
# read plant attributes and merge with the interaction matrix
trait.data <- read.table("Data/individual_data_halimium_20190714.csv", sep=";", header=T)
complete.df <- merge(int.matrix, trait.data, by="Plant_ID")
View(complete.df)
# Subset interaction data
pol.int <- complete.df %>%
dplyr::select(2:22)
View(pol.int)
bi.g <- graph_from_incidence_matrix(pol.int, weighted= T, add.names=NULL) # create the igraph object
bi.g
# Subset the covariates from the main dataset (phenotypic and microsite variation among plant individuals)
attr <- complete.df %>%
dplyr::select(c(1, 25:51))
# Scale the predictor variables I will use in the models
attr$Max_height_m <- scale(attr$Max_height_m)
attr$Flower_guide <- scale(attr$Flower_guide)
attr$Flower_size <- scale(attr$Flower_size)
attr$Sum_flower_number <- scale(attr$Sum_flower_number)
attr$Synchrony_marquis <- scale(attr$Synchrony_marquis)
attr$Distance_to_stream <- scale(attr$Distance_to_stream)
attr$Distance_to_tree <- scale(attr$Distance_to_tree)
attr$Distance_to_edge <- scale(attr$Distance_to_edge)
attr$Neighbour_intra_1m_m2 <- scale(attr$Neighbour_intra_1m_m2)
attr$Neighbour_inter_1m_m2 <- scale(attr$Neighbour_inter_1m_m2)
attr$Neighbour_intra_2m_m2 <- scale(attr$Neighbour_intra_2m_m2)
attr$Neighbour_inter_2m_m2 <- scale(attr$Neighbour_inter_2m_m2)
# Associate plant attributes to plant nodes in the igraph object
V(bi.g)$Plant_ID <- c(attr$Plant_ID[1:60], rep(NA, 21))
V(bi.g)$Max_height_m <- c(attr$Max_height_m[1:60], rep(NA, 21))
V(bi.g)$Flower_guide <- c(attr$Flower_guide[1:60], rep(NA, 21))
V(bi.g)$Flower_size <- c(attr$Flower_size[1:60], rep(NA, 21))
V(bi.g)$Sum_flower_number <- c(attr$Sum_flower_number[1:60], rep(NA, 21))
V(bi.g)$Synchrony_marquis <- c(attr$Synchrony_marquis[1:60], rep(NA, 21))
V(bi.g)$Distance_to_stream <- c(attr$Distance_to_stream[1:60], rep(NA, 21))
V(bi.g)$Distance_to_tree <- c(attr$Distance_to_tree[1:60], rep(NA, 21))
V(bi.g)$Distance_to_edge <- c(attr$Distance_to_edge[1:60], rep(NA, 21))
V(bi.g)$Neighbour_intra_1m_m2 <- c(attr$Neighbour_intra_1m_m2[1:60], rep(NA, 21))
V(bi.g)$Neighbour_inter_1m_m2 <- c(attr$Neighbour_inter_1m_m2[1:60], rep(NA, 21))
V(bi.g)$Neighbour_intra_2m_m2 <- c(attr$Neighbour_intra_2m_m2[1:60], rep(NA, 21))
V(bi.g)$Neighbour_inter_2m_m2 <- c(attr$Neighbour_inter_2m_m2[1:60], rep(NA, 21))
V(bi.g)$Plant_ID
AA <- get.adjacency(bi.g, attr = "weight") # create an adjacency matrix from a igraph object
v.attrs <- get.data.frame(bi.g, what="vertices")[1:60,] # create a dataframe with the attribute variables
AA
as.matrix(AA)
bi.net <- network::as.network(as.matrix(AA),
directed=FALSE, bipartite=60, ignore.eval=FALSE, names.eval='weight')
as.matrix(bi.net, attrname="weight") # check the link weights are correct
network::set.vertex.attribute(bi.net, "Plant_ID",
v.attrs$Plant_ID)
network::set.vertex.attribute(bi.net, "Max_height_m",
v.attrs$Max_height_m)
network::set.vertex.attribute(bi.net, "Flower_guide",
v.attrs$Flower_guide)
network::set.vertex.attribute(bi.net, "Flower_size",
v.attrs$Flower_size)
network::set.vertex.attribute(bi.net, "Sum_flower_number",
v.attrs$Sum_flower_number)
network::set.vertex.attribute(bi.net, "Synchrony_marquis",
v.attrs$Synchrony_marquis)
network::set.vertex.attribute(bi.net, "Distance_to_stream",
v.attrs$Distance_to_stream)
network::set.vertex.attribute(bi.net, "Distance_to_tree",
v.attrs$Distance_to_tree)
network::set.vertex.attribute(bi.net, "Distance_to_edge",
v.attrs$Distance_to_edge)
network::set.vertex.attribute(bi.net, "Neighbour_intra_1m_m2",
v.attrs$Neighbour_intra_1m_m2)
network::set.vertex.attribute(bi.net, "Neighbour_inter_1m_m2",
v.attrs$Neighbour_inter_1m_m2)
network::set.vertex.attribute(bi.net, "Neighbour_intra_2m_m2",
v.attrs$Neighbour_intra_2m_m2)
network::set.vertex.attribute(bi.net, "Neighbour_inter_2m_m2",
v.attrs$Neighbour_inter_2m_m2)
bi.net
list.vertex.attributes(bi.net)
summary.network(bi.net)
get.vertex.attribute(bi.net)
## Check
get.vertex.attribute(bi.net, "Pollinator_fg")
## List of pollinator species appearing in the interaction matrix
pol.sp <- as.data.frame(colnames(int.matrix[, 2:22]))
colnames(pol.sp)[1] <- "Pollinator_sp"
## Merge this list with data on pollinator functional groups, the same list has now a second column specifying the functional group of each species
pol.fg <- int.data %>% dplyr::select(Pollinator_sp, Pollinator_fg)
pol.fg <- unique(pol.fg)
pol.sp.fg <- join(pol.sp, pol.fg)
## Associate each pollinator species in the nodes of the network object with its functional group
network::set.vertex.attribute(bi.net, "Pollinator_fg",
c(rep(NA, 60), as.vector(pol.sp.fg$Pollinator_fg)))
## Check
get.vertex.attribute(bi.net, "Pollinator_fg")
## Check
get.vertex.attribute(bi.net, "Pollinator_fg")
## Check
bi.net %v% "Pollinator_fg"
bi.net %v% "Plant_ID"
network::set.vertex.attribute(bi.net, "Plant_ID",
c(v.attrs$Plant_ID, rep(NA, 21)))
bi.net %v% "Plant_ID"
network::set.vertex.attribute(bi.net, "Plant_ID",
c(v.attrs$Plant_ID, rep(NA, 21)))
network::set.vertex.attribute(bi.net, "Max_height_m",
c(v.attrs$Max_height_m, rep(NA, 21)))
network::set.vertex.attribute(bi.net, "Flower_guide",
c(v.attrs$Flower_guide, rep(NA, 21)))
network::set.vertex.attribute(bi.net, "Flower_size",
c(v.attrs$Flower_size, rep(NA, 21)))
network::set.vertex.attribute(bi.net, "Sum_flower_number",
c(v.attrs$Sum_flower_number, rep(NA, 21)))
network::set.vertex.attribute(bi.net, "Synchrony_marquis",
c(v.attrs$Synchrony_marquis, rep(NA, 21)))
network::set.vertex.attribute(bi.net, "Distance_to_stream",
c(v.attrs$Distance_to_stream, rep(NA, 21)))
network::set.vertex.attribute(bi.net, "Distance_to_tree",
c(v.attrs$Distance_to_tree, rep(NA, 21)))
network::set.vertex.attribute(bi.net, "Distance_to_edge",
c(v.attrs$Distance_to_edge, rep(NA, 21)))
network::set.vertex.attribute(bi.net, "Neighbour_intra_1m_m2",
c(v.attrs$Neighbour_intra_1m_m2, rep(NA, 21)))
network::set.vertex.attribute(bi.net, "Neighbour_inter_1m_m2",
c(v.attrs$Neighbour_inter_1m_m2, rep(NA, 21)))
network::set.vertex.attribute(bi.net, "Neighbour_intra_2m_m2",
c(v.attrs$Neighbour_intra_2m_m2, rep(NA, 21)))
network::set.vertex.attribute(bi.net, "Neighbour_inter_2m_m2",
c(v.attrs$Neighbour_inter_2m_m2, rep(NA, 21)))
## List of pollinator species appearing in the interaction matrix
pol.sp <- as.data.frame(colnames(int.matrix[, 2:22]))
colnames(pol.sp)[1] <- "Pollinator_sp"
## Merge this list with data on pollinator functional groups, the same list has now a second column specifying the functional group of each species
pol.fg <- int.data %>% dplyr::select(Pollinator_sp, Pollinator_fg)
pol.fg <- unique(pol.fg)
pol.sp.fg <- join(pol.sp, pol.fg)
## Associate each pollinator species in the nodes of the network object with its functional group
network::set.vertex.attribute(bi.net, "Pollinator_fg",
c(rep(NA, 60), as.vector(pol.sp.fg$Pollinator_fg)))
## Check
bi.net %v% "Pollinator_fg"
ergm0 <- formula(bi.net ~ sum)
ergm1 <- formula(bi.net ~ sum
+ b1cov("Max_height_m", form= "sum")
+ b1cov("Flower_guide", form= "sum")
+ b1cov("Sum_flower_number", form= "sum")
+ b1cov("Synchrony_marquis", form= "sum")
+ b1cov("Distance_to_stream", form= "sum")
+ b1cov("Distance_to_tree", form= "sum")
+ b1cov("Distance_to_edge", form= "sum")
+ b1cov("Neighbour_intra_1m_m2", form= "sum")
+ b1cov("Neighbour_inter_1m_m2", form= "sum")
+ b1cov("Neighbour_intra_2m_m2", form= "sum")
+ b1cov("Neighbour_inter_2m_m2", form= "sum")
+ b2factor("Pollinator_fg", form="sum"))
ergm2 <- formula(bi.net ~ sum
+ b1cov("Max_height_m", form= "sum")
+ b1cov("Flower_guide", form= "sum")
+ b1cov("Flower_size", form= "sum")
+ b1cov("Sum_flower_number", form= "sum")
+ b1cov("Synchrony_marquis", form= "sum")
#+ b1cov("Distance_to_stream", form= "sum")
#+ b1cov("Distance_to_tree", form= "sum")
#+ b1cov("Distance_to_edge", form= "sum")
#+ b1cov("Neighbour_intra_1m_m2", form= "sum")
#+ b1cov("Neighbour_inter_1m_m2", form= "sum")
#+ b1cov("Neighbour_intra_2m_m2", form= "sum")
#+ b1cov("Neighbour_inter_2m_m2", form= "sum")
+ b2factor("Pollinator_fg", form="sum"))
ergm3 <- formula(bi.net ~ sum
#+ b1cov("Max_height_m", form= "sum")
#+ b1cov("Flower_guide", form= "sum")
#+ b1cov("Flower_size", form= "sum")
#+ b1cov("Sum_flower_number", form= "sum")
#+ b1cov("Synchrony_marquis", form= "sum"))
+ b1cov("Distance_to_stream", form= "sum")
+ b1cov("Distance_to_tree", form= "sum")
+ b1cov("Distance_to_edge", form= "sum")
#+ b1cov("Neighbour_intra_1m_m2", form= "sum")
#+ b1cov("Neighbour_inter_1m_m2", form= "sum")
#+ b1cov("Neighbour_intra_2m_m2", form= "sum")
#+ b1cov("Neighbour_inter_2m_m2", form= "sum")
+ b2factor("Pollinator_fg", form="sum"))
ergm4 <- formula(bi.net ~ sum
#+ b1cov("Max_height_m", form= "sum")
#+ b1cov("Flower_guide", form= "sum")
#+ b1cov("Flower_size", form= "sum")
#+ b1cov("Sum_flower_number", form= "sum")
#+ b1cov("Synchrony_marquis", form= "sum"))
#+ b1cov("Distance_to_stream", form= "sum")
#+ b1cov("Distance_to_tree", form= "sum")
#+ b1cov("Distance_to_edge", form= "sum")
+ b1cov("Neighbour_intra_1m_m2", form= "sum")
+ b1cov("Neighbour_inter_1m_m2", form= "sum")
+ b1cov("Neighbour_intra_2m_m2", form= "sum")
+ b1cov("Neighbour_inter_2m_m2", form= "sum")
+ b2factor("Pollinator_fg", form="sum"))
# Model tests
set.seed(42)
ergm.fit0 <- ergm(ergm0, response= "weight", reference= ~Poisson) # Simplest model
ergm.fit1 <- ergm(ergm1, response= "weight", reference= ~Poisson) # Full model
summary(ergm.fit0)
mcmc.diagnostics(ergm.fit0)
summary(ergm.fit1)
gof(ergm.fit1)
mcmc.diagnostics(ergm.fit1) # For ergm() specifically, recent changes in the estimation algorithm mean that these plots can no longer be used to ensure that the mean statistics from the model match the observed network statistics.
uni <- as.one.mode(as.matrix(pol.int), project="lower") # pol.int is the plant-pollinator interaction matrix, plants in rows and pollinators in columns
uni.g <- graph.adjacency(uni, mode = "undirected", weighted = TRUE, diag = FALSE) # igraph object
V(uni.g)$Plant_ID<- attr$Plant_ID
V(uni.g)$Max_height_m<- attr$Max_height_m
V(uni.g)$Flower_guide<- attr$Flower_guide
V(uni.g)$Flower_size<- attr$Flower_size
V(uni.g)$Sum_flower_number<- attr$Sum_flower_number
V(uni.g)$Synchrony_marquis<- attr$Synchrony_marquis
V(uni.g)$Distance_to_stream<- attr$Distance_to_stream
V(uni.g)$Distance_to_tree<- attr$Distance_to_tree
V(uni.g)$Distance_to_edge<- attr$Distance_to_edge
V(uni.g)$Neighbour_intra_1m_m2<- attr$Neighbour_intra_1m_m2
V(uni.g)$Neighbour_inter_1m_m2<- attr$Neighbour_inter_1m_m2
V(uni.g)$Neighbour_intra_2m_m2<- attr$Neighbour_intra_2m_m2
V(uni.g)$Neighbour_inter_2m_m2<- attr$Neighbour_inter_2m_m2
V(uni.g)
uni.g
uni.AA <- get.adjacency(uni.g, attr = "weight") # First separate the igraph object into adjacency matrix and plant attributes
uni.AA
s.matrix(uni.AA)
as.matrix(uni.AA)
uni.v.attrs <- get.data.frame(uni.g, what="vertices")[1:60,]
uni.v.attrs
as.matrix(uni.net,attrname="weight") # We can view the weight attribute as a matrix
uni.net <- network::as.network(as.matrix(uni.AA),
directed=FALSE, ignore.eval=FALSE, names.eval='weight') # network object for ergm
as.matrix(uni.net,attrname="weight") # We can view the weight attribute as a matrix
network::set.vertex.attribute(uni.net, "Plant_ID",
uni.v.attrs$Plant_ID)
network::set.vertex.attribute(uni.net, "Max_height_m",
uni.v.attrs$Max_height_m)
network::set.vertex.attribute(uni.net, "Flower_guide",
uni.v.attrs$Flower_guide)
network::set.vertex.attribute(uni.net, "Flower_size",
uni.v.attrs$Flower_size)
network::set.vertex.attribute(uni.net, "Sum_flower_number",
uni.v.attrs$Sum_flower_number)
network::set.vertex.attribute(uni.net, "Synchrony_marquis",
uni.v.attrs$Synchrony_marquis)
network::set.vertex.attribute(uni.net, "Distance_to_stream",
uni.v.attrs$Distance_to_stream)
network::set.vertex.attribute(uni.net, "Distance_to_tree",
uni.v.attrs$Distance_to_tree)
network::set.vertex.attribute(uni.net, "Distance_to_edge",
uni.v.attrs$Distance_to_edge)
network::set.vertex.attribute(uni.net, "Neighbour_intra_1m_m2",
uni.v.attrs$Neighbour_intra_1m_m2)
network::set.vertex.attribute(uni.net, "Neighbour_inter_1m_m2",
uni.v.attrs$Neighbour_inter_1m_m2)
network::set.vertex.attribute(uni.net, "Neighbour_intra_2m_m2",
uni.v.attrs$Neighbour_intra_2m_m2)
network::set.vertex.attribute(uni.net, "Neighbour_inter_2m_m2",
uni.v.attrs$Neighbour_inter_2m_m2)
uni.distance <- read.csv("Data/distance_matrix.csv", sep=",")
uni.distance <- as.matrix(uni.distance[,2:61])
set.network.attribute(uni.net, "distance", uni.distance) # spatial distance between plants as network attribute
set.edge.value(uni.net,'distance', uni.distance)
uni.net
uni.net %e% "distance"
uni.AA <- get.adjacency(uni.g, attr = "weight") # First separate the igraph object into adjacency matrix and plant attributes
uni.v.attrs <- get.data.frame(uni.g, what="vertices")[1:60,]
uni.net <- network::as.network(as.matrix(uni.AA),
directed=FALSE, ignore.eval=FALSE, names.eval='weight') # network object for ergm
as.matrix(uni.net,attrname="weight") # We can view the weight attribute as a matrix
network::set.vertex.attribute(uni.net, "Plant_ID",
uni.v.attrs$Plant_ID)
network::set.vertex.attribute(uni.net, "Max_height_m",
uni.v.attrs$Max_height_m)
network::set.vertex.attribute(uni.net, "Flower_guide",
uni.v.attrs$Flower_guide)
network::set.vertex.attribute(uni.net, "Flower_size",
uni.v.attrs$Flower_size)
network::set.vertex.attribute(uni.net, "Sum_flower_number",
uni.v.attrs$Sum_flower_number)
network::set.vertex.attribute(uni.net, "Synchrony_marquis",
uni.v.attrs$Synchrony_marquis)
network::set.vertex.attribute(uni.net, "Distance_to_stream",
uni.v.attrs$Distance_to_stream)
network::set.vertex.attribute(uni.net, "Distance_to_tree",
uni.v.attrs$Distance_to_tree)
network::set.vertex.attribute(uni.net, "Distance_to_edge",
uni.v.attrs$Distance_to_edge)
network::set.vertex.attribute(uni.net, "Neighbour_intra_1m_m2",
uni.v.attrs$Neighbour_intra_1m_m2)
network::set.vertex.attribute(uni.net, "Neighbour_inter_1m_m2",
uni.v.attrs$Neighbour_inter_1m_m2)
network::set.vertex.attribute(uni.net, "Neighbour_intra_2m_m2",
uni.v.attrs$Neighbour_intra_2m_m2)
network::set.vertex.attribute(uni.net, "Neighbour_inter_2m_m2",
uni.v.attrs$Neighbour_inter_2m_m2)
uni.distance <- read.csv("Data/distance_matrix.csv", sep=",")
uni.distance <- as.matrix(uni.distance[,2:61])
#set.network.attribute(uni.net, "distance", uni.distance) # spatial distance between plants as network attribute
set.edge.value(uni.net,'distance', uni.distance)
uni.net %e% "distance"
uni.ergm0 <- formula(uni.net ~ sum)
# test main effects of plant traits in sharing pollinators
uni.ergm1 <- formula(uni.net ~ sum
+ nodecov("Max_height_m", form= "sum")
+ nodecov("Flower_guide", form= "sum")
+ nodecov("Flower_size", form= "sum")
+ nodecov("Sum_flower_number", form= "sum")
+ nodecov("Synchrony_marquis", form= "sum")
+ nodecov("Distance_to_stream", form= "sum")
+ nodecov("Distance_to_tree", form= "sum")
+ nodecov("Distance_to_edge", form= "sum")
+ nodecov("Neighbour_intra_1m_m2", form= "sum")
+ nodecov("Neighbour_inter_1m_m2", form= "sum")
+ nodecov("Neighbour_intra_2m_m2", form= "sum")
+ nodecov("Neighbour_inter_2m_m2", form= "sum")
+ edgecov("distance", form="sum"))
# test homophily
uni.ergm2 <- formula(uni.net ~ sum
+ absdiff("Max_height_m", form= "sum")
+ absdiff("Flower_guide", form= "sum")
+ absdiff("Flower_size", form= "sum")
+ absdiff("Sum_flower_number", form= "sum")
+ absdiff("Synchrony_marquis", form= "sum")
+ absdiff("Distance_to_stream", form= "sum")
+ absdiff("Distance_to_tree", form= "sum")
+ absdiff("Distance_to_edge", form= "sum")
+ absdiff("Neighbour_intra_1m_m2", form= "sum")
+ absdiff("Neighbour_inter_1m_m2", form= "sum")
+ absdiff("Neighbour_intra_2m_m2", form= "sum")
+ absdiff("Neighbour_inter_2m_m2", form= "sum")
+ edgecov("distance", form="sum"))
set.seed(42)
uni.ergm.fit0 <- ergm(uni.ergm0, response ="weight", reference= ~Poisson)
summary(uni.ergm.fit0)
mcmc.diagnostics(uni.ergm.fit0)
summary(uni.ergm.fit0)
uni.ergm.fit1 <- ergm(uni.ergm1, response ="weight", reference= ~Poisson)
set.network.attribute(uni.net, "distance", uni.distance) # spatial distance between plants as network attribute
get.network.attribute(uni.net, "distance")
uni.ergm.fit1 <- ergm(uni.ergm1, response ="weight", reference= ~Poisson)
summary(uni.ergm.fit1)
mcmc.diagnostics(uni.ergm.fit1)
uni.ergm.fit2 <- ergm(uni.ergm2, response ="weight", reference= ~Poisson)
summary(uni.ergm.fit2)
mcmc.diagnostics(uni.ergm.fit1)