##################################################################### ### BMTRY 790: MACHINE LEARNING AND DATA MINING, Spring 2023 ### ### ### ### Lecture 5: Penalized Regression, Part III ### ### ### ### Body Fat Data Analysis ### ### ### ### Looking at elastic netrelative to lasso using the body fat ### ### data ### ##################################################################### bodyfat<-read.csv("H:/public_html/BMTRY790_Spring2023/Datasets/Body_fat.csv")[,-1] bodyfat2<-scale(bodyfat) library(elasticnet) ################################################### ### Fitting Elastic Net Model to Bodyfat data ### ################################################### ### First conducting 10-fold CV to select our tuning parmaters ### par(mfrow=c(2,3)) set.seed(3210) menet0<-cv.enet(x=bodyfat2[,2:14], y=bodyfat2[,1], s=seq(0,1,length=100), lambda=0, mode="fraction") set.seed(3210) menet001<-cv.enet(x=bodyfat2[,2:14], y=bodyfat2[,1], s=seq(0,1,length=100), lambda=0.01, mode="fraction") set.seed(3210) menet01<-cv.enet(x=bodyfat2[,2:14], y=bodyfat2[,1], s=seq(0,1,length=100), lambda=0.1, mode="fraction") set.seed(3210) menet1<-cv.enet(x=bodyfat2[,2:14], y=bodyfat2[,1], s=seq(0,1,length=100), lambda=1, mode="fraction") set.seed(3210) menet10<-cv.enet(x=bodyfat2[,2:14], y=bodyfat2[,1], s=seq(0,1,length=100), lambda=10, mode="fraction") set.seed(3210) menet100<-cv.enet(x=bodyfat2[,2:14], y=bodyfat2[,1], s=seq(0,1,length=100), lambda=100, mode="fraction") ### Looking at minimum cv error for each choice of lambda_2 min(menet0$cv) min(menet001$cv) min(menet01$cv) min(menet1$cv) min(menet10$cv) min(menet100$cv) ### Selecting the lambda_1 from the model with the smallest cv.error fracL1<-menet0$s[which(menet0$cv==min(menet0$cv))] ### Comparing elastic net path to lasso path mod.enet<-enet(x=bodyfat2[,2:14], y=bodyfat2[,1], lambda=0) plot(mod.enet, use.color=T, main="Body Fat Example") abline(v=fracL1, lty=2, col=2, lwd=2) ### extracting the model based on the selected lambda_2 and fraction of L1 predict(mod.enet, s=fracL1, type="coefficients", mode="fraction")