#The function requires a vector containing the phenotype and a matrix of #marker genotypes; the user has the option to specify the number of steps #the Lasso algorithm should take (the max.steps option from the lars #function, default is 10) and the number of permutations (default is #1000). The lars package must be loaded. library(lars) QTLlasso<-function(markers,phenotype,steps=10,nperms=1000){ notmissing<-setdiff(1:length(phenotype),unique(c(which(is.na(phenotype)),which(is.na(markers),arr.ind=T)[,1]))) pvalue<-rep(1,ncol(markers)) phenotype<-phenotype[notmissing] markers<-markers[notmissing,] phenotype<-scale(phenotype,center=T,scale=T) markers<-scale(markers,center=T,scale=T) lasso<-try(lars(markers,phenotype,max.steps=steps,use.Gram=F)) if (length(lasso)!=1) { threshold<-apply(abs(t(markers)%*%(as.vector(phenotype)-markers%*%t(lasso$beta))),2,max) permutations<-matrix(phenotype,nrow(phenotype),nperms) permutations<-apply(permutations,2,function(x){sample(x,nrow(phenotype),replace=F)}) null<-abs(t(permutations)%*%markers) null<-as.vector(apply(null,1,max)) for (i in 1:length(lasso$action)){if (lasso$action[[i]][1]>0){if(pvalue[lasso$action[[i]][1]]==1) {pvalue[lasso$action[[i]][1]]<-sum(null>threshold[i])/nperms}}}} else {stop("Lars function hasn't returned suitable object, try reducing the number of steps")} return(pvalue) }