/
em_vs_mixsqp.Rmd
148 lines (118 loc) · 3.82 KB
/
em_vs_mixsqp.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
---
title: Comparing two different ways of estimating the mixture weights in mr-ash
author: Peter Carbonetto
date: February 20, 2020
site: workflowr::wflow_site
output: workflowr::wflow_html
---
Here we compare two different ways of updating the mixture weights
in mr-ash: using EM, and using mix-SQP.
```{r knitr, echo=FALSE}
knitr::opts_chunk$set(comment = "#",results = "hold",collapse = TRUE,
fig.align = "center")
```
## Script parameters
These are the data simulation settings.
I suggest trying this example with `s = 0` (all predictors are
independent) and `s = 0.4` (the correlation between all predictors
is 0.4).
```{r datasim-params}
n <- 500
p <- 100
sd <- c(0, 1, 2)
w <- c(0.9, 0.05, 0.05)
s <- 0
```
This specifies the variances for the mixture-of-normals prior on the
regression coefficients.
```{r set-s0}
s0 <- 10^seq(-4,0,length.out = 12)
```
## Load functions
These are the packages used in this analysis.
```{r load-pkgs, message=FALSE, warning=FALSE}
library(ggplot2)
library(cowplot)
library(MASS)
library(mixsqp)
```
This R code provides a very simple implementation of the mr-ash
algorithm.
```{r load-functions}
source("../code/misc.R")
source("../code/mr_ash.R")
```
## Simulate data
The predictors are drawn from the multivariate normal with zero mean
and covariance matrix S, in which all diagonal entries are 1, and
all off-diagonal entries are s. Setting `s = 0.5` reproduces the
simulation of the predictors used in Example 3 of Zou & Hastie
(2005).
```{r simulate-data}
set.seed(2)
S <- matrix(s,p,p)
diag(S) <- 1
X <- mvrnorm(n,rep(0,p),S)
k <- sample(length(w),p,replace = TRUE,prob = w)
beta <- sd[k] * rnorm(p)
y <- drop(X %*% beta + rnorm(n))
```
## Fit model
These are the initial estimates of residual variance (`s`), mixture
weights (`w0`), and posterior mean estimates of the regression
coefficients (b).
```{r init-model}
k <- length(s0)
se <- 1
w0 <- rep(1/k,k)
b <- rep(0,p)
```
Fit the model by running 200 EM updates for the mixture weights.
```{r fit-model-em}
fit1 <- mr_ash(X,y,se,s0,w0,b,maxiter = 200,verbose = FALSE)
```
Fit the model a second time using the mix-SQP updates for the mixture
weights.
```{r fit-model-mixsqp}
fit2 <- mr_ash_with_mixsqp(X,y,se,s0,w0,b,numiter = 10,maxiter.inner = 50,
tol.inner = 1e-8)
```
## Review model fit
Compare the posterior mean estimates from the two fits.
```{r, fig.height=3, fig.width=3}
ggplot(data.frame(em = fit1$b,mixsqp = fit2$b),
aes(x = em,y = mixsqp)) +
geom_point(color = "darkblue") +
geom_abline(intercept = 0,slope = 1,col = "magenta",lty = "dotted") +
labs(title = "coefs") +
theme_cowplot()
```
Compare the posterior mean estimates against the values used to
simulate the data.
```{r, fig.height=3, fig.width=3}
ggplot(data.frame(true = beta,mixsqp = fit2$b),
aes(x = true,y = mixsqp)) +
geom_point(color = "darkblue") +
geom_abline(intercept = 0,slope = 1,col = "magenta",lty = "dotted") +
labs(title = "coefs") +
theme_cowplot()
```
Plot the improvement in the solution over time. The EM updates are
shown in blue, and the mix-SQP updates are shown in orange.
```{r fig.height=4, fig.width=4}
elbo.best <- max(c(fit1$elbo,fit2$elbo))
pdat <- rbind(data.frame(update = "em",
iter = 1:length(fit1$elbo),
elbo = fit1$elbo),
data.frame(update = "mixsqp",
iter = cumsum(fit2$niter),
elbo = fit2$elbo))
pdat$elbo <- elbo.best - pdat$elbo + 1e-4
ggplot(pdat,aes(x = iter,y = elbo,color = update)) +
geom_line() +
geom_point() +
scale_y_log10() +
scale_color_manual(values = c("royalblue","darkorange")) +
labs(y = "distance to \"best\" elbo",title = paste("s =",s)) +
theme_cowplot()
```