library(MASS)
data(longley)
help(longley)
names(longley)
longley

plot(longley)

LM1<-lm(Employed~GNP+Unemployed+Armed.Forces+Population+GNP.deflator,data=longley)
summary(LM1)
LM2<-lm(Employed ~ ., longley)
summary(LM2)
help(lm.ridge)
LMR1<-lm.ridge(Employed ~ ., longley)

lam.seq<-seq(0,0.01,0.0001)
LMR2<-lm.ridge(Employed ~ ., longley,lambda = lam.seq)

plot(LMR2)
select(LMR2)

?gcv.methods
plot(lam.seq,LMR2$GCV,type="l",ylab="GCV")

##########
library(lasso2)

help(l1ce)
data(Prostate)
help(Prostate)
names(Prostate)
attach(Prostate)




l1c.P1 <- l1ce(lpsa ~ ., Prostate, bound=(1:3)/3)

l1c.P1 
coef(l1c.P1)
coef(l1c.P1)[,1]


l1c.P <- l1ce(lpsa ~ ., Prostate, bound=(1:30)/30)
bound<-(1:30)/30
##### Figure 3.9 in ElemStatLearn #########
plot(bound,coef(l1c.P)[,2],xlab="Shrinkage factor s",ylim=c(-0.3,0.6), ylab="Coefficients", main="Prostate data set: Lasso estimators", col=2)
lines(bound,coef(l1c.P)[,2],col=2)
points(bound,coef(l1c.P)[,3],col=3)
lines(bound,coef(l1c.P)[,3],col=3)
points(bound,coef(l1c.P)[,4],col=4)
lines(bound,coef(l1c.P)[,4],col=4)
points(bound,coef(l1c.P)[,5],col=5)
lines(bound,coef(l1c.P)[,5],col=5)
points(bound,coef(l1c.P)[,6],col=6)
lines(bound,coef(l1c.P)[,6],col=6)
points(bound,coef(l1c.P)[,7],col=7)
lines(bound,coef(l1c.P)[,7],col=7)
points(bound,coef(l1c.P)[,8],col=8)
lines(bound,coef(l1c.P)[,8],col=8)
points(bound,coef(l1c.P)[,9],col=9)
lines(bound,coef(l1c.P)[,9],col=9)
legend(locator(n=1),legend=c("lcavol","lweight","age","lbph","svi","lcp","gleason","pgg45"),lwd=2,col=2:9)

### Compare with ridge- Prostate  #################
attach(Prostate)
names(Prostate)
lam.seq<-seq(0,100,.1)
L2<-lm.ridge(lpsa ~ lcavol+lweight+age+lbph+svi+lcp+gleason+pgg45, Prostate,
              lambda = lam.seq)

plot(lm.ridge(lpsa ~ lcavol+lweight+age+lbph+svi+lcp+gleason+pgg45, Prostate,
              lambda= lam.seq), main="Prostate data set: Ridge Estimator")

select(L2)

plot(lam.seq,coef(L2)[,1],type="l")















