1glmnet=function(x,y,family=c("gaussian","binomial","poisson","multinomial","cox","mgaussian"),weights,offset=NULL,alpha=1.0,nlambda=100,lambda.min.ratio=ifelse(nobs<nvars,1e-2,1e-4),lambda=NULL,standardize=TRUE,thresh=1e-7,dfmax=nvars+1,pmax=min(dfmax*2+20,nvars),exclude,penalty.factor=rep(1,nvars),maxit=100000,type.gaussian=ifelse(nvars<500,"covariance","naive"),standardize.response=FALSE,type.multinomial=c("ungrouped","grouped")){ 2 3### Prepare all the generic arguments, then hand off to family functions 4 family=match.arg(family) 5 if(alpha>1){ 6 warning("alpha >1; set to 1") 7 alpha=1 8 } 9 if(alpha<0){ 10 warning("alpha<0; set to 0") 11 alpha=0 12 } 13 alpha=as.double(alpha) 14 15 this.call=match.call() 16 nlam=as.integer(nlambda) 17 y=drop(y) # we dont like matrix responses unless we need them 18 np=dim(x) 19 ###check dims 20 nobs=as.integer(np[1]) 21 if(missing(weights))weights=rep(1,nobs) 22 else if(length(weights)!=nobs)stop(paste("number of elements in weights (",length(weights),") not equal to the number of rows of x (",nobs,")",sep="")) 23 nvars=as.integer(np[2]) 24 dimy=dim(y) 25 nrowy=ifelse(is.null(dimy),length(y),dimy[1]) 26 if(nrowy!=nobs)stop(paste("number of observations in y (",nrowy,") not equal to the number of rows of x (",nobs,")",sep="")) 27 vnames=colnames(x) 28 if(is.null(vnames))vnames=paste("V",seq(nvars),sep="") 29 ne=as.integer(dfmax) 30 nx=as.integer(pmax) 31 if(!missing(exclude)){ 32 jd=match(exclude,seq(nvars),0) 33 if(!all(jd>0))stop("Some excluded variables out of range") 34 jd=as.integer(c(length(jd),jd)) 35 }else jd=as.integer(0) 36 vp=as.double(penalty.factor) 37 isd=as.integer(standardize) 38 jsd=as.integer(standardize.response) 39 thresh=as.double(thresh) 40 if(is.null(lambda)){ 41 if(lambda.min.ratio>=1)stop("lambda.min.ratio should be less than 1") 42 flmin=as.double(lambda.min.ratio) 43 ulam=double(1) 44 } 45 else{ 46 flmin=as.double(1) 47 if(any(lambda<0))stop("lambdas should be non-negative") 48 ulam=as.double(rev(sort(lambda))) 49 nlam=as.integer(length(lambda)) 50 } 51 is.sparse=FALSE 52 ix=jx=NULL 53 if(inherits(x,"sparseMatrix")){##Sparse case 54 is.sparse=TRUE 55 x=as(x,"CsparseMatrix") 56 x=as(x,"dgCMatrix") 57 ix=as.integer(x@p+1) 58 jx=as.integer(x@i+1) 59 x=as.double(x@x) 60 } 61kopt=as.integer(0) #This means to use the exact Hessian, rather than the upper bound 62 if(family=="multinomial"){ 63 type.multinomial=match.arg(type.multinomial) 64 if(type.multinomial=="grouped")kopt=as.integer(2) 65 } 66 67 fit=switch(family, 68 "gaussian"=elnet(x,is.sparse,ix,jx,y,weights,offset,type.gaussian,alpha,nobs,nvars,jd,vp,ne,nx,nlam,flmin,ulam,thresh,isd,vnames,maxit), 69 "poisson"=fishnet(x,is.sparse,ix,jx,y,weights,offset,alpha,nobs,nvars,jd,vp,ne,nx,nlam,flmin,ulam,thresh,isd,vnames,maxit), 70 "binomial"=lognet(x,is.sparse,ix,jx,y,weights,offset,alpha,nobs,nvars,jd,vp,ne,nx,nlam,flmin,ulam,thresh,isd,vnames,maxit,kopt,family), 71 "multinomial"=lognet(x,is.sparse,ix,jx,y,weights,offset,alpha,nobs,nvars,jd,vp,ne,nx,nlam,flmin,ulam,thresh,isd,vnames,maxit,kopt,family), 72 "cox"=coxnet(x,is.sparse,ix,jx,y,weights,offset,alpha,nobs,nvars,jd,vp,ne,nx,nlam,flmin,ulam,thresh,isd,vnames,maxit), 73 "mgaussian"=mrelnet(x,is.sparse,ix,jx,y,weights,offset,alpha,nobs,nvars,jd,vp,ne,nx,nlam,flmin,ulam,thresh,isd,jsd,vnames,maxit) 74 ) 75 76 if(is.null(lambda))fit$lambda=fix.lam(fit$lambda)##first lambda is infinity; changed to entry point 77fit$call=this.call 78 fit$nobs=nobs 79 class(fit)=c(class(fit),"glmnet") 80 fit 81} 82