@@ -283,6 +283,37 @@ function glmnet!(X::Matrix{Float64}, y::Vector{Float64},
283
283
284
284
@check_and_return
285
285
end
286
+ function glmnet! (X:: SparseMatrixCSC{Float64,Int32} , y:: Vector{Float64} ,
287
+ family:: Normal = Normal ();
288
+ weights:: Vector{Float64} = ones (length (y)),
289
+ naivealgorithm:: Bool = (size (X, 2 ) >= 500 ), alpha:: Real = 1.0 ,
290
+ penalty_factor:: Vector{Float64} = ones (size (X, 2 )),
291
+ constraints:: Array{Float64, 2} = [x for x in (- Inf , Inf ), y in 1 : size (X, 2 )],
292
+ dfmax:: Int = size (X, 2 ), pmax:: Int = min (dfmax* 2 + 20 , size (X, 2 )), nlambda:: Int = 100 ,
293
+ lambda_min_ratio:: Real = (length (y) < size (X, 2 ) ? 1e-2 : 1e-4 ),
294
+ lambda:: Vector{Float64} = Float64[], tol:: Real = 1e-7 , standardize:: Bool = true ,
295
+ intercept:: Bool = true , maxit:: Int = 1000000 )
296
+ @validate_and_init
297
+
298
+ ccall ((:spelnet_ , libglmnet), Void,
299
+ (Ref{Int32}, Ref{Float64}, Ref{Int32}, Ref{Int32}, Ptr{Float64}, Ptr{Int32},
300
+ Ptr{Int32}, Ptr{Float64}, Ptr{Float64}, Ref{Int32}, Ptr{Float64}, Ptr{Float64},
301
+ Ref{Int32}, Ref{Int32}, Ref{Int32}, Ref{Float64}, Ptr{Float64}, Ref{Float64},
302
+ Ref{Int32}, Ref{Int32}, Ref{Int32}, Ptr{Int32}, Ptr{Float64}, Ptr{Float64},
303
+ Ptr{Int32}, Ptr{Int32}, Ptr{Float64}, Ptr{Float64}, Ptr{Int32}, Ptr{Int32}),
304
+ (naivealgorithm ? 2 : 1 ), alpha, size (X, 1 ), size (X, 2 ), X. nzval, X. colptr,
305
+ X. rowval, y, weights, 0 , penalty_factor, constraints, dfmax, pmax, nlambda,
306
+ lambda_min_ratio, lambda, tol, standardize, intercept, maxit, lmu, a0, ca, ia,
307
+ nin, fdev, alm, nlp, jerr)
308
+
309
+ null_dev = 0.0
310
+ mu = mean (y)
311
+ for i = 1 : length (y)
312
+ null_dev += abs2 (null_dev- mu)
313
+ end
314
+
315
+ @check_and_return
316
+ end
286
317
287
318
function glmnet! (X:: Matrix{Float64} , y:: Matrix{Float64} ,
288
319
family:: Binomial ;
@@ -330,6 +361,54 @@ function glmnet!(X::Matrix{Float64}, y::Matrix{Float64},
330
361
null_dev = null_dev[1 ]
331
362
@check_and_return
332
363
end
364
+ function glmnet! (X:: SparseMatrixCSC{Float64,Int32} , y:: Matrix{Float64} ,
365
+ family:: Binomial ;
366
+ offsets:: Union{Vector{Float64},Void} = nothing ,
367
+ weights:: Vector{Float64} = ones (size (y, 1 )),
368
+ alpha:: Real = 1.0 ,
369
+ penalty_factor:: Vector{Float64} = ones (size (X, 2 )),
370
+ constraints:: Array{Float64, 2} = [x for x in (- Inf , Inf ), y in 1 : size (X, 2 )],
371
+ dfmax:: Int = size (X, 2 ), pmax:: Int = min (dfmax* 2 + 20 , size (X, 2 )), nlambda:: Int = 100 ,
372
+ lambda_min_ratio:: Real = (length (y) < size (X, 2 ) ? 1e-2 : 1e-4 ),
373
+ lambda:: Vector{Float64} = Float64[], tol:: Real = 1e-7 , standardize:: Bool = true ,
374
+ intercept:: Bool = true , maxit:: Int = 1000000 , algorithm:: Symbol = :newtonraphson )
375
+ @validate_and_init
376
+ size (y, 2 ) == 2 || error (" glmnet for logistic models requires a two-column matrix with " *
377
+ " counts of negative responses in the first column and positive " *
378
+ " responses in the second" )
379
+ kopt = algorithm == :newtonraphson ? 0 :
380
+ algorithm == :modifiednewtonraphson ? 1 :
381
+ algorithm == :nzsame ? 2 : error (" unknown algorithm " )
382
+ offsets:: Vector{Float64} = isa (offsets, @compat Void) ? zeros (size (y, 1 )) : copy (offsets)
383
+ length (offsets) == size (y, 1 ) || error (" length of offsets must match length of y" )
384
+
385
+ null_dev = Vector {Float64} (1 )
386
+
387
+ # The Fortran code expects positive responses in first column, but
388
+ # this convention is evidently unacceptable to the authors of the R
389
+ # code, and, apparently, to us
390
+ for i = 1 : size (y, 1 )
391
+ a = y[i, 1 ]
392
+ b = y[i, 2 ]
393
+ y[i, 1 ] = b* weights[i]
394
+ y[i, 2 ] = a* weights[i]
395
+ end
396
+
397
+ ccall ((:splognet_ , libglmnet), Void,
398
+ (Ref{Float64}, Ref{Int32}, Ref{Int32}, Ref{Int32}, Ptr{Float64}, Ptr{Int32},
399
+ Ptr{Int32}, Ptr{Float64}, Ptr{Float64}, Ref{Int32}, Ptr{Float64}, Ptr{Float64},
400
+ Ref{Int32}, Ref{Int32}, Ref{Int32}, Ref{Float64}, Ptr{Float64}, Ref{Float64},
401
+ Ref{Int32}, Ref{Int32}, Ref{Int32}, Ref{Int32}, Ptr{Int32}, Ptr{Float64},
402
+ Ptr{Float64}, Ptr{Int32}, Ptr{Int32}, Ptr{Float64}, Ptr{Float64}, Ptr{Float64},
403
+ Ptr{Int32}, Ptr{Int32}),
404
+ alpha, size (X, 1 ), size (X, 2 ), 1 , X. nzval, X. colptr, X. rowval, y, copy (offsets),
405
+ 0 , penalty_factor, constraints, dfmax, pmax, nlambda, lambda_min_ratio, lambda,
406
+ tol, standardize, intercept, maxit, kopt, lmu, a0, ca, ia, nin, null_dev, fdev,
407
+ alm, nlp, jerr)
408
+
409
+ null_dev = null_dev[1 ]
410
+ @check_and_return
411
+ end
333
412
334
413
function glmnet! (X:: Matrix{Float64} , y:: Vector{Float64} ,
335
414
family:: Poisson ;
@@ -361,13 +440,49 @@ function glmnet!(X::Matrix{Float64}, y::Vector{Float64},
361
440
null_dev = null_dev[1 ]
362
441
@check_and_return
363
442
end
443
+ function glmnet! (X:: SparseMatrixCSC{Float64,Int32} , y:: Vector{Float64} ,
444
+ family:: Poisson ;
445
+ offsets:: Union{Vector{Float64},Void} = nothing ,
446
+ weights:: Vector{Float64} = ones (length (y)),
447
+ alpha:: Real = 1.0 ,
448
+ penalty_factor:: Vector{Float64} = ones (size (X, 2 )),
449
+ constraints:: Array{Float64, 2} = [x for x in (- Inf , Inf ), y in 1 : size (X, 2 )],
450
+ dfmax:: Int = size (X, 2 ), pmax:: Int = min (dfmax* 2 + 20 , size (X, 2 )), nlambda:: Int = 100 ,
451
+ lambda_min_ratio:: Real = (length (y) < size (X, 2 ) ? 1e-2 : 1e-4 ),
452
+ lambda:: Vector{Float64} = Float64[], tol:: Real = 1e-7 , standardize:: Bool = true ,
453
+ intercept:: Bool = true , maxit:: Int = 1000000 )
454
+ @validate_and_init
455
+ null_dev = Vector {Float64} (1 )
456
+
457
+ offsets:: Vector{Float64} = isa (offsets, Void) ? zeros (length (y)) : copy (offsets)
458
+ length (offsets) == length (y) || error (" length of offsets must match length of y" )
459
+
460
+ ccall ((:spfishnet_ , libglmnet), Void,
461
+ (Ref{Float64}, Ref{Int32}, Ref{Int32}, Ptr{Float64}, Ptr{Int32}, Ptr{Int32},
462
+ Ptr{Float64}, Ptr{Float64}, Ptr{Float64}, Ref{Int32}, Ptr{Float64}, Ptr{Float64},
463
+ Ref{Int32}, Ref{Int32}, Ref{Int32}, Ref{Float64}, Ptr{Float64}, Ref{Float64},
464
+ Ref{Int32}, Ref{Int32}, Ref{Int32}, Ptr{Int32}, Ptr{Float64}, Ptr{Float64},
465
+ Ptr{Int32}, Ptr{Int32}, Ptr{Float64}, Ptr{Float64}, Ptr{Float64}, Ptr{Int32},
466
+ Ptr{Int32}),
467
+ alpha, size (X, 1 ), size (X, 2 ), X. nzval, X. colptr, X. rowval, y, offsets, weights,
468
+ 0 , penalty_factor, constraints, dfmax, pmax, nlambda, lambda_min_ratio, lambda,
469
+ tol, standardize, intercept, maxit, lmu, a0, ca, ia, nin, null_dev, fdev, alm,
470
+ nlp, jerr)
471
+
472
+ null_dev = null_dev[1 ]
473
+ @check_and_return
474
+ end
364
475
365
476
glmnet (X:: Matrix{Float64} , y:: Vector{Float64} , family:: Distribution = Normal (); kw... ) =
366
477
glmnet! (copy (X), copy (y), family; kw... )
367
478
glmnet (X:: AbstractMatrix , y:: AbstractVector , family:: Distribution = Normal (); kw... ) =
368
479
glmnet (convert (Matrix{Float64}, X), convert (Vector{Float64}, y), family; kw... )
480
+ glmnet (X:: SparseMatrixCSC , y:: AbstractVector , family:: Distribution = Normal (); kw... ) =
481
+ glmnet! (convert (SparseMatrixCSC{Float64,Int32}, X), convert (Vector{Float64}, y), family; kw... )
369
482
glmnet (X:: Matrix{Float64} , y:: Matrix{Float64} , family:: Binomial ; kw... ) =
370
483
glmnet! (copy (X), copy (y), family; kw... )
484
+ glmnet (X:: SparseMatrixCSC , y:: AbstractMatrix , family:: Binomial ; kw... ) =
485
+ glmnet! (convert (SparseMatrixCSC{Float64,Int32}, X), convert (Matrix{Float64}, y), family; kw... )
371
486
glmnet (X:: Matrix , y:: Matrix , family:: Binomial ; kw... ) =
372
487
glmnet (convert (Matrix{Float64}, X), convert (Matrix{Float64}, y), family; kw... )
373
488
0 commit comments