How to change the type of a variable for a loaded table?

I’m trying to use JuliaDB to load tables and then use MixedModels to fit models.

The MixedModels package requires the clustering variable “id” to be categorical, but in my dataset “id” is integer.

So my question is, how can I change the type of “id” to categorical after loading the table? One solution is to convert the table to a DataFrame and then change the type, but I’m sure there are better solutions.


categorical!(df::DataFrame, cols::Type=Union{AbstractString, Missing};  compress::Bool=false)

You can do that with ScientificTypes fairly easily (actually that’s exactly the kind of use case ST was developed for)

julia> using ScientificTypes
julia> t = JuliaDB.table(1:10, randn(10), rand(Bool, 10); names = [:x, :y, :z]);
julia> t2 = coerce(t, :z => Multiclass)

In the background what that does is form a derived table where the column :z has been obtained by doing categorical(select(t, z)); if you wanted to have that as ordered categoricals, coerce(t, :z => OrderedFactor) does the trick. t2 has the same type as t.

1 Like

What version of MixedModels are you using? I thought we changed the behavior so that a clustering variable did not need to be categorical.

1 Like

Hi Thibaut, thank you for the reply. I’m running Julia v1.1 and just installed ScientificTypes v0.2.3. I ran the example you gave and got the error message “type IndexedTable has no field x”.

I’m running Julia v1.1 and MixedModels v2.1.2.

Can you upgrade to v.2.2.0 and try again?

Sure. Looks like I need to upgrade Julia to v1.3 too. I’ll get back to you in a bit.

I’ve upgraded to MixedModels v2.2 and Julia v1.3.1, but it’s still asking the blocking variable to be categorical.

**ERROR:** ArgumentError: blocking variables (those behind |) must be Categorical (id is not)

You may have entered a typo, below is the exact script I just ran in a fresh environment

using JuliaDB, ScientificTypes

t = JuliaDB.table(1:10, randn(10), rand(Bool, 10); names = [:x, :y, :z]);

t2 = coerce(t, :z => Multiclass)

@show typeof(t2)
@show typeof(select(t2, :z))

Here’s the output

typeof(t2) = IndexedTable{StructArrays.StructArray{NamedTuple{(:x, :y, :z),Tuple{Int64,Float64,CategoricalArrays.CategoricalValue{Bool,UInt8}}},1,NamedTuple{(:x, :y, :z),Tuple{Array{Int64,1},Array{Float64,1},CategoricalArrays.CategoricalArray{Bool,1,UInt8,Bool,CategoricalArrays.CategoricalValue{Bool,UInt8},Union{}}}},Int64}}
typeof(select(t2, :z)) = CategoricalArrays.CategoricalArray{Bool,1,UInt8,Bool,CategoricalArrays.CategoricalValue{Bool,UInt8},Union{}}

I’m on Julia 1.3.1 with JuliaDB 0.13 and ScientificType 0.3.2

Thanks for checking.

Thanks for checking. I upgraded to Julia v1.3.1 and copied the same code above and it works now. So it might have been a problem with v1.1. In any case, thanks for your help.

1 Like