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
| if(F){ library(ggsci) library(tidyverse) library(reshape2) cvfit<-lasso_cv aaa <- data.frame(lamda = cvfit[["lambda"]]%>%log()%>%round(.,3), cvm = cvfit[["cvm"]], cvup = cvfit[["cvup"]], cvlo = cvfit[["cvlo"]], nzero = as.numeric(cvfit[["nzero"]])) aaa$nzero2 <- aaa$nzero aaa$nzero2 <- factor(aaa$nzero2) xbreaks <- aaa$lamda[seq(1,100,10)] xlabels <- as.character(aaa$nzero[seq(1,100,10)]) figure3C1 <- ggplot(data = aaa)+ geom_errorbar(aes(x=lamda,ymin = cvlo,ymax = cvup,color = nzero2), linewidth = .5,width = 0.1,alpha = 0.5)+ geom_point(aes(x=lamda,y = cvm,color = nzero2))+ scale_color_d3(c('category20'))+ scale_x_continuous(sec.axis = sec_axis(~.,breaks = xbreaks,labels = xlabels))+ labs(x = 'Log Lambda', y = 'Binomial Deviance',color = 'vars')+ geom_vline(xintercept = log(c(cvfit$lambda.min,cvfit$lambda.1se)),lty = 2,col = 'grey50')+ theme_classic()+ theme(axis.title = element_text(size = 15), axis.text = element_text(size = 12), axis.line = element_line(linewidth = .5), legend.position = c(0.7,0.6), legend.background = element_blank())+ guides(colour = guide_legend(ncol = 2, byrow = F)) figure3C1 fit<-lasso dev <- fit[["dev.ratio"]] df <- fit[["df"]] lamda <- fit[["lambda"]]%>%log() aaa2 <- as.matrix(fit[["beta"]]) aaa2 <- aaa2[apply(aaa2,1,sum) != 0,] norm <- apply(abs(aaa2),2,sum) bbb <- melt(aaa2) bbb$lamda <- rep(lamda,each = nrow(aaa2)) bbb$dev <- rep(dev,each = nrow(aaa2)) bbb$df <- rep(df,each = nrow(aaa2)) bbb$norm <- rep(norm,each = nrow(aaa2)) xbreaks <- lamda[seq(1,100,20)] xlabels <- df[seq(1,100,20)] library(RColorBrewer) mypalette <- c(brewer.pal(11,"Set3"),brewer.pal(11,"Spectral"),brewer.pal(8,"Accent")) figure3C2 <- ggplot(data = bbb,aes(x = lamda,y = value,color = Var1))+ geom_smooth(linewidth = 0.7,se = F)+ labs(x = 'Log Lambda', y = 'Coefficients',color = ' ')+ scale_color_ucscgb()+ scale_x_continuous(sec.axis = sec_axis(~.,breaks = xbreaks,labels = xlabels))+ scale_y_continuous(limits = c(-12,15))+ theme_classic()+ theme(legend.background = element_blank() )+ guides(colour = guide_legend(ncol = 2, byrow = F))+my_theme+ scale_color_manual(name=" ",values = mypalette)+ theme(legend.text = ggplot2::element_text(family = "Times New Roman", size = 8)) figure3C2 }
|