-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBayesian Lasso.R
132 lines (107 loc) · 3.86 KB
/
Bayesian Lasso.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
###################################################################################
###################################################################################
library(care)
#loading the data
data("efron2004")
dim(efron2004$x) # 442 10
colnames(efron2004$x)
length(efron2004$y) # 442
#head(efron2004)
##################################################################################
##################################################################################
#Full conditional posterior distributions
library(MASS)
library(statmod)
updateBeta <- function(Y,X,Sig2,Tau){
D <- solve(diag(Tau))
sig.post <- Sig2 * solve(t(X)%*%X + D)
m.post <- sig.post %*% t(X) %*% Y
return(mvrnorm(1,m.post,sig.post))
}
updateSig2 <- function(a,b,Y,X,Beta,Tau){
N <- length(Y)
p <- length(Beta)
D <- solve(diag(Tau))
a.post <- (N - 1 + p)*0.5 + a
b.post <- 0.5*(t(Y - X%*%Beta)%*%(Y - X%*%Beta) + t(Beta)%*%D%*%Beta) + b
return(1/rgamma(1,a.post,b.post))
}
updateTau <- function(Lambda,Sig2,Beta){
dispersion <- Lambda^2
mu <- sqrt(Lambda^2 * Sig2 / Beta^2)
return(1/rinvgauss(length(mu),mean = mu, dispersion = dispersion))
}
updateLambda <- function(p,r,Delta,Tau){#p is length of Beta
a.post <- p + r
b.post <- sum(Tau^2)*0.5 + Delta
return(sqrt(rgamma(1,a.post,b.post)))
}
#################################################################################
#Number of iterations
Niter <- 10000
#Variables
Y <- efron2004$y
X <- efron2004$x
#Creating matrices that will store MCMC samples
Beta.out <- array(NA, dim = c(Niter, dim(efron2004$x)[2]))
Tau.out <- array(NA, dim = c(Niter, dim(efron2004$x)[2]))
Sig.out <- array(NA, dim = Niter)
Lambda.out <- array(NA, dim = Niter)
#Initial Values
Beta.out[1,] <- rep(1,dim(efron2004$x)[2])
Tau.out[1,] <- rep(1,dim(efron2004$x)[2])
Sig.out[1] <- 1
Lambda.out[1] <- 1
#Gibbs sampler
for(i in 2:Niter){
Beta.out[i,] <- updateBeta(Y,X,Sig.out[i-1],Tau.out[i-1,])
Tau.out[i,] <- updateTau(Lambda.out[i-1],Sig.out[i-1],Beta.out[i,])
Sig.out[i] <- updateSig2(0.01,0.01,Y,X,Beta.out[i,],Tau.out[i,])
#the values r = 1 and Delta = 1.78 are discussed in the paper
Lambda.out[i] <- updateLambda(length(Beta.out[1,]),1,1.78,Tau.out[i,])
print(i)
}
###################################################################################
#Burn-in phase
Nburn <- 1000
Beta <- Beta.out[-(1:Nburn),]
plot(Beta[,1],type = 'l')
median(Beta[,1])
quantile(Beta[,1],probs=c(0.025,0.975))
plot(Beta[,2],type = 'l')
median(Beta[,2])
quantile(Beta.out[,2],probs=c(0.025,0.975)) #Doesn't include zero
plot(Beta[,3],type = 'l') #Doesn't include zero
median(Beta[,3])
quantile(Beta.out[,3],probs=c(0.025,0.975))
plot(Beta[,4],type = 'l') #Doesn't include zero
median(Beta[,4])
quantile(Beta.out[,4],probs=c(0.025,0.975))
plot(Beta[,5],type = 'l')
median(Beta[,5])
quantile(Beta.out[,5],probs=c(0.025,0.975))
plot(Beta[,6],type = 'l')
median(Beta[,6])
quantile(Beta.out[,6],probs=c(0.025,0.975))
plot(Beta[,7],type = 'l')
median(Beta[,7])
quantile(Beta.out[,7],probs=c(0.025,0.975))
plot(Beta[,8],type = 'l')
median(Beta[,8])
quantile(Beta.out[,8],probs=c(0.025,0.975))
plot(Beta[,9],type = 'l') #Doesn't include zero
median(Beta[,9])
quantile(Beta.out[,9],probs=c(0.025,0.975))
plot(Beta[,10],type = 'l')
median(Beta[,10])
quantile(Beta.out[,10],probs=c(0.025,0.975))
#################################################################################
#Plot similar to figure 2 of the paper, showing the same results were obtained
plot(1, type="n", xlab="", ylab="", xlim=c(-1, 1), ylim=c(11, 1),
main = "Diabetes data intervals")
abline(v = 0, lty = 2)
for(i in 10:1){
lines(x = c(quantile(Beta[,i], probs = c(0.025)),
quantile(Beta[,i], probs = c(0.975))), y = c(i,i))
points(median(Beta[,i]), y = i, pch = 10)
}