lasso.dr1<-function(x,y,kfold=5,rep=10,plot=T,grid=seq(from=0,to=1,length=100)){ # wrapper for lasso in lars, with modifications to cv.lars require(lars) # set.seed(seed) ok<-complete.cases(x,y) x<-x[ok,] # get rid of na's y<-y[ok] # since regsubsets can't handle na's m<-ncol(x) n<-nrow(x) as.matrix(x)->x cvout<-cv.lars1(x,y,K=kfold,plot.it=F,fraction=grid) sumcv<-cvout$cv frac<-cvout$fraction if(rep >1){ for(i in 1:(rep-1)){ sumcv<-sumcv+cv.lars1(x,y,K=kfold,plot.it=F,fraction=grid)$cv } # ends for } # ends if sumcv<-sumcv/rep if(plot) plot(frac,sumcv) sfrac<-frac[which.min(sumcv)] # assume frac stays the same object<-lars(x,y,type="lasso") fit <- predict.lars(object,x,s=sfrac,type="fit",mode="fraction")$fit coeff<-predict.lars(object,x,s=sfrac,type="coef",mode="fraction")$coefficients st<-sum(coeff !=0) # number nonzero mse<-sum((y-fit)^2)/(n-st-1) colnames(x)<-colnames(x,do.NULL=F,prefix="") # corrects for no colnames x<-x[,colnames(x)[which(coeff !=0)]] cat("",fill=T) cat("K (num. of folds)=",kfold,fill=T) cat("Num. of reps)=",rep,fill=T) cat("Fraction from CV=",sfrac,fill=T) cat("number nonzero=",st,fill=T) cat("mse=",mse,fill=T) cat("Variables Selected =",colnames(x),fill=T) return(list(l.object=object,frac=frac,fit=fit,coeff=coeff,st=st,mse=mse,sumcv=sumcv)) }