Skip to content

Commit

Permalink
enforce type consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
getzze committed Jul 24, 2023
1 parent 4d02de0 commit fe24993
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
20 changes: 4 additions & 16 deletions src/robustlinearmodel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,8 @@ function StatsAPI.fit(
end

# Make sure X and y have the same float eltype
T = promote_type(float(eltype(X)), float(eltype(y)))
if !(T <: AbstractFloat)
msg = "promoting X and y arrays to float types"
throw(TypeError(:fit, msg, Type{<:AbstractFloat}, T))
end
MT = AbstractMatrix{T}
VT = AbstractVector{T}
return fit(M, convert.(T, X)::MT, convert.(T, y)::VT, args...; kwargs...)
pX, py = promote_to_same_float(X, y)
return fit(M, pX, py, args...; kwargs...)
end

## Convert from formula-data to modelmatrix-response calling form
Expand All @@ -125,14 +119,8 @@ function StatsAPI.fit(
# Extract arrays from data using formula
f, y, X, extra = modelframe(f, data, contrasts, dropmissing, M; wts=wts)
# Call the `fit` method with arrays
T = promote_type(float(eltype(X)), float(eltype(y)))
if !(T <: AbstractFloat)
msg = "promoting X and y arrays to float types"
throw(TypeError(:fit, msg, Type{<:AbstractFloat}, T))
end
MT = AbstractMatrix{T}
VT = AbstractVector{T}
return fit(M, X::MT, y::VT, args...; wts=extra.wts, contrasts=contrasts, __formula=f, kwargs...)
pX, py = promote_to_same_float(X, y)
return fit(M, pX, py, args...; wts=extra.wts, contrasts=contrasts, __formula=f, kwargs...)
end


Expand Down
14 changes: 14 additions & 0 deletions src/tools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@
## Missing values
################################################

function promote_to_same_float(
X::AbstractMatrix,
y::AbstractVector,
)
T = promote_type(float(eltype(X)), float(eltype(y)))
if !(T <: AbstractFloat)
msg = "promoting X and y arrays to float types"
throw(TypeError(:fit, msg, Type{<:AbstractFloat}, T))

Check warning on line 14 in src/tools.jl

View check run for this annotation

Codecov / codecov/patch

src/tools.jl#L13-L14

Added lines #L13 - L14 were not covered by tests
end
MT = AbstractMatrix{T}
VT = AbstractVector{T}
return convert.(T, X)::MT, convert.(T, y)::VT
end

_missing_omit(x::AbstractArray{T}) where {T} = copyto!(similar(x, nonmissingtype(T)), x)

function StatsModels.missing_omit(X::AbstractMatrix, y::AbstractVector)
Expand Down

0 comments on commit fe24993

Please sign in to comment.