Skip to content

Fix handling of string columns in ModelMatrix #1085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nalimilan opened this issue Oct 1, 2016 · 1 comment
Closed

Fix handling of string columns in ModelMatrix #1085

nalimilan opened this issue Oct 1, 2016 · 1 comment
Milestone

Comments

@nalimilan
Copy link
Member

With latest master, string columns trigger an error when used in a formula to build a ModelMatrix. We should probably treat them as categorical variables, either by converting them to CategoricalArray, or (even better) by building contrasts for them on the fly (without a copy).

Though I wonder what to do with other kinds of non-numeric columns. Raise an error? Treat them as categorical by default?

@kleinschmidt Comments?

Reproducer:

julia> using DataFrames

julia> df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"])
4×2 DataFrames.DataFrame
│ Row │ A │ B   │
├─────┼───┼─────┤
│ 11"M" │
│ 22"F" │
│ 33"F" │
│ 44"M" │

julia> ModelMatrix(ModelFrame(A~B, df))
ERROR: MethodError: Cannot `convert` an object of type String to an object of type Float64
This may have arisen from a call to the constructor Float64(...),
since type constructors fall back to convert methods.
 in copy!(::Base.LinearFast, ::Array{Float64,2}, ::Base.LinearFast, ::Array{String,2}) at ./abstractarray.jl:575
 in modelmat_cols(::Type{Array{Float64,2}}, ::NullableArrays.NullableArray{String,1}) at /home/milan/.julia/DataFrames/src/statsmodels/formula.jl:349
 in #modelmat_cols#122(::Bool, ::Function, ::Type{Array{Float64,2}}, ::Symbol, ::DataFrames.ModelFrame) at /home/milan/.julia/DataFrames/src/statsmodels/formula.jl:342
 in (::DataFrames.#kw##modelmat_cols)(::Array{Any,1}, ::DataFrames.#modelmat_cols, ::Type{Array{Float64,2}}, ::Symbol, ::DataFrames.ModelFrame) at ./<missing>:0
 in DataFrames.ModelMatrix{Array{Float64,2}}(::DataFrames.ModelFrame) at /home/milan/.julia/DataFrames/src/statsmodels/formula.jl:478
 in DataFrames.ModelMatrix{T<:AbstractArray{T<:AbstractFloat,2}}(::DataFrames.ModelFrame) at /home/milan/.julia/DataFrames/src/statsmodels/formula.jl:501
@nalimilan
Copy link
Member Author

Closing as it needs to be fixed in StatsModels now (JuliaStats/StatsModels.jl#13).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant