-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomophily-figure.R
177 lines (149 loc) · 4.37 KB
/
homophily-figure.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
# Figures: Why does it make sense to include the square term?
library(ergmito)
library(network)
library(sna)
load("data/model_data.rda")
net <- NETWORKS$advice
# Removing cases with missing
net <- net[which(!(as.integer(names(net)) %in% as.integer(miss$Female)))]
net <- net[nvertex(net) > 3]
# Looking at the expected number of homophilic ties
n0 <- structure(
c(0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0),
.Dim = c(5L, 5L),
.Dimnames = list(c("A", "B", "C", "D", "E"), c("A", "B", "C", "D", "E"))
)
n0 <- network(n0)
set.vertex.attribute(n0, "Female", c(0L, 0L, 1L, 1L, 0L))
# gplot(n0, vertex.col = get.vertex.attribute(n0, "Female"))
f <- ergmito_formulae(
n0 ~ edges + nodematch("Female"),
model_update = ~ . + I(nodematch.Female^(1.25)) + I(nodematch.Female^(.5))
)
# Computing likelihoods
thetas <- seq(-5, 5, length.out = 100)
theta_par0 <- .5
# Nodematch
nm <- sapply(thetas, function(t.) {
# Calculating over all the possible states
p <- exact_loglik(
x = f$stats_statmat[[1]],
params = c(-.5, theta_par0, 0, 0),
stats_weights = f$stats_weights,
stats_statmat = f$stats_statmat
)
# Computing the expected values
sum(exp(p) * f$stats_weights[[1]] * f$stats_statmat[[1]][,1])
})
# Nodematch
nm_squared <- sapply(thetas, function(t.) {
# Calculating over all the possible states
p <- exact_loglik(
x = f$stats_statmat[[1]],
params = c(-.5, theta_par0, t., 0),
stats_weights = f$stats_weights,
stats_statmat = f$stats_statmat
)
# Computing the expected values
sum(exp(p) * f$stats_weights[[1]] * f$stats_statmat[[1]][,2])
})
nm_sqrt <- sapply(thetas, function(t.) {
# Calculating over all the possible states
p <- exact_loglik(
x = f$stats_statmat[[1]],
params = c(-.5, theta_par0, 0, t.),
stats_weights = f$stats_weights,
stats_statmat = f$stats_statmat
)
# Computing the expected values
sum(exp(p) * f$stats_weights[[1]] * f$stats_statmat[[1]][,2])
})
plot(
x = thetas, y = nm, type = "l",
ylim = range(f$stats_statmat[[1]][,2]),
lwd = 2, lty = 1,
xlab = expression(plain(Values~of)~theta),
ylab = c("Expected count of","homophilic (gender) ties")
)
lines(x = thetas, y =nm_sqrt, lwd = 2, col = "blue", lty = 2)
lines(x = thetas, y =nm_squared, lwd = 2, col = "red", lty = 3)
legend(
"topleft",
legend = expression(
Homophily,
Homophily^(1/2),
Homophily^2
),
lty = 1L:3L, lwd = 2,
col = c("black", "blue", "red"),
bty = "n"
)
# Calculating probabilities of observing certain figures
counts <- sort(unique(f$stats_statmat[[1]][,2]))
theta_par <- -.5
# Nodematch
nm <- sapply(counts, function(t.) {
# Calculating over all the possible states
p <- exact_loglik(
x = f$stats_statmat[[1]],
params = c(-.5, theta_par0, 0, 0),
stats_weights = f$stats_weights,
stats_statmat = f$stats_statmat
)
# Computing the expected values
idx <- which(f$stats_statmat[[1]][,2] == t.)
sum(exp(p[idx]) * f$stats_weights[[1]][idx])
})
# Nodematch
nm_squared <- sapply(counts, function(t.) {
# Calculating over all the possible states
p <- exact_loglik(
x = f$stats_statmat[[1]],
params = c(-.5, theta_par0, theta_par, 0),
stats_weights = f$stats_weights,
stats_statmat = f$stats_statmat
)
# Computing the expected values
idx <- which(f$stats_statmat[[1]][,2] == t.)
sum(exp(p[idx]) * f$stats_weights[[1]][idx])
})
nm_sqrt <- sapply(counts, function(t.) {
# Calculating over all the possible states
p <- exact_loglik(
x = f$stats_statmat[[1]],
params = c(-.5, theta_par0, 0, theta_par),
stats_weights = f$stats_weights,
stats_statmat = f$stats_statmat
)
# Computing the expected values
idx <- which(f$stats_statmat[[1]][,2] == t.)
sum(exp(p[idx]) * f$stats_weights[[1]][idx])
})
plot(
x = counts, y = nm, type = "l",
ylim = c(0, .4),
lwd = 2, lty = 1,
xlab = c("Count of homophilic (gender) ties"),
ylab = c("Marginal CDF for","homophilic (gender) ties")
)
lines(x = counts, y =nm_sqrt, lwd = 2, col = "blue", lty = 2)
lines(x = counts, y =nm_squared, lwd = 2, col = "red", lty = 3)
legend(
"topleft",
legend = expression(
Homophily,
Homophily^(1/2),
Homophily^2
),
lty = 1L:3L, lwd = 2,
col = c("black", "blue", "red"),
bty = "n"
)
legend(
"topright",
legend = expression(
plain(All~values~of)~theta==0.5,
theta[plain(edges)]==-0.5
),
bty = "n"
)