# Regression using Categorical variables

**URL:** <https://discourse.julialang.org/t/regression-using-categorical-variables/7521>\
**Category:** General Usage\
**Tags:** dataframes, glm\
**Created:** [December 5, 2017, 11:01am UTC](https://discourse.julialang.org/t/regression-using-categorical-variables/7521 "2017-12-05T11:01:08Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![lukaebby](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lukaebby/32/4055_2.png) [@lukaebby](https://discourse.julialang.org/u/lukaebby)\
**Post date:** [December 5, 2017, 11:01am UTC](https://discourse.julialang.org/t/regression-using-categorical-variables/7521/1 "2017-12-05T11:01:08Z")

</div>

Can anyone tell me how to define categorical variables (with more than two levels) while performing regression in Julia…?  
Thanks  
say my data is

```julia
my_dataframe= DataFrame(class=[3,1,3,2,1,2,1],hist=[34,36,38,40,42,44,46], math=[32,65,98,78,45,12,92], scie=[45,98,56,23,12,45,95], espan= [98,65,45,75,62,35,76], engl =[84,62,74,93,62,46,83], rank =[1,2,3,4,5,6,7])

```

Everything except class NOT categorical.  
How would i say that class is categorical.?  
Furthermore, would it fit the regression considering the qualitative factor…?

```julia
using GLM
linearmodel= fit(LinearModel, @formula(rank ~ maths+engl+science+social+civics+class), my_dataframe)

```

Thanks…!!  
Ebby

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [December 5, 2017, 11:02am UTC](https://discourse.julialang.org/t/regression-using-categorical-variables/7521/2 "2017-12-05T11:02:30Z")

</div>

Just do:

```julia
using CategoricalArrays
DataFrame(class=categorical([3,1,3,2,1,2,1]), ...)

```

(Please use triple backquotes around code blocks so that they are rendered as such.)

---

<div class="post-metadata">

**Author:** ![lukaebby](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lukaebby/32/4055_2.png) [@lukaebby](https://discourse.julialang.org/u/lukaebby)\
**Post date:** [December 5, 2017, 11:54am UTC](https://discourse.julialang.org/t/regression-using-categorical-variables/7521/3 "2017-12-05T11:54:41Z")

</div>

Thanks…!

---

<div class="post-metadata">

**Author:** ![lukaebby](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lukaebby/32/4055_2.png) [@lukaebby](https://discourse.julialang.org/u/lukaebby)\
**Post date:** [December 5, 2017, 11:54am UTC](https://discourse.julialang.org/t/regression-using-categorical-variables/7521/4 "2017-12-05T11:54:42Z")

</div>

Thanks,  
But when I perform the following,

Pkg.add(“CategoricalArrays”)  
using CategoricalArrays  
using DataFrames  
my\_dataframe=DataFrame(class=categorical([3,1,3,2,1,2,1]),civics=[34,36,38,40,42,44,46], maths=[32,65,98,78,45,12,92], science=[45,98,56,23,12,45,95], social= [98,65,45,75,62,35,76], engl =[84,62,74,93,62,46,83], rank =[1,2,3,4,5,6,7])

it says

MethodError: no method matching upgrade\_vector(::CategoricalArrays.CategoricalArray{Int64,1,UInt32})  
Closest candidates are:

upgrade\_vector(!Matched::BitArray{1}) at C:\Users\uqethom9.julia\v0.6\DataFrames\src\dataframe\dataframe.jl:349  
upgrade\_vector(!Matched::Array{T,1} where T) at C:\Users\uqethom9.julia\v0.6\DataFrames\src\dataframe\dataframe.jl:347  
upgrade\_vector(!Matched::Range) at C:\Users\uqethom9.julia\v0.6\DataFrames\src\dataframe\dataframe.jl:348  
…  
setindex!(::DataFrames.DataFrame, ::CategoricalArrays.CategoricalArray{Int64,1,UInt32}, ::Symbol) at dataframe.jl:364  
#DataFrame#19(::Array{Any,1}, ::Type{T} where T) at dataframe.jl:100  
(::Core.#kw#Type)(::Array{Any,1}, ::Type{DataFrames.DataFrame}) at :0  
include\_string(::String, ::String) at loading.jl:522  
include\_string(::String, ::String, ::Int64) at eval.jl:30  
include\_string(::Module, ::String, ::String, ::Int64, ::Vararg{Int64,N} where N) at eval.jl:34  
(::Atom.##49#53{String,Int64,String})() at eval.jl:50  
withpath(::Atom.##49#53{String,Int64,String}, ::String) at utils.jl:30  
withpath(::Function, ::String) at eval.jl:38  
macro expansion at eval.jl:49 [inlined]  
(::Atom.##48#52{Dict{String,Any}})() at task.jl:80

And I am sorry that I did not understand what you meant by this “(Please use triple backquotes around code blocks so that they are rendered as such.)”

Ebby

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 5, 2017, 12:28pm UTC](https://discourse.julialang.org/t/regression-using-categorical-variables/7521/5 "2017-12-05T12:28:16Z")

</div>

````
```

````

code here  
```  
Use the backwards apostrophe “tick” three times to begin and end your block of code.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 5, 2017, 1:34pm UTC](https://discourse.julialang.org/t/regression-using-categorical-variables/7521/6 "2017-12-05T13:34:19Z")

</div>

> [@lukaebby](#):
>
> And I am sorry that I did not understand what you meant by this “(Please use triple backquotes around code blocks so that they are rendered as such.)”

> [@PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530):
>
> This is a short post on how to use backticks (` and ```) to quote code, so it is easy to read. This post can be linked to new users who are confused by this feature. Why By quoting your code, you get a monospaced font (which preserves indentation) and syntax highlighting. This makes it easier to read your code and help you. Displayed code looks like this: function displayed\_code(x::Int, y::Int) if x \< y println("x is smaller") else println("or not") end end Inline…

You can still edit your comment.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [December 5, 2017, 1:53pm UTC](https://discourse.julialang.org/t/regression-using-categorical-variables/7521/7 "2017-12-05T13:53:01Z")

</div>

I can reproduce the error:

```julia
using CategoricalArrays, DataFrames
a = categorical([3,1,3,2,1,2,1])
DataFrame(a = a, b = 1:7)

```

gives

```julia
ERROR: MethodError: no method matching upgrade_vector(::CategoricalArrays.CategoricalArray{Int64,1,UInt32})
Closest candidates are:
  upgrade_vector(::BitArray{1}) at /Users/michael/.julia/v0.6/DataFrames/src/dataframe/dataframe.jl:349
  upgrade_vector(::Array{T,1} where T) at /Users/michael/.julia/v0.6/DataFrames/src/dataframe/dataframe.jl:347
  upgrade_vector(::Range) at /Users/michael/.julia/v0.6/DataFrames/src/dataframe/dataframe.jl:348
  ...
Stacktrace:
 [1] setindex!(::DataFrames.DataFrame, ::CategoricalArrays.CategoricalArray{Int64,1,UInt32}, ::Symbol) at /Users/michael/.julia/v0.6/DataFrames/src/dataframe/dataframe.jl:364
 [2] #DataFrame#19(::Array{Any,1}, ::Type{T} where T) at /Users/michael/.julia/v0.6/DataFrames/src/dataframe/dataframe.jl:100
 [3] (::Core.#kw#Type)(::Array{Any,1}, ::Type{DataFrames.DataFrame}) at ./<missing>:100

```

Version congruency issue?

```julia
Pkg.status.(["DataFrames", "CategoricalArrays"])
 - DataFrames 0.10.1
 - CategoricalArrays 0.1.6

```

---

<div class="post-metadata">

**Author:** ![lukaebby](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lukaebby/32/4055_2.png) [@lukaebby](https://discourse.julialang.org/u/lukaebby)\
**Post date:** [December 6, 2017, 12:14am UTC](https://discourse.julialang.org/t/regression-using-categorical-variables/7521/8 "2017-12-06T00:14:04Z")

</div>

Thanks for trying them. But

`Pkg.status.(["DataFrames","CategoricalArrays","GLM"]) `

- DataFrames 0.10.1
- CategoricalArrays 0.1.6
- GLM 0.8.1

And GLM and DataFrames work perfectly together.!

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [December 6, 2017, 7:04am UTC](https://discourse.julialang.org/t/regression-using-categorical-variables/7521/10 "2017-12-06T07:04:15Z")

</div>

I don’t understand, I just said that I can indeed reproduce.

---

<div class="post-metadata">

**Author:** ![ValdarT](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/valdart/32/24146_2.png) [@ValdarT](https://discourse.julialang.org/u/ValdarT)\
**Post date:** [December 6, 2017, 9:58am UTC](https://discourse.julialang.org/t/regression-using-categorical-variables/7521/11 "2017-12-06T09:58:08Z")

</div>

Yes, you need the new DataFrames (v0.11).

---

<div class="post-metadata">

**Author:** ![fraxx001](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fraxx001/32/8613_2.png) [@fraxx001](https://discourse.julialang.org/u/fraxx001)\
**Post date:** [May 27, 2019, 8:59pm UTC](https://discourse.julialang.org/t/regression-using-categorical-variables/7521/12 "2019-05-27T20:59:35Z")

</div>

Sorry to open this again but Iam not able to use a simple logistic regression with glm:

```julia
a = categorical([3,1,3,2,1,2,1])
df = DataFrame(a = a, b = 1:7)
glm(@formula(a ~ b), df, Binomial(), LogitLink())

```

gives:

```julia
MethodError: no method matching zero(::Type{CategoricalValue{Int64,UInt32}})
Closest candidates are:
  zero(!Matched::Type{LibGit2.GitHash}) at /build/julia/src/julia-1.1.0/usr/share/julia/stdlib/v1.1/LibGit2/src/oid.jl:220
  zero(!Matched::Type{Pkg.Resolve.VersionWeights.VersionWeight}) at /build/julia/src/julia-1.1.0/usr/share/julia/stdlib/v1.1/Pkg/src/resolve/VersionWeights.jl:19
  zero(!Matched::Type{Pkg.Resolve.MaxSum.FieldValues.FieldValue}) at /build/julia/src/julia-1.1.0/usr/share/julia/stdlib/v1.1/Pkg/src/resolve/FieldValues.jl:44
  ...

Stacktrace:
 [1] float(::Array{CategoricalValue{Int64,UInt32},1}) at ./float.jl:886
 [2] #fit#9(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::Type{GeneralizedLinearModel}, ::Array{Float64,2}, ::Array{CategoricalValue{Int64,UInt32},1}, ::Binomial{Float64}, ::LogitLink) at /home/alexander/.julia/packages/GLM/YjBzj/src/glmfit.jl:323
 [3] fit(::Type{GeneralizedLinearModel}, ::Array{Float64,2}, ::Array{CategoricalValue{Int64,UInt32},1}, ::Binomial{Float64}, ::LogitLink) at /home/alexander/.julia/packages/GLM/YjBzj/src/glmfit.jl:323
 [4] #fit#36(::Dict{Any,Any}, ::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::Type{GeneralizedLinearModel}, ::Formula, ::DataFrame, ::Binomial{Float64}, ::Vararg{Any,N} where N) at /home/alexander/.julia/packages/StatsModels/pBxdt/src/statsmodel.jl:72
 [5] fit(::Type{GeneralizedLinearModel}, ::Formula, ::DataFrame, ::Binomial{Float64}, ::LogitLink) at /home/alexander/.julia/packages/StatsModels/pBxdt/src/statsmodel.jl:66
 [6] #glm#10(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::Formula, ::DataFrame, ::Binomial{Float64}, ::Vararg{Any,N} where N) at /home/alexander/.julia/packages/GLM/YjBzj/src/glmfit.jl:326
 [7] glm(::Formula, ::DataFrame, ::Binomial{Float64}, ::Vararg{Any,N} where N) at /home/alexander/.julia/packages/GLM/YjBzj/src/glmfit.jl:326
 [8] top-level scope at In[209]:3

```

---

<div class="post-metadata">

**Author:** ![aharoun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aharoun/32/6887_2.png) [@aharoun](https://discourse.julialang.org/u/aharoun)\
**Post date:** [May 28, 2019, 2:25am UTC](https://discourse.julialang.org/t/regression-using-categorical-variables/7521/13 "2019-05-28T02:25:20Z")

</div>

It looks like GLM does not support categorical variables on the left hand side. Also I don’t think GLM does multinomial logistic regression.

May be [flux](https://fluxml.ai/Flux.jl/v0.1.0/examples/logreg.html) can do what you want.

---

<div class="post-metadata">

**Author:** ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)\
**Post date:** [May 28, 2019, 8:06am UTC](https://discourse.julialang.org/t/regression-using-categorical-variables/7521/14 "2019-05-28T08:06:44Z")

</div>

You have more than 2 categories in `a` but you’re specifying a binomial distribution. That cannot be done. Unless the `a` is meant to be counts? If it’s categories you want you need to specify a multinomial distribution or a categorical.

---

<div class="post-metadata">

**Author:** ![fraxx001](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fraxx001/32/8613_2.png) [@fraxx001](https://discourse.julialang.org/u/fraxx001)\
**Post date:** [May 28, 2019, 8:48am UTC](https://discourse.julialang.org/t/regression-using-categorical-variables/7521/15 "2019-05-28T08:48:03Z")

</div>

Hi,  
thanks for the reply. I tried it with a binary classification problem and it gives the same error. It seems that GLM can’t use Categorical Values in the target. This was a bad example from me. From a Mathematical point of view youre absolutly right. Logistic Regression cant be applied on multiclassification problems.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [April 20, 2020, 4:19am UTC](https://discourse.julialang.org/t/regression-using-categorical-variables/7521/16 "2020-04-20T04:19:44Z")

</div>

> [@nalimilan](#):
>
> DataFrame(class=categorical([3,1,3,2,1,2,1]), …)

What if you want to change certain variables from an existing dataframe. ie. “consume”, in df, which is an imported .xlsx

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [April 20, 2020, 8:14am UTC](https://discourse.julialang.org/t/regression-using-categorical-variables/7521/17 "2020-04-20T08:14:09Z")

</div>

Can you, as usual, attempt to create an MWE to show what you’re trying to do, what you expect to happen, and what is going wrong?

---

<div class="post-metadata">

**Author:** ![nicolas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nicolas/32/26439_2.png) [@nicolas](https://discourse.julialang.org/u/nicolas)\
**Post date:** [February 19, 2021, 10:23pm UTC](https://discourse.julialang.org/t/regression-using-categorical-variables/7521/18 "2021-02-19T22:23:45Z")

</div>

I assume you want to change from numeric to categorical:

```julia
categorical!(df, :consume)

```
