# Add method for GLM.LinearModel: MethodError: no method found

**URL:** <https://discourse.julialang.org/t/add-method-for-glm-linearmodel-methoderror-no-method-found/13036>\
**Category:** New to Julia\
**Tags:** fit, glm\
**Created:** [August 8, 2018, 12:22pm UTC](https://discourse.julialang.org/t/add-method-for-glm-linearmodel-methoderror-no-method-found/13036 "2018-08-08T12:22:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DominiqueMakowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dominiquemakowski/32/51410_2.png) [@DominiqueMakowski](https://discourse.julialang.org/u/DominiqueMakowski)\
**Post date:** [August 8, 2018, 12:22pm UTC](https://discourse.julialang.org/t/add-method-for-glm-linearmodel-methoderror-no-method-found/13036/1 "2018-08-08T12:22:39Z")

</div>

I am trying to implement a method GLM’s linear model. Unfortunately, I can’t specify this type in the function.

```julia
using GLM, RDatasets

iris = dataset("datasets", "iris")

fit = lm(@formula(SepalLength ~ PetalWidth), iris)

function print_r2(fit::LinearModel)
    
    R2 = r2(fit)
    R2_Adj = adjr2(fit)
    println("The model's explanatory power (R2) is of $(round(R2, 2)) (Adj. R2 = $(round(R2_Adj, 2))).")
    
end

print_r2(fit)

```

```julia
MethodError: no method matching print_r2(::StatsModels.DataFrameRegressionModel{GLM.LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,Base.LinAlg.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}})
Closest candidates are:
  print_r2(::GLM.LinearModel) at In[7]:9

```

What is wrong with it? Should I specify the whole big type (StatsModels.DataFrame…) ?

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [August 8, 2018, 12:40pm UTC](https://discourse.julialang.org/t/add-method-for-glm-linearmodel-methoderror-no-method-found/13036/2 "2018-08-08T12:40:38Z")

</div>

This is a duplicate of [types - Julia: Creating a method for a subtype (LinearModel) - Stack Overflow](https://stackoverflow.com/questions/51746593/julia-creating-a-method-for-a-subtype-linearmodel). I have written the answer there. The short explanation of the problem is that type of `fit` is not subtype of `GLM.LinearModel`.

This is what the error message tries to tell you; i.e. that the type of `fit` does not match method signature. Actually in Julia you could even leave out type specification like this:

```julia
function print_r2(fit)
[...]

```

and you should be fine most of the time.

---

<div class="post-metadata">

**Author:** ![DominiqueMakowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dominiquemakowski/32/51410_2.png) [@DominiqueMakowski](https://discourse.julialang.org/u/DominiqueMakowski)\
**Post date:** [August 8, 2018, 12:47pm UTC](https://discourse.julialang.org/t/add-method-for-glm-linearmodel-methoderror-no-method-found/13036/3 "2018-08-08T12:47:53Z")

</div>

Thanks, and sorry for the duplicate!
