/
test_gergm.R
181 lines (147 loc) · 6.02 KB
/
test_gergm.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
test_that("Simple model with no covariates runs", {
skip_on_cran()
skip("For time")
########################### 1. No Covariates #############################
# Preparing an unbounded network without covariates for gergm estimation #
#skip("Skipping test as it can only be run in the global environment.")
set.seed(12345)
net <- matrix(rnorm(100,0,20),10,10)
colnames(net) <- rownames(net) <- letters[1:10]
# one parameter model
formula <- net ~ edges + ttriads
test <- gergm(formula,
number_of_networks_to_simulate = 40000,
thin = 1/40,
proposal_variance = 0.5,
MCMC_burnin = 10000,
seed = 456,
convergence_tolerance = 0.5)
check_against <- c(-0.081)
expect_equal(round(as.numeric(test@theta.coef[1,]),3), check_against)
})
test_that("3 param model with no covariates runs", {
skip_on_cran()
skip("Weird travis errors")
set.seed(12345)
net <- matrix(rnorm(100,0,20),10,10)
colnames(net) <- rownames(net) <- letters[1:10]
# three parameter model
formula <- net ~ edges + mutual + ttriads
test <- gergm(formula,
number_of_networks_to_simulate = 40000,
thin = 1/40,
proposal_variance = 0.5,
MCMC_burnin = 10000,
seed = 456,
convergence_tolerance = 0.5)
check_against <- c(2.180, -0.268)
expect_equal(round(as.numeric(test@theta.coef[1,]),3), check_against)
})
test_that("4 param model with no covariates runs", {
skip_on_cran()
skip("For time")
set.seed(12345)
net <- matrix(rnorm(100,0,20),10,10)
colnames(net) <- rownames(net) <- letters[1:10]
# five parameter model
formula2 <- net ~ edges + mutual + ttriads + in2stars
test <- gergm(formula2,
number_of_networks_to_simulate = 40000,
thin = 1/40,
proposal_variance = 0.5,
downweight_statistics_together = TRUE,
MCMC_burnin = 10000,
seed = 456,
convergence_tolerance = 0.5)
check_against <- c(2.329, 0.064, -0.498)
expect_equal(round(as.numeric(test@theta.coef[1,]),3), check_against)
})
test_that("Unidrected model with no covariates runs", {
skip_on_cran()
skip("Weird travis errors")
#check that code works for undirected network
set.seed(12345)
net <- matrix(rnorm(100,0,20),10,10)
colnames(net) <- rownames(net) <- letters[1:10]
formula <- net ~ edges + ttriads + twostars
test <- gergm(formula,
network_is_directed = FALSE,
number_of_networks_to_simulate = 40000,
thin = 1/40,
proposal_variance = 0.5,
MCMC_burnin = 10000,
seed = 456,
convergence_tolerance = 0.5)
check_against <- c(0.120, -0.282)
expect_equal(round(as.numeric(test@theta.coef[1,]),3), check_against)
})
test_that("MPLE Only", {
skip_on_cran()
#check that code works with MPLE only
set.seed(12345)
net <- matrix(rnorm(100,0,20),10,10)
colnames(net) <- rownames(net) <- letters[1:10]
formula <- net ~ edges + ttriads + in2stars
test <- gergm(formula,
use_MPLE_only = TRUE,
estimation_method = "Metropolis",
number_of_networks_to_simulate = 40000,
thin = 1/40,
proposal_variance = 0.5,
downweight_statistics_together = TRUE,
MCMC_burnin = 10000,
seed = 456,
convergence_tolerance = .5)
check_against <- c(0.187, -0.451)
expect_equal(round(as.numeric(test@theta.coef[1,]),3), check_against)
})
test_that("Model with covariates runs", {
skip_on_cran()
skip("Weird travis errors")
set.seed(12345)
net <- matrix(runif(100,0,1),10,10)
colnames(net) <- rownames(net) <- letters[1:10]
node_level_covariates <- data.frame(Age = c(25,30,34,27,36,39,27,28,35,40),
Height = c(70,70,67,58,65,67,64,74,76,80),
Type = c("A","B","B","A","A","A","B","B","C","C"))
rownames(node_level_covariates) <- letters[1:10]
network_covariate <- net + matrix(rnorm(100,0,.5),10,10)
formula <- net ~ edges + mutual + ttriads + sender("Age") +
netcov("network_covariate") + nodemix("Type",base = "A")
test <- gergm(formula,
covariate_data = node_level_covariates,
number_of_networks_to_simulate = 100000,
thin = 1/100,
proposal_variance = 0.5,
MCMC_burnin = 50000,
seed = 456,
convergence_tolerance = 0.5)
check_against <- c(0.764, -0.071, -0.016, -0.025, -0.023, -0.056, -0.056, -0.034,
0.002, -0.039, -0.050, 3.096, 0.128, -1.933)
expect_equal(c(round(as.numeric(test@theta.coef[1,]),3),round(as.numeric(test@lambda.coef[1,]),3)), check_against)
})
test_that("Additional Model with covariates runs", {
skip_on_cran()
skip("Time")
set.seed(12345)
net <- matrix(runif(100,0,1),10,10)
colnames(net) <- rownames(net) <- letters[1:10]
node_level_covariates <- data.frame(Age = c(25,30,34,27,36,39,27,28,35,40),
Height = c(70,70,67,58,65,67,64,74,76,80),
Type = c("A","B","B","A","A","A","B","B","C","C"))
rownames(node_level_covariates) <- letters[1:10]
network_covariate <- net + matrix(rnorm(100,0,.5),10,10)
formula <- net ~ edges + mutual + ttriads + sender("Age") +
netcov("network_covariate") + nodematch("Type")
test <- gergm(formula,
covariate_data = node_level_covariates,
number_of_networks_to_simulate = 100000,
thin = 1/100,
proposal_variance = 0.5,
MCMC_burnin = 50000,
seed = 456,
convergence_tolerance = 0.5,
convex_hull_proportion = 0.9)
check_against <- c(1.339, -0.074, -0.017, -0.024, 3.098, 0.132, -1.837)
expect_equal(c(round(as.numeric(test@theta.coef[1,]),3),round(as.numeric(test@lambda.coef[1,]),3)), check_against)
})