# GLM - Questions

**URL:** https://discourse.julialang.org/t/glm-questions/78191
**Category:** New to Julia
**Tags:** glm
**Created:** [March 21, 2022, 4:35am UTC](https://discourse.julialang.org/t/glm-questions/78191 "2022-03-21T04:35:20Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![DaKlingons](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daklingons/32/38427_2.png) [@DaKlingons](https://discourse.julialang.org/u/DaKlingons)
#### Post date: [March 21, 2022, 4:35am UTC](https://discourse.julialang.org/t/glm-questions/78191/1 "2022-03-21T04:35:20Z")

</div>

Hi All.  
I have 2 questions about implementing GLM.jl

1. Weighting the Dependent Variable - I’m looking at using the Poisson distribution, whereby the dependent variable, is weighted by a Unit of Exposure (DaysActive/365).  
In SAS you use the Weight function, In R its the Offset function. I haven’t found the equivalent in Julia in any example or document. Am I missing the obvious here?

2. I have some a categorical variable with ~180 levels, when I fit some are statistically significant, others are not. I’d like to only keep those which are significant (pasimony). In R one uses (var==“value1”)+(var=="value2) which is read like a IF statement creating a dummy variable, SAS is similar. Again when looking online whilst I see some information about "contrasts = Dict(:var =\> DummyCoding(), I’m unsure how to use this in what I am doing.

I would assume these are really common user cases, so hopefully someone can provide clarity (or reference to some example I may have missed or other package which may be appropriate).

Thanks

---

<div class="post-metadata">

### Author: ![huang\_min](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huang_min/32/34337_2.png) [@huang\_min](https://discourse.julialang.org/u/huang_min)
#### Post date: [March 21, 2022, 4:47am UTC](https://discourse.julialang.org/t/glm-questions/78191/2 "2022-03-21T04:47:57Z")

</div>

I see both wts and offset arguments in GLM.jl

I do not see why a person needs a categorical variable with 180 levels. Why not making it continuous?

---

<div class="post-metadata">

### Author: ![DaKlingons](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daklingons/32/38427_2.png) [@DaKlingons](https://discourse.julialang.org/u/DaKlingons)
#### Post date: [March 21, 2022, 6:01am UTC](https://discourse.julialang.org/t/glm-questions/78191/3 "2022-03-21T06:01:58Z")

</div>

Hi huang\_min,  
Do you have an example using wts &/or offsets with a Poisson regression? In my tests did not work (but user error is possible to likely).

With respect to Categorical Variables, the Dataset I’m using is ~2.5m rows. The categorical variable is geographic areas (think suburb\_town as groups of addresses).  
I generally:

1. Start by putting all levels in for any categorical variable
2. Remove grossly insignificant ones (t-test) & retest as I remove.
3. Group similar co-efficient’s especially if geographic area is close (use AIC/BIC)
4. Finally settle on a selection of single & grouped variables for the final iteration.

From experience, its fairly dangerous to treat say an Integer representation of a Categorical Variable as continuous. Our user cases may well be different.

Thanks

---

<div class="post-metadata">

### Author: ![huang\_min](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huang_min/32/34337_2.png) [@huang\_min](https://discourse.julialang.org/u/huang_min)
#### Post date: [March 21, 2022, 6:47am UTC](https://discourse.julialang.org/t/glm-questions/78191/4 "2022-03-21T06:47:27Z")

</div>

using GLM, DataFrames  
df = DataFrame(:y =\> rand(1:20,100), :x =\> rand(100), :d =\> rand(100))  
glm(@formula(y ~ x),df,Poisson(), wts = df[!,:d])

I think this is a minimal example you need.

As for the second one, maybe you can try MixedModels.jl.

---

<div class="post-metadata">

### Author: ![DaKlingons](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daklingons/32/38427_2.png) [@DaKlingons](https://discourse.julialang.org/u/DaKlingons)
#### Post date: [March 21, 2022, 10:56pm UTC](https://discourse.julialang.org/t/glm-questions/78191/5 "2022-03-21T22:56:50Z")

</div>

Hi huang\_min.

Many thanks for your example. It was very instructive. What I found was:

1. Your example worked perfectly, yet mine did not.

2. I examined the wts = df[!,:d]) in your & my equivalent and noticed mine was described as  
Vector{Union{Missing, Float64}} whereas yours was Vector{Float64}.

Whilst there were no Missing values in the underlying dataset (explicitly removed prior to creating the data frame), the Vector has made an allowance for Missing Values.

1. I changed the code slightly to wts=coalesce.(df[!,:d], 0) which overwrites Missing Values and this changed it to Vector{Float64}, and this worked without issue in the glm function.

A good learning as the error message I was getting "  
TypeError: in keyword argument wts, expected AbstractArray{#s37,1} where #s37\<:Real, got Array{Union{Missing, Float64},1}" did not make sense to me.

I will review MixedModels.jl to see if this assists in specifying categorical variables.

---

<div class="post-metadata">

### Author: ![huang\_min](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huang_min/32/34337_2.png) [@huang\_min](https://discourse.julialang.org/u/huang_min)
#### Post date: [March 22, 2022, 1:31am UTC](https://discourse.julialang.org/t/glm-questions/78191/6 "2022-03-22T01:31:42Z")

</div>

It’s great to know that.
