-
Notifications
You must be signed in to change notification settings - Fork 1
/
mixem.Rmd
51 lines (45 loc) · 1.62 KB
/
mixem.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
# A small script to illustrate application of the DAAREM method for
# computing maximum-likelihood estimates of mixture proportions in a
# mixture model.
# SET UP ENVIRONMENT
# ------------------
library(ggplot2)
library(cowplot)
library(daarem)
source("misc.R")
source("mixem.R")
# LOAD DATA
# ---------
cat("Loading data.\n")
load("../data/mixdata.RData")
n <- nrow(L)
m <- ncol(L)
cat(sprintf("Loaded %d x %d data matrix.\n",n,m))
# Set initial estimate of mixture proportions.
x0 <- rep(1/m,m)
# RUN BASIC EM
# ------------
cat("Fitting mixture model with basic EM method.\n")
out <- system.time(fit1 <- mixem(L,x0,numiter = 200))
f1 <- mixobjective(L,fit1$x)
cat(sprintf("Computation took %0.2f seconds.\n",out["elapsed"]))
cat(sprintf("Log-likelihood at EM estimate is %0.12f.\n",f1))
# RUN ACCELERATED EM
# ------------------
cat("Fitting mixture model with accelerated EM method (DAAREM).\n")
out <- system.time(fit2 <- mixdaarem(L,x0,numiter = 200))
f2 <- mixobjective(L,fit2$x)
cat(sprintf("Computation took %0.2f seconds.\n",out["elapsed"]))
cat(sprintf("Objective value at DAAREM estimate is %0.12f.\n",f2))
# PLOT IMPROVEMENT IN SOLUTION OVER TIME
# --------------------------------------
f <- mixobjective(L,x)
pdat <-
rbind(data.frame(iter = 1:200,dist = f - fit1$value,method = "EM"),
data.frame(iter = 1:200,dist = f - fit2$value,method = "DAAREM"))
p <- ggplot(pdat,aes(x = iter,y = dist,col = method)) +
geom_line(size = 1) +
scale_y_continuous(trans = "log10",breaks = 10^seq(-4,4)) +
scale_color_manual(values = c("darkorange","dodgerblue")) +
labs(x = "iteration",y = "distance from solution")
print(p)