-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnull_distr_v2.R
134 lines (116 loc) · 3.51 KB
/
null_distr_v2.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
# empirical distribution of SNAC and SNAC+
library(ggplot2)
library(EnvStats)
library(nett)
library(dplyr)
library(fastRG) # used to simulate Poisson DCSBM
set.seed(123)
n <- 10000 # number of nodes
K <- 3 # number of communities
beta <- 0.05 # out-in ratio
lambda <- 40 # average degrees
Pi <- rep(1,K)/K # distribution of cluster labels
B <- pp_conn(n, beta, lambda, Pi)$B
z <- sample(K, n, replace=T, prob=Pi)
theta <- rpareto(n, 3/4, 4)
n <- length(theta)
ave_deg <- lambda
boot_nac <- function(z, K, nrep = 2000, dcsbm_boot = T) {
boot_samp <- sapply(1:nrep, function(i) {
Aboot_ber <- nett::fast_sbm(z, B)
model_sbm <- sbm(n, K, B, expected_degree = ave_deg)
# # zboot <- spec_clust(Aboot, K)
Aboot <- sample_sparse(model_sbm)
model_dcsbm <- dcsbm(theta = theta, B = B, expected_degree = ave_deg)
Aboot_DC <- sample_sparse(model_dcsbm)
Aboot_DC_ber <- nett::sample_dcsbm(z, B, theta = theta)
# zboot_DC <- spec_clust(Aboot_DC, K)
c(
snac_test(Aboot, K)$stat,
snac_test(Aboot_ber, K)$stat,
snac_test(Aboot_DC, K)$stat,
snac_test(Aboot_DC_ber, K)$stat
# adj_spec_test(Aboot, K, zboot)
)
})
return(boot_samp)
}
res <- boot_nac(z, K)
res <- data.frame(SBM = res[1, ], berSBM = res[2, ], DCSBM = res[3, ], berDCSBM = res[4, ] )
ggplot(res, aes(x = SBM)) +
theme_bw() + xlab("SNAC+")+
geom_histogram(aes(y =..density..),
colour = "black",
fill = "white",
bins = 20)+
stat_function(fun = dnorm, args = list(mean = 0, sd = 1),
color = "red", size = 2)+
theme(
text = element_text(size=26)
)
# ggsave(paste0("SBM_null_hist.pdf"), width = 8.22, height = 6.94)
ggplot(res, aes(x = DCSBM)) +
theme_bw() + xlab("SNAC+")+
geom_histogram(aes(y =..density..),
colour = "black",
fill = "white",
bins = 20)+
stat_function(fun = dnorm, args = list(mean = 0, sd = 1),
color = "red", size = 2)+
theme(
text = element_text(size=26)
)
# ggsave(paste0("DCSBM_null_hist.pdf"), width = 8.22, height = 6.94)
ggplot(res, aes(x = berSBM)) +
theme_bw() + xlab("SNAC+")+
geom_histogram(aes(y =..density..),
colour = "black",
fill = "white",
bins = 20)+
stat_function(fun = dnorm, args = list(mean = 0, sd = 1),
color = "red", size = 2)+
theme(
text = element_text(size=26)
)
# ggsave(paste0("berSBM_null_hist.pdf"), width = 8.22, height = 6.94)
ggplot(res, aes(x = berDCSBM)) +
theme_bw() + xlab("SNAC+")+
geom_histogram(aes(y =..density..),
colour = "black",
fill = "white",
bins = 20)+
stat_function(fun = dnorm, args = list(mean = 0, sd = 1),
color = "red", size = 2)+
theme(
text = element_text(size=26)
)
# ggsave(paste0("berDCSBM_null_hist.pdf"), width = 8.22, height = 6.94)
mean(res$DCSBM)
#-0.0477519
sd(res$DCSBM)
# 1.043478
mean(res$SBM)
# [1] -0.08605762
sd(res$SBM)
# [1] 0.9863768
shapiro.test(res$DCSBM)
shapiro.test(res$SBM)
ks.test(res$DCSBM, "pnorm")
#p-value = 0.01563
ks.test(res$SBM, "pnorm")
# p-value = 0.009916
## strangely, the KS test has large pvalue when there are
## less data points
# Bernoulli version
ks.test(res$berDCSBM, "pnorm")
ks.test(res$berSBM, "pnorm")
shapiro.test(res$berDCSBM)
shapiro.test(res$berSBM)
mean(res$berDCSBM)
# [1] -0.2915549
sd(res$berDCSBM)
# [1] 1.022245
mean(res$berSBM)
# [1] -0.2571552
sd(res$berSBM)
# [1] 0.9948815