Skip to content

Commit 8eb73de

Browse files
authored
Fix determination of nobs in lambda_min_ratio (#73)
1 parent 4167f0c commit 8eb73de

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

src/CoxNet.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function glmnet!(X::Matrix{Float64}, y::Matrix{Float64}, family::CoxPH;
9191
constraints::Array{Float64, 2}=[x for x in (-Inf, Inf), y in 1:size(X, 2)],
9292
dfmax::Int=size(X, 2)+1, pmax::Int=min(dfmax*2+20, size(X, 2)),
9393
nlambda::Int=100,
94-
lambda_min_ratio::Real=(length(y) < size(X, 2) ? 1e-2 : 1e-4),
94+
lambda_min_ratio::Real=(size(y, 1) < size(X, 2) ? 1e-2 : 1e-4),
9595
lambda::Vector{Float64}=Float64[], tol::Real=1e-7, standardize::Bool=true,
9696
intercept::Bool=true, maxit::Int=1000000)
9797
@validate_and_init

src/GLMNet.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ function glmnet!(X::Matrix{Float64}, y::Matrix{Float64},
342342
penalty_factor::Vector{Float64}=ones(size(X, 2)),
343343
constraints::Array{Float64, 2}=[x for x in (-Inf, Inf), y in 1:size(X, 2)],
344344
dfmax::Int=size(X, 2), pmax::Int=min(dfmax*2+20, size(X, 2)), nlambda::Int=100,
345-
lambda_min_ratio::Real=(length(y) < size(X, 2) ? 1e-2 : 1e-4),
345+
lambda_min_ratio::Real=(size(y, 1) < size(X, 2) ? 1e-2 : 1e-4),
346346
lambda::Vector{Float64}=Float64[], tol::Real=1e-7, standardize::Bool=true,
347347
intercept::Bool=true, maxit::Int=1000000, algorithm::Symbol=:newtonraphson)
348348
@validate_and_init
@@ -385,7 +385,7 @@ function glmnet!(X::SparseMatrixCSC{Float64,Int32}, y::Matrix{Float64},
385385
penalty_factor::Vector{Float64}=ones(size(X, 2)),
386386
constraints::Array{Float64, 2}=[x for x in (-Inf, Inf), y in 1:size(X, 2)],
387387
dfmax::Int=size(X, 2), pmax::Int=min(dfmax*2+20, size(X, 2)), nlambda::Int=100,
388-
lambda_min_ratio::Real=(length(y) < size(X, 2) ? 1e-2 : 1e-4),
388+
lambda_min_ratio::Real=(size(y, 1) < size(X, 2) ? 1e-2 : 1e-4),
389389
lambda::Vector{Float64}=Float64[], tol::Real=1e-7, standardize::Bool=true,
390390
intercept::Bool=true, maxit::Int=1000000, algorithm::Symbol=:newtonraphson)
391391
@validate_and_init

src/Multinomial.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ function glmnet!(X::Matrix{Float64}, y::Matrix{Float64},
144144
penalty_factor::Vector{Float64}=ones(size(X, 2)),
145145
constraints::Array{Float64, 2}=[a for a in (-Inf, Inf), y in 1:size(X, 2)],
146146
dfmax::Int=size(X, 2)+1, pmax::Int=min(dfmax*2+20, size(X, 2)), nlambda::Int=100,
147-
lambda_min_ratio::Real=(length(y) < size(X, 2) ? 1e-2 : 1e-4),
147+
lambda_min_ratio::Real=(size(y, 1) < size(X, 2) ? 1e-2 : 1e-4),
148148
lambda::Vector{Float64}=Float64[], tol::Real=1e-7, standardize::Bool=true,
149149
intercept::Bool=true, maxit::Int=1000000, grouped_multinomial::Bool=false,
150150
algorithm::Symbol=:newtonraphson)

test/runtests.jl

+38-1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@ cv4 = glmnetcv(X, y; rng=MersenneTwister(1))
189189
# Test previously ambiguous definitions
190190
glmnet(float.(X), y)
191191
glmnetcv(float.(X), y)
192+
193+
# Check for issue #72
194+
nobs = size(X, 2) - 1
195+
glmnetcv(X[1:nobs, :], y[1:nobs], nfolds=2)
196+
nobs = size(X, 2) + 1
197+
glmnetcv(X[1:nobs, :], y[1:nobs], nfolds=2)
192198
end
193199

194200

@@ -355,6 +361,12 @@ cv = glmnetcv(X, yl, Binomial(); folds=[1,1,1,1,2,2,2,3,3,3])
355361
show(IOBuffer(), cv)
356362
show(IOBuffer(), cv.path)
357363

364+
# Check for issue #72
365+
nobs = size(X, 2) - 1
366+
glmnetcv(X[1:nobs, :], yl[1:nobs, :], Binomial(), nfolds=2)
367+
nobs = size(X, 2) + 1
368+
glmnetcv(X[1:nobs, :], yl[1:nobs, :], Binomial(), nfolds=2)
369+
358370
# Passing RNG makes cv deterministic
359371
cv1 = glmnetcv(X, yl, Binomial())
360372
cv2 = glmnetcv(X, yl, Binomial())
@@ -532,6 +544,12 @@ cv = glmnetcv(X, y, Poisson(); folds=[1,1,1,1,2,2,2,3,3,3])
532544
show(IOBuffer(), cv)
533545
show(IOBuffer(), cv.path)
534546

547+
# Check for issue #72
548+
nobs = size(X, 2) - 1
549+
glmnetcv(X[1:nobs, :], y[1:nobs], Poisson(), nfolds=2)
550+
nobs = size(X, 2) + 1
551+
glmnetcv(X[1:nobs, :], y[1:nobs], Poisson(), nfolds=2)
552+
535553
# Passing RNG makes cv deterministic
536554
cv1 = glmnetcv(X, y, Poisson())
537555
cv2 = glmnetcv(X, y, Poisson())
@@ -602,6 +620,12 @@ coxcv = glmnetcv(dat[:,3:size(dat,2)], dat[:,1], dat[:,2], lambda = cox_lambda,
602620
show(IOBuffer(), coxcv)
603621
show(IOBuffer(), coxcv.path)
604622

623+
# Check for issue #72
624+
X = dat[:,3:size(dat,2)]
625+
nobs = size(X, 2) - 1
626+
glmnetcv(X[1:nobs, :], dat[1:nobs,1], dat[1:nobs,2], nfolds=2)
627+
nobs = size(X, 2) + 1
628+
glmnetcv(X[1:nobs, :], dat[1:nobs,1], dat[1:nobs,2], nfolds=2)
605629

606630
# Passing RNG makes cv deterministic
607631
cv1 = glmnetcv(dat[:,3:size(dat,2)], dat[:,1], dat[:,2])
@@ -670,6 +694,13 @@ iris_cv2 = glmnetcv(iris_x, iris_yy, Multinomial(), folds = multi_folds)
670694
# Make sure show works
671695
show(IOBuffer(), iris_cv)
672696
show(IOBuffer(), iris_cv.path)
697+
698+
# Check for issue #72
699+
X = [iris_x iris_x]
700+
nobs = size(X, 2) - 1
701+
glmnetcv(X[1:nobs, :], iris_yy[1:nobs, :], Multinomial(), nfolds=2)
702+
nobs = size(X, 2) + 1
703+
glmnetcv(X[1:nobs, :], iris_yy[1:nobs, :], Multinomial(), nfolds=2)
673704
end
674705

675706
@testset "MvNormal" begin
@@ -725,4 +756,10 @@ end
725756
@test path.lambda R_lambda
726757
@test path.a0 R_a0
727758

728-
end
759+
# Check for issue #72
760+
nobs = size(X, 2) - 1
761+
glmnetcv(X[1:nobs, :], Y[1:nobs, :], MvNormal())
762+
nobs = size(X, 2) + 1
763+
glmnetcv(X[1:nobs, :], Y[1:nobs, :], MvNormal())
764+
765+
end

0 commit comments

Comments
 (0)