This script requires that the working directory includes the folders “data”, “results”, and “manuscript”. We obtained our results using R 4.3.0 (2023-04-21) with cornet 0.0.8 (2023-06-01) on a local machine (aarch64-apple-darwin20, macOS Ventura 13.4). Floating point errors might lead to slightly different results on other platforms.
::pdf("manuscript/figure_idea.pdf",width=5,height=2.5)
grDevices
<- function(x,y,width=0.22,height=0.2,labels="",cex=1,col="black",...){
box <- x + 0.5*c(-1,-1,1,1)*width
xs <- y + 0.5*c(-1,1,1,-1)*height
ys ::polygon(x=xs,y=ys,border=col,lwd=2,...)
graphics::text(x=x,y=y,labels=labels,col=col,cex=cex)
graphics
}
::par(mar=c(0,0,0,0))
graphics::plot.new()
graphics::plot.window(xlim=c(0,1),ylim=c(0,1))
graphics
<- h <- 0.1
v
box(x=0+h,y=0.5,labels="outcomes,\nfeatures")
box(x=0.5,y=1-v,labels="initial binary\nclassification",col="red")
box(x=0.5,y=0+v,labels="numerical\nprediction",col="blue")
box(x=1-h,y=0.5,labels="final binary\nclassification",col="red")
<- 0.02
d ::arrows(x0=0.2+d,y0=0.5+c(-d,d),x1=0.4-d,y1=c(v,1-v),lwd=2,col=c("blue","red"))
graphics::arrows(x0=0.6+d,y0=c(v,1-v),x1=0.8-d,y1=0.5+c(-d,d),lwd=2,col=c("blue","red"))
graphics
::text(x=0.4,y=0.55,labels="binary outcome:\nlogistic regression",col="red",cex=0.7,pos=3)
graphics::text(x=0.4,y=0.45,labels="numerical outcome:\nlinear regression",col="blue",cex=0.7,pos=1)
graphics::text(x=0.63,y=0.5,labels="combine\npredicted\nprobabilities",col="darkgrey",cex=0.7)
graphics::text(x=0.8,y=0.3,labels="transform\npredicted values to\npredicted probabilities",col="darkgrey",cex=0.7,pos=1)
graphics
::dev.off() grDevices
<- list()
loss for(i in seq_len(4)){
<- list()
loss[[i]] cat("mode:",i,"\n")
for(j in seq_len(100)){
set.seed(j)
cat("iteration:",j,"\n")
<- 100; n1 <- 10000; p <- 500
n0 <- n0 + n1
n <- matrix(data=stats::rnorm(n*p),nrow=n,ncol=p)
X <- stats::rbinom(n=p,size=1,prob=0.05)*stats::rnorm(n=p)
beta <- X %*% beta
eta <- stats::rnorm(n=n)
epsilon if(i==1){
<- eta + epsilon
y else if(i==2){
} <- ifelse(eta<0,-2,+2)+epsilon
y table(y>=0,eta>=0)
else if(i==3){
} <- ifelse(eta<0,-sqrt(abs(eta+epsilon)),(eta+epsilon)^2)
y else if(i==4){
} <- eta + epsilon + stats::rbinom(n=n,size=1,prob=0.05)*(2*stats::rbinom(n=n,size=1,prob=0.5)-1)*1.5*max(abs(eta))
y
}<- rep(c(0,1),times=c(n0,n1))
foldid <- cornet::cv.cornet(y=y,cutoff=0,X=X,foldid.ext=foldid)
loss[[i]][[j]]
}
}
save(loss,file="results/simulation.RData")
writeLines(text=capture.output(utils::sessionInfo(),cat("\n"),
::session_info()),con="results/info_sim.txt") sessioninfo
load("results/simulation.RData")
::pdf("manuscript/figure_EXA.pdf",width=5,height=5)
grDevices
::par(mfrow=c(2,2),mar=c(2,2,1,1))
graphics<- c(binomial=1,combined=2,gaussian=3)
pos <- c(binomial="red",combined="grey",gaussian="blue")
col <- 0.7
cex <- c("binomial","combined","gaussian")
names for(i in seq_len(4)){
<- as.data.frame(t(sapply(loss[[i]],function(x) x$deviance)))
frame ::boxplot(x=frame[,names],at=pos[names],col=col[names],cex.axis=cex,main=paste0("example ",i),cex.main=cex,axes=FALSE)
graphics::box()
graphics::axis(side=1,at=pos[names],labels=names,cex.axis=cex,tick=FALSE,line=-1)
graphics::axis(side=2,cex.axis=cex)
graphicsfor(j in c("binomial","combined","gaussian")){
<- mean(frame[[j]])
mean ::points(x=pos[j],y=mean,pch=21,col="white",bg="black")
graphicsif(j=="combined"){next}
<- stats::wilcox.test(x=frame$combined,y=frame[[j]],alternative="less")$p.value
pvalue <- ifelse(pvalue<=0.05/8,"*","")
signif ::text(x=mean(c(pos["combined"],pos[[j]])),y=min(frame),labels=paste0("p=",format(pvalue,digits=2,scientific=TRUE),signif),pos=3,cex=0.7)
graphics
}
}
::dev.off() grDevices
<- 1000
iter set.seed(1)
<- data.frame(cor=runif(n=iter,min=0,max=0.9),
frame n=round(runif(n=iter,min=100,max=200))+10000,
prob=runif(n=iter,min=0.01,max=0.1),
sd=runif(n=iter,min=1,max=2),
exp=runif(n=iter,min=0.1,max=2),
frac=runif(n=iter,min=0.5,max=0.9))
<- lasso <- list()
ridge <- utils::txtProgressBar(min=0,max=nrow(frame),width=20,style=3)
pb for(i in seq_len(nrow(frame))){
::setTxtProgressBar(pb=pb,value=i)
utilsset.seed(i)
<- do.call(what=cornet:::.simulate,args=cbind(frame[i,],p=500))
data <- rep(c(0,1),times=c(frame$n[i],10000))
foldid set.seed(i)
<- do.call(what=cornet:::cv.cornet,args=c(data,alpha=0,foldid=foldid))
ridge[[i]] set.seed(i)
<- do.call(what=cornet:::cv.cornet,args=c(data,alpha=1,foldid=foldid))
lasso[[i]]
}names(lasso) <- names(ridge) <- paste0("set",seq_len(nrow(frame)))
save(lasso,ridge,frame,file="results/simulation.RData")
writeLines(text=capture.output(utils::sessionInfo(),cat("\n"),
::session_info()),con="results/info_sim.txt") sessioninfo
#--- boxplot of different metrics ---
load("results/simulation.RData",verbose=TRUE)
<- fuse1 <- list()
fuse0 for(i in c("deviance","class","mse","mae","auc")){
<- sapply(ridge,function(x) (x[[i]]["combined"]-x[[i]]["binomial"]))
fuse0[[i]] <- sapply(lasso,function(x) (x[[i]]["combined"]-x[[i]]["binomial"]))
fuse1[[i]]
}
::pdf("manuscript/figure_BOX.pdf",width=6,height=4)
grDevices::par(mar=c(1.9,1.9,0.1,0.1))
graphics::plot.new()
graphics<- range(unlist(fuse0),unlist(fuse1))
ylim <- seq(from=1,to=9,by=2)
at ::plot.window(xlim=c(min(at)-0.6,max(at)+0.6),ylim=ylim)
graphics::axis(side=2)
graphics::abline(h=0,col="grey",lty=2)
graphics::abline(v=at+1,col="grey",lty=2)
graphics::box()
graphics::boxplot(fuse1,at=at-0.5,add=TRUE,axes=FALSE,col="white",border="black")
graphics::boxplot(fuse0,at=at+0.5,add=TRUE,axes=FALSE,col="white",border="darkgrey")
graphics<- names(fuse1)
labels <- ifelse(labels=="class","mcr",labels)
labels <- ifelse(labels %in% c("mcr","mse","mae","auc"),toupper(labels),labels)
labels for(i in seq_along(labels)){
::axis(side=1,at=at[i],labels=bquote(Delta ~ .(labels[i])))
graphics
}::dev.off()
grDevices
# decrease
sapply(fuse1,function(x) mean(x<0)) # lasso
sapply(fuse0,function(x) mean(x<0)) # ridge
# constant
sapply(fuse1,function(x) mean(x==0)) # lasso
sapply(fuse0,function(x) mean(x==0)) # ridge
# increase
sapply(fuse1,function(x) mean(x>0)) # lasso
sapply(fuse0,function(x) mean(x>0)) # ridge
#--- plot of percentage changes ---
load("results/simulation.RData",verbose=TRUE)
<- list()
loss $ridge <- as.data.frame(t(sapply(ridge,function(x) x$deviance)))
loss$lasso <- as.data.frame(t(sapply(lasso,function(x) x$deviance)))
loss
<- list()
data for(i in c("ridge","lasso")){
<- data.frame(row.names=rownames(frame))
data[[i]] $"(1)" <- 100*(loss[[i]]$binomial-loss[[i]]$intercept)/loss[[i]]$intercept
data[[i]]$"(2)" <- 100*(loss[[i]]$combined-loss[[i]]$intercept)/loss[[i]]$intercept
data[[i]]$"(3)" <- 100*(loss[[i]]$combined-loss[[i]]$binomial)/loss[[i]]$binomial
data[[i]]
}
<- colnames(data$lasso)
row <- colnames(frame)
col <- expression(rho,n,s,sigma,t,q)
txt
for(k in c("ridge","lasso")){
::pdf(paste0("manuscript/figure_",k,".pdf"),width=6.5,height=4)
grDevices::par(mfrow=c(length(row),length(col)),
graphicsmar=c(0.2,0.2,0.2,0.2),oma=c(4,4,0,0))
for(i in seq_along(row)){
for(j in seq_along(col)){
<- data[[k]][[row[i]]]
y <- frame[[col[j]]]
x ::plot.new()
graphics::plot.window(xlim=range(x),ylim=range(y),xaxs="i")
graphics::box()
graphics::abline(h=0,lty=1,col="grey")
graphics::points(y=y,x=x,cex=0.5,pch=16,col=ifelse(y>0,"black","grey"))
graphics<- stats::loess.smooth(y=y,x=x,evaluation=200)
line ::lines(x=line$x,y=line$y,col="black",lty=2,lwd=1)
graphicsif(j==1){
::mtext(text=row[i],side=2,line=2.5,las=2)
graphics::axis(side=2)
graphics
}if(i==length(row)){
::mtext(text=txt[j],side=1,line=2.5)
graphics::axis(side=1)
graphics
}
}
}::dev.off()
grDevices
}
cbind(col,as.character(txt)) # verify