# Comparing functions with their alias in GLM.jl

**URL:** https://discourse.julialang.org/t/comparing-functions-with-their-alias-in-glm-jl/47593
**Category:** General Usage
**Tags:** question, package, glm
**Created:** [October 1, 2020, 11:30am UTC](https://discourse.julialang.org/t/comparing-functions-with-their-alias-in-glm-jl/47593 "2020-10-01T11:30:39Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![yordiak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yordiak/32/12584_2.png) [@yordiak](https://discourse.julialang.org/u/yordiak)
#### Post date: [October 1, 2020, 11:30am UTC](https://discourse.julialang.org/t/comparing-functions-with-their-alias-in-glm-jl/47593/1 "2020-10-01T11:30:39Z")

</div>

Not sure how clear the title is or whether this is the right place for asking the following question, but any way… 🙂

I am testing the `GLM` package mostly for educational purposes. In the package documentation I saw that the `lm` function is an alias to `fit(LinearModel,...)` and `glm` for `fit(GeneralizedLinearModel)`. However, when I compare the two functions, I get a `false` statement. For instance,

```julia
using DataFrames, GLM

data = DataFrame(X=[1,2,3], Y=[1,0,1])

probit1 = glm(@formula(Y~X), data, Binomial(), ProbitLink())
probit2 =fit(GeneralizedLinearModel, @formula(Y~X), data, Binomial(), ProbitLink())

probit1 == probit2 # return false

```

I don’t understand why this happens (the false return)

---

<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: [October 1, 2020, 12:06pm UTC](https://discourse.julialang.org/t/comparing-functions-with-their-alias-in-glm-jl/47593/2 "2020-10-01T12:06:49Z")

</div>

Try `isapprox`, I guess? Maybe there are numerical differences that don’t matter qualitatively but get detected by `==`

---

<div class="post-metadata">

### Author: ![yordiak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yordiak/32/12584_2.png) [@yordiak](https://discourse.julialang.org/u/yordiak)
#### Post date: [October 1, 2020, 12:15pm UTC](https://discourse.julialang.org/t/comparing-functions-with-their-alias-in-glm-jl/47593/3 "2020-10-01T12:15:42Z")

</div>

also, `isequal(probit1,probit2)` returns false

---

<div class="post-metadata">

### Author: ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)
#### Post date: [October 1, 2020, 12:17pm UTC](https://discourse.julialang.org/t/comparing-functions-with-their-alias-in-glm-jl/47593/4 "2020-10-01T12:17:15Z")

</div>

I’m not exactly sure what this comparison does but glms are maximum likelihood estimations so you would have to set a seed I think. Is this the same for `lm`? Of course numerical differences may still persist

---

<div class="post-metadata">

### Author: ![yordiak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yordiak/32/12584_2.png) [@yordiak](https://discourse.julialang.org/u/yordiak)
#### Post date: [October 1, 2020, 12:19pm UTC](https://discourse.julialang.org/t/comparing-functions-with-their-alias-in-glm-jl/47593/5 "2020-10-01T12:19:18Z")

</div>

I was just playing around. However, I expected a `true` returns, since `glm` is an alias of `fit(GeneralizedLinearModel,...)`

---

<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: [October 1, 2020, 12:20pm UTC](https://discourse.julialang.org/t/comparing-functions-with-their-alias-in-glm-jl/47593/6 "2020-10-01T12:20:23Z")

</div>

sorry I meant `isapprox` and edited my post

---

<div class="post-metadata">

### Author: ![yordiak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yordiak/32/12584_2.png) [@yordiak](https://discourse.julialang.org/u/yordiak)
#### Post date: [October 1, 2020, 12:21pm UTC](https://discourse.julialang.org/t/comparing-functions-with-their-alias-in-glm-jl/47593/7 "2020-10-01T12:21:45Z")

</div>

`isapprox(probit1,probit2)` returns an error

> MethodError: no method matching isapprox(::StatsModels.TableRegressionModel{GeneralizedLinearModel{GLM.GlmResp{Array{Float64,1},Binomial{Float64},ProbitLink},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}, ::StatsModels.TableRegressionModel{GeneralizedLinearModel{GLM.GlmResp{Array{Float64,1},Binomial{Float64},ProbitLink},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}})

I am using Julia 1.5.2 on win10 64bit, GLM 1.3.10

---

<div class="post-metadata">

### Author: ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)
#### Post date: [October 1, 2020, 12:22pm UTC](https://discourse.julialang.org/t/comparing-functions-with-their-alias-in-glm-jl/47593/8 "2020-10-01T12:22:19Z")

</div>

> [@yordiak](#):
>
> ```julia
> using DataFrames, GLM
> 
> data = DataFrame(X=[1,2,3], Y=[1,0,1])
> 
> probit1 = glm(@formula(Y~X), data, Binomial(), ProbitLink())
> probit2 =fit(GeneralizedLinearModel, @formula(Y~X), data, Binomial(), ProbitLink())
> 
> probit1 == probit2      
> 
> ```

Note that

```julia
julia> glm(@formula(Y~X), data, Binomial(), ProbitLink()) == glm(@formula(Y~X), data, Binomial(), ProbitLink()) 
false

```

---

<div class="post-metadata">

### Author: ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)
#### Post date: [October 1, 2020, 12:24pm UTC](https://discourse.julialang.org/t/comparing-functions-with-their-alias-in-glm-jl/47593/9 "2020-10-01T12:24:33Z")

</div>

I think the problem is with the comparison.  
This works for me so the models are equivalent:

```julia
julia> coef(probit1) == coef(probit2)
true

julia> stderr(probit1) == stderr(probit2)
true

```

---

<div class="post-metadata">

### Author: ![yordiak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yordiak/32/12584_2.png) [@yordiak](https://discourse.julialang.org/u/yordiak)
#### Post date: [October 1, 2020, 12:24pm UTC](https://discourse.julialang.org/t/comparing-functions-with-their-alias-in-glm-jl/47593/10 "2020-10-01T12:24:57Z")

</div>

> [@danielw2904](#):
>
> ```julia
> 
> ```

why that happens? I am asking just for curiosity

---

<div class="post-metadata">

### Author: ![yordiak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yordiak/32/12584_2.png) [@yordiak](https://discourse.julialang.org/u/yordiak)
#### Post date: [October 1, 2020, 12:26pm UTC](https://discourse.julialang.org/t/comparing-functions-with-their-alias-in-glm-jl/47593/11 "2020-10-01T12:26:29Z")

</div>

I am getting the same result. Only when I compare `probit1 ==probit2` I get a `false` return

---

<div class="post-metadata">

### Author: ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)
#### Post date: [October 1, 2020, 12:29pm UTC](https://discourse.julialang.org/t/comparing-functions-with-their-alias-in-glm-jl/47593/12 "2020-10-01T12:29:12Z")

</div>

I am not the right person to answer that question with authority but basically `==` is implemented via multiple dispatch for different types.

e.g.

```julia
julia> typeof(probit1)
StatsModels.TableRegressionModel{GeneralizedLinearModel{GLM.GlmResp{Array{Float64,1},Binomial{Float64},ProbitLink},GLM.DensePredChol{Float64,Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}

```

so it is not clear what it does exactly for this type. One would have to check the source of the GLM package if that defines a specific `==`. There is a fallback I think:

from the docs:

```julia
help?> isequal
search: isequal issetequal InverseSquareLink

  isequal(x, y)

  Similar to ==, except for the treatment of floating point numbers and of missing values. isequal treats all floating-point NaN values as equal to each other, treats -0.0 as
  unequal to 0.0, and missing as equal to missing. Always returns a Bool value.

  Implementation
  ≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡

  The default implementation of isequal calls ==, so a type that does not involve floating-point values generally only needs to define ==.

  isequal is the comparison function used by hash tables (Dict). isequal(x,y) must imply that hash(x) == hash(y).

  This typically means that types for which a custom == or isequal method exists must implement a corresponding hash method (and vice versa). Collections typically implement isequal
  by calling isequal recursively on all contents.

  Scalar types generally do not need to implement isequal separate from ==, unless they represent floating-point numbers amenable to a more efficient implementation than that
  provided as a generic fallback (based on isnan, signbit, and ==).

```

EDIT:

Looks like the fallback is called which would not work I guess

```julia
julia> @which isequal(probit1, probit2)
isequal(x, y) in Base at operators.jl:123

```

---

<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: [October 3, 2020, 9:50pm UTC](https://discourse.julialang.org/t/comparing-functions-with-their-alias-in-glm-jl/47593/13 "2020-10-03T21:50:22Z")

</div>

By default `==` falls back to `===`, and I don’t think anybody cared about implementing `==` for GLMs. So two different fits will always be considered as different. PR welcome to implement a smarter behavior (by calling `==` recursively on relevant fields).

---

<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: [October 4, 2020, 8:34am UTC](https://discourse.julialang.org/t/comparing-functions-with-their-alias-in-glm-jl/47593/14 "2020-10-04T08:34:59Z")

</div>

I am not sure that `==` is a very useful operator for types like this to expose in the API. For any nontrivial data, you only get exact `==` if you run the exact same exercise.
