-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathem-mixture-mv.Rmd
301 lines (209 loc) · 6.99 KB
/
em-mixture-mv.Rmd
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
## Multivariate Mixture Model
The following code is based on algorithms noted in Murphy, 2012 Probabilistic Machine Learning.
Specifically, Chapter 11, section 4.
### Function
This estimating function will be used for both examples.
```{r em_mixture-mv}
em_mixture <- function(
params,
X,
clusters = 2,
tol = .00001,
maxits = 100,
showits = TRUE
) {
# Arguments are
# params: starting parameters (means, covariances, cluster probability)
# X: data
# clusters: number of clusters desired
# tol: tolerance
# maxits: maximum iterations
# showits: whether to show iterations
require(mvtnorm)
# Starting points
N = nrow(X)
mu = params$mu
var = params$var
probs = params$probs
# initializations
# cluster 'responsibilities', i.e. probability of cluster membership for each
# observation i
ri = matrix(0, ncol=clusters, nrow=N)
ll = 0 # log likelihood
it = 0 # iteration count
converged = FALSE # convergence
# Show iterations if showits == true
if (showits)
cat(paste("Iterations of EM:", "\n"))
while (!converged & it < maxits) {
probsOld = probs
# muOld = mu # Use direct values or loglike for convergence check
# varOld = var
llOld = ll
riOld = ri
### E
# Compute responsibilities
for (k in 1:clusters){
ri[,k] = probs[k] * dmvnorm(X, mu[k, ], sigma = var[[k]], log = FALSE)
}
ri = ri/rowSums(ri)
### M
rk = colSums(ri) # rk is weighted average cluster membership size
probs = rk/N
for (k in 1:clusters){
varmat = matrix(0, ncol = ncol(X), nrow = ncol(X)) # initialize to sum matrices
for (i in 1:N){
varmat = varmat + ri[i,k] * X[i,]%*%t(X[i,])
}
mu[k,] = (t(X) %*% ri[,k]) / rk[k]
var[[k]] = varmat/rk[k] - mu[k,]%*%t(mu[k,])
ll[k] = -.5*sum( ri[,k] * dmvnorm(X, mu[k,], sigma = var[[k]], log = TRUE) )
}
ll = sum(ll)
# compare old to current for convergence
parmlistold = c(llOld, probsOld) # c(muOld, unlist(varOld), probsOld)
parmlistcurrent = c(ll, probs) # c(mu, unlist(var), probs)
it = it + 1
# if showits true, & it =1 or modulo of 5 print message
if (showits & it == 1 | it%%5 == 0)
cat(paste(format(it), "...", "\n", sep = ""))
converged = min(abs(parmlistold - parmlistcurrent)) <= tol
}
clust = which(round(ri) == 1, arr.ind = TRUE) # create cluster membership
clust = clust[order(clust[,1]), 2] # order accoring to row rather than cluster
list(
probs = probs,
mu = mu,
var = var,
resp = ri,
cluster = clust,
ll = ll
)
}
```
### Example 1: Old Faithful
This example uses Old Faithful geyser eruptions as before. This is can be compared to the univariate code from the [other chapter][Mixture Model]. See also http://www.geyserstudy.org/geyser.aspx?pGeyserNo=OLDFAITHFUL
#### Data Setup
```{r mixmv-setup1}
library(tidyverse)
data("faithful")
```
#### Estimation
Create starting values and estimate.
```{r mixmv-starts1}
mustart = rbind(c(3, 60), c(3, 60.1)) # must be at least slightly different
covstart = list(cov(faithful), cov(faithful))
probs = c(.01, .99)
# params is a list of mu, var, and probs
starts = list(mu = mustart, var = covstart, probs = probs)
```
```{r mixmv-est1}
mix_faithful = em_mixture(
params = starts,
X = as.matrix(faithful),
clusters = 2,
tol = 1e-12,
maxits = 1500,
showits = TRUE
)
str(mix_faithful)
```
Visualize.
```{r mixmv-vis1, echo=FALSE}
library(ggplot2)
faithful %>%
mutate(cluster = factor(mix_faithful$cluster)) %>%
ggplot(aes(x = eruptions, y = waiting) ) +
geom_density2d(color = 'gray92') +
geom_point(aes(color = cluster)) +
scico::scale_color_scico_d(begin = .25, end = .75, alpha = .5) +
guides(color = guide_legend('Cluster'))
faithful %>%
mutate(prob_clus_1 = mix_faithful$resp[, 1]) %>%
ggplot(aes(x = eruptions, y = waiting)) +
geom_density2d(color = 'gray92') +
geom_point(aes(color = prob_clus_1)) +
scico::scale_color_scico(palette = 'bilbao', begin = .25) +
guides(color = guide_legend('Prob. Cluster 1'))
# relatively speaking, these are extremely well-separated clusters
worst = apply(mix_faithful$resp, 1, function(x) max(x) < .99)
ggplot(aes(x = eruptions, y = waiting), data = faithful) +
geom_point(aes(color = worst)) +
scico::scale_color_scico_d(palette = 'bilbao', begin = .25)
```
#### Comparison
Compare to <span class="pack" style = "">mclust</span> results. Options are set to be more similar to the settings demonstrated.
```{r mixmv-compare1}
library(mclust)
mix_mclust = mclust::Mclust(
faithful[, 1:2],
2,
modelNames = 'VVV',
control = emControl(tol = 1e-12)
)
detach(package:mclust)
# str(mix_mclust, 1)
```
Compare means.
```{r mixmv-compare1-means}
t(mix_faithful$mu)
mix_mclust$parameters$mean
```
Compare variances.
```{r mixmv-compare1-vars}
mix_faithful$var
mix_mclust$parameters$variance$sigma
```
Compare classifications. Reverse in case arbitrary labeling of one of the clusters is opposite.
```{r mixmv-compare1-class}
table(mix_faithful$cluster, mix_mclust$classification)
table(ifelse(mix_faithful$cluster == 2, 1, 2),
mix_mclust$classification)
# compare responsibilities; reverse one if arbitrary numbering of one of them is opposite
# cbind(round(mix_faithful$resp[,1], 2), round(mix_mclust$z[,2], 2)) # cluster '1'
# cbind(round(mix_faithful$resp[,2], 2), round(mix_mclust$z[,1], 2)) # cluster '2'
```
### Example 2: Iris
#### Data Setup
Set up the data.
```{r mixmv-setup2}
iris2 = iris %>% select(-Species)
```
#### Estimation
Run and examine. We add noise to our starting value, and the function is notably sensitive to starts, but we don't want to cheat too badly.
```{r mixmv-starts2}
mustart = iris %>%
group_by(Species) %>%
summarise(across(.fns = function(x) mean(x) + runif(1, 0, .5))) %>%
select(-Species) %>%
as.matrix()
# use purrr::map due to mclust::map masking
covstart = iris %>%
split(.$Species) %>%
purrr::map(select, -Species) %>%
purrr::map(function(x) cov(x) + diag(runif(4, 0, .5)))
probs = c(.1, .2, .7)
starts = list(mu = mustart, var = covstart, probs = probs)
```
```{r mixmv-est2}
mix_mclust_iris = em_mixture(
params = starts,
X = as.matrix(iris2),
clusters = 3,
tol = 1e-8,
maxits = 1500,
showits = T
)
table(mix_mclust_iris$cluster, iris$Species)
```
#### Comparison
Compare to <span class="pack" style = "">mclust</span> results.
```{r mixmv-compare2}
library(mclust)
mclust_iris = mclust::Mclust(iris[,1:4], 3)
table(mclust_iris$classification, iris$Species)
detach(package:mclust)
```
### Source
Original code available at
https://github.com/m-clark/Miscellaneous-R-Code/blob/master/ModelFitting/EM%20Examples/EM%20Mixture%20MV.R