############################################################
### Plot ridge coeficient vs lambda so you can see the nature of the shinkage.
############################################################
#dpl=FALSE
dpl=TRUE
library(glmnet)
################################################
if(1) {cat("### read in data\n")
ddf = read.csv("diabetes.csv")
y = ddf$y
x = as.matrix(ddf[,2:ncol(ddf)])
}
################################################
if(1) {cat("### see lasso\n")

## fit with just two orthogonal x's, see that 1/bhat(lambda) is linear in lambda
x1 = x[,3]; x2 = x[,4]
x2.1 = x2-(sum(x1*x2)/sum(x1^2))*x1
x2.1 = scale(x2.1)
XX = cbind(x1,x2.1)
lv = 10:0 #lambda vector

dgnR2 = glmnet(XX,y,family="gaussian",standardize=FALSE,intercept=FALSE,alpha=0,lambda=lv)
bhat = sum(x1*y)/sum(x1^2)
bhRI = bhat/dgnR2$beta[1,]

if(dpl) pdf(file="see-ridge.pdf",width=12,height=10)
plot(lv,bhRI,cex.axis=1.5,cex.lab=1.5,type="b",col="blue",xlab="lambda",ylab="lsbhat/ridgehbat")
if(dpl) dev.off()

}
###############################################
if(dpl) rm(list=ls())
