if(!require(MASS)){install.packages("MASS"); library(MASS)}
ifname<-"DataAgonist.csv"
dataAll <- read.delim(ifname, row.names=1,header=T, sep= ",", as.is=TRUE, 
                      strip.white=FALSE)
nf<-dim(dataAll)[[2]]-1
#[1]----------------------------------------------------------------------
results.ridge <- lm.ridge(y~., data=dataAll, lambda = seq(0., 10, .2)) 
seq(0., 10, .2); names(results.ridge)
#[2]----------------------------------------------------------------------
coef(results.ridge)
matplot(seq(0., 10, .2),coef(results.ridge)[,-c(1)],pch=1:nf,col=1,
        xlab="lambda",ylab="coefficients",main="Lambda vs. coefficients")
par(ps = 10,mgp=c(1., 0.1, 0.1)); par(mai=c(.8,.8,.8,1.2),xpd=TRUE)
legend(legend=names(results.ridge$xm),pch=1:nf,col=1,"topright",ncol=2)
#[3]--------------------------------------------------------------------------
matplot(seq(0., 10, .2),coef(results.ridge)[,-c(1)],pch=1:nf,col=1,
        xlab="lambda",ylab="coefficients")
par(ps = 10,mgp=c(1.0, 0.1, 0.1)); par(mai=c(.8,.8,.8,1.2),xpd=TRUE)
par(new=TRUE)
plot(seq(0.,10,.2),results.ridge$GCV,type="l",axes=FALSE,
     xlab="",ylab="",col=1,main="Lambda vs. coefficients")
axis(4)
legend(legend=names(results.ridge$xm),pch=1:nf,col=1,"topright",ncol=2)
#[4]- Best Ridge -----------------------
whichIsBest<-which.min(results.ridge$GCV); whichIsBest
results.ridge$GCV[whichIsBest[[1]]]
oplambda<-as.numeric(names(whichIsBest))
best.ridge <- lm.ridge(y~., data=dataAll, lambda = oplambda); best.ridge
pred.ridge <- sapply(1:nrow(dataAll), function(i){
  sum(dataAll[i, 2:nf+1]*coef(best.ridge)[2:nf+1],coef(best.ridge)[1])
}); pred.ridge
s <- paste("Ridge: r = ",sprintf("%5.3f",cor(dataAll[,1],pred.ridge)))
plot(dataAll[,1],pred.ridge,xlab="original",ylab="prediction",main=s)
cor(dataAll[,1],pred.ridge)