getzze / RobustModels.jl

A Julia package for robust regressions using M-estimators and quantile regressions
MIT License
36 stars 1 forks source link

StackOverflowError if inputs allow Missing type #16

Closed svilupp closed 1 year ago

svilupp commented 1 year ago

First of all, thank you for the great package!

I think we have found a bug with @cbhower in handling inputs that allow missing (no actual missing values were present).

Expected behaviour: If I provided a matrix with a Missing eltype, I should get a MethodError that it's not supported.

Actual behaviour: User a gets StackOverflowError, which is hard to debug. See below

How to reproduce: See the MWE below. We can reproduce it on Julia 1.8.5 both ARM-based and x86-based.

mwe.jl

using RobustModels
using Random
using Missings

N = 10
# if you remove allowmissing, it will work
X = randn(N, 4) |> allowmissing
y = ones(N)

quantreg(X, y; quantile=0.5)

Returns:

LoadError: StackOverflowError: Stacktrace: [1] fit(::Type{QuantileRegression}, X::Matrix{Union{Missing, Float64}}, y::Vector{Float64}; kwargs::Base.Pairs{Symbol, Float64, Tuple{Symbol}, NamedTuple{(:quantile,), Tuple{Float64}}}) (repeats 37022 times) @ RobustModels ~/.julia/packages/RobustModels/FVHvF/src/quantileregression.jl:140 [2] quantreg(::Matrix{Union{Missing, Float64}}, ::Vector{Float64}; kwargs::Base.Pairs{Symbol, Float64, Tuple{Symbol}, NamedTuple{(:quantile,), Tuple{Float64}}}) @ RobustModels ~/.julia/packages/RobustModels/FVHvF/src/quantileregression.jl:76

For comparison, with GLM.jl:

using GLM
using Random
using Missings

N = 10
X = randn(N, 4) |> allowmissing
y = ones(N)

lm(X, y)

Returns:

ERROR: MethodError: no method matching fit(::Type{LinearModel}, ::Matrix{Union{Missing, Float64}}, ::Vector{Float64}, ::Nothing) Closest candidates are: fit(::Type{LinearModel}, ::AbstractMatrix{<:Real}, ::AbstractVector{<:Real}, ::Union{Nothing, Bool}; wts, dropcollinear) at ~/.julia/packages/GLM/4A2DM/src/lm.jl:134

Versioninfo():

RobustModels v0.4.5 (fresh install)

Julia Version 1.8.5 Commit 17cfb8e65ea (2023-01-08 06:45 UTC) Platform Info: OS: macOS (arm64-apple-darwin21.5.0) CPU: 8 × Apple M1 Pro WORD_SIZE: 64 LIBM: libopenlibm LLVM: libLLVM-13.0.1 (ORCJIT, apple-m1) Threads: 6 on 6 virtual cores Environment: JULIA_EDITOR = code JULIA_NUM_THREADS = 8

svilupp commented 1 year ago

I had a look at the code base and I think StackOverflow is a result of recursion because of this line: https://github.com/getzze/RobustModels.jl/blob/5fd6ffd703e5a228d7efdef211b8cdfc2687f292/src/quantileregression.jl#L140