From df0db73e3dce6f90df2a95e5a83559e647cb62da Mon Sep 17 00:00:00 2001 From: lynnyi Date: Wed, 8 Nov 2017 17:04:29 -0800 Subject: [PATCH 1/2] extend glmnet to univariate cases (one variable) --- R/glmnet.R | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/R/glmnet.R b/R/glmnet.R index 3c609ba..5207dd4 100644 --- a/R/glmnet.R +++ b/R/glmnet.R @@ -17,8 +17,14 @@ glmnet=function(x,y,family=c("gaussian","binomial","poisson","multinomial","cox" y=drop(y) # we dont like matrix responses unless we need them np=dim(x) ###check dims - if(is.null(np)|(np[2]<=1))stop("x should be a matrix with 2 or more columns") - nobs=as.integer(np[1]) + if(is.null(np)){ + nobs = length(x) + nvars = as.integer(1) + } + else { + nobs = as.integer(np[1]) + nvars = as.integer(np[2]) + } if(missing(weights))weights=rep(1,nobs) 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="")) nvars=as.integer(np[2]) @@ -28,16 +34,10 @@ glmnet=function(x,y,family=c("gaussian","binomial","poisson","multinomial","cox" vnames=colnames(x) if(is.null(vnames))vnames=paste("V",seq(nvars),sep="") ne=as.integer(dfmax) - nx=as.integer(pmax) - if(missing(exclude))exclude=integer(0) - if(any(penalty.factor==Inf)){ - exclude=c(exclude,seq(nvars)[penalty.factor==Inf]) - exclude=sort(unique(exclude)) - } - if(length(exclude)>0){ + nx=as.integer(pmax) + if(!missing(exclude)){ jd=match(exclude,seq(nvars),0) if(!all(jd>0))stop("Some excluded variables out of range") - penalty.factor[jd]=1 #ow can change lambda sequence jd=as.integer(c(length(jd),jd)) }else jd=as.integer(0) vp=as.double(penalty.factor) @@ -67,7 +67,7 @@ glmnet=function(x,y,family=c("gaussian","binomial","poisson","multinomial","cox" } storage.mode(cl)="double" ### end check on limits - + isd=as.integer(standardize) intr=as.integer(intercept) if(!missing(intercept)&&family=="cox")warning("Cox model has no intercept") @@ -79,7 +79,7 @@ glmnet=function(x,y,family=c("gaussian","binomial","poisson","multinomial","cox" ulam=double(1) } else{ - flmin=as.double(1) + flmin=as.double(1) if(any(lambda<0))stop("lambdas should be non-negative") ulam=as.double(rev(sort(lambda))) nlam=as.integer(length(lambda)) From 3169f1b6785d671d01887e1a930763fa7acb4907 Mon Sep 17 00:00:00 2001 From: lynnyi Date: Wed, 8 Nov 2017 17:17:49 -0800 Subject: [PATCH 2/2] Univariate Glmnet --- R/glmnet.R | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/R/glmnet.R b/R/glmnet.R index 5207dd4..a9a83b7 100644 --- a/R/glmnet.R +++ b/R/glmnet.R @@ -18,7 +18,7 @@ glmnet=function(x,y,family=c("gaussian","binomial","poisson","multinomial","cox" np=dim(x) ###check dims if(is.null(np)){ - nobs = length(x) + nobs = as.integer(length(x)) nvars = as.integer(1) } else { @@ -27,7 +27,6 @@ glmnet=function(x,y,family=c("gaussian","binomial","poisson","multinomial","cox" } if(missing(weights))weights=rep(1,nobs) 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="")) - nvars=as.integer(np[2]) dimy=dim(y) nrowy=ifelse(is.null(dimy),length(y),dimy[1]) if(nrowy!=nobs)stop(paste("number of observations in y (",nrowy,") not equal to the number of rows of x (",nobs,")",sep=""))