-
Notifications
You must be signed in to change notification settings - Fork 8
/
2.11-ridge_regression.Rmd
127 lines (94 loc) · 3.96 KB
/
2.11-ridge_regression.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
# Ridge Regression {#ridge_regression}
THIS CHAPTER IS UNDER CONSTRUCTION!!!
> We should provide an example in Stan.
## Introduction
```{r, eval=FALSE}
# Settings
library(R2OpenBUGS)
bugslocation <- "C:/Program Files/OpenBUGS323/OpenBugs.exe" # location of OpenBUGS
bugsworkingdir <- file.path(getwd(), "BUGS") # Bugs working directory
#-------------------------------------------------------------------------------
# Simulate fake data
#-------------------------------------------------------------------------------
library(MASS)
n <- 50 # sample size
b0 <- 1.2
b <- rnorm(5, 0, 2)
Sigma <- matrix(c(10,3,3,2,1,
3,2,3,2,1,
3,3,5,3,2,
2,2,3,10,3,
1,1,2,3,15),5,5)
Sigma
x <- mvrnorm(n = n, rep(0, 5), Sigma)
simresid <- rnorm(n, 0, sd=3) # residuals
x.z <- x
for(i in 1:ncol(x)) x.z[,i] <- (x[,i]-mean(x[,i]))/sd(x[,i])
y <- b0 + x.z%*%b + simresid # calculate y, i.e. the data
#-------------------------------------------------------------------------------
# Function to generate initial values
#-------------------------------------------------------------------------------
inits <- function() {
list(b0=runif(1, -2, 2),
b=runif(5, -2, 2),
sigma=runif(1, 0.1, 2))
}
#-------------------------------------------------------------------------------
# Run OpenBUGS
#-------------------------------------------------------------------------------
parameters <- c("b0", "b", "sigma")
lambda <- c(1, 2, 10, 25, 50, 100, 500, 1000, 10000)
bs <- matrix(ncol=length(lambda), nrow=length(b))
bse <- matrix(ncol=length(lambda), nrow=length(b))
for(j in 1:length(lambda)){
datax <- list(y=as.numeric(y), x=x, n=n, mb=rep(0, 5), lambda=lambda[j])
fit <- bugs(datax, inits, parameters, model.file="ridge_regression.txt",
n.thin=1, n.chains=2, n.burnin=5000, n.iter=10000,
debug=FALSE, OpenBUGS.pgm = bugslocation,
working.directory=bugsworkingdir)
bs[,j] <- fit$mean$b
bse[,j] <- fit$sd$b
}
range(bs)
plot(1:length(lambda), seq(-2, 1, length=length(lambda)), type="n")
colkey <- rainbow(length(b))
for(j in 1:nrow(bs)){
lines(1:length(lambda), bs[j,], col=colkey[j], lwd=2)
lines(1:length(lambda), bs[j,]-2*bse[j,], col=colkey[j], lty=3)
lines(1:length(lambda), bs[j,]+2*bse[j,], col=colkey[j], lty=3)
}
abline(h=0)
round(fit$summary,2)
#-------------------------------------------------------------------------------
# Run WinBUGS
#-------------------------------------------------------------------------------
library(R2WinBUGS)
bugsdir <- "C:/Users/fk/WinBUGS14" #
mod <- bugs(datax, inits= inits, parameters,
model.file="normlinreg.txt", n.chains=2, n.iter=1000,
n.burnin=500, n.thin=1, debug=TRUE,
bugs.directory=bugsdir, program="WinBUGS", working.directory=bugsworkingdir)
#-------------------------------------------------------------------------------
# Test convergence and make inference
#-------------------------------------------------------------------------------
library(blmeco)
# Make Figure 12.2
par(mfrow=c(3,1))
historyplot(fit, "beta0")
historyplot(fit, "beta1")
historyplot(fit, "sigmaRes")
# Parameter estimates
print(fit$summary, 3)
# Make predictions for covariate values between 10 and 30
newdat <- data.frame(x=seq(10, 30, length=100))
Xmat <- model.matrix(~x, data=newdat)
predmat <- matrix(ncol=fit$n.sim, nrow=nrow(newdat))
for(i in 1:fit$n.sim) predmat[,i] <- Xmat%*%c(fit$sims.list$beta0[i], fit$sims.list$beta1[i])
newdat$lower.bugs <- apply(predmat, 1, quantile, prob=0.025)
newdat$upper.bugs <- apply(predmat, 1, quantile, prob=0.975)
plot(y~x, pch=16, las=1, cex.lab=1.4, cex.axis=1.2, type="n", main="")
polygon(c(newdat$x, rev(newdat$x)), c(newdat$lower.bugs, rev(newdat$upper.bugs)), col=grey(0.7), border=NA)
abline(c(fit$mean$beta0, fit$mean$beta1), lwd=2)
box()
points(x,y)
```