# How to check if GLM will be able to run OLS beforehand?

**URL:** <https://discourse.julialang.org/t/how-to-check-if-glm-will-be-able-to-run-ols-beforehand/48432>\
**Category:** General Usage\
**Tags:** question, glm\
**Created:** [October 15, 2020, 2:18pm UTC](https://discourse.julialang.org/t/how-to-check-if-glm-will-be-able-to-run-ols-beforehand/48432 "2020-10-15T14:18:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![drarnau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drarnau/32/4476_2.png) [@drarnau](https://discourse.julialang.org/u/drarnau)\
**Post date:** [October 15, 2020, 2:18pm UTC](https://discourse.julialang.org/t/how-to-check-if-glm-will-be-able-to-run-ols-beforehand/48432/1 "2020-10-15T14:18:41Z")

</div>

Dear all,

I use the GLM package to compute a simple OLS regression with simulated data inside a loop. In some cases, the simulated data is such that OLS cannot be computed because `PosDefException: matrix is not positive definite; Cholesky factorization failed.`. That’s completely fine. However, it stops my loop. I would like to (pseudo-code):

```julia
if ols_is_feasible
  return lm(@formula(y ~ x), df)
else
  return Inf
end

```

Any idea how to easily do that?

Thank you.

---

<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:** [October 15, 2020, 2:32pm UTC](https://discourse.julialang.org/t/how-to-check-if-glm-will-be-able-to-run-ols-beforehand/48432/2 "2020-10-15T14:32:28Z")

</div>

I think you are looking for a try/catch statement: [Control Flow · The Julia Language](https://docs.julialang.org/en/v1/manual/control-flow/#The-try/catch-statement)

---

<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 15, 2020, 2:50pm UTC](https://discourse.julialang.org/t/how-to-check-if-glm-will-be-able-to-run-ols-beforehand/48432/3 "2020-10-15T14:50:27Z")

</div>

The documentation is not good for `lm`. But you _can_ estimate a model with perfect multi-colinearity

```julia
julia> t = (y = rand(100), x1 = 1:100, x2 = 1:100);

julia> lm(@formula(y ~ x1 + x2), t, true); # a positional argument `true` here

```

---

<div class="post-metadata">

**Author:** ![drarnau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drarnau/32/4476_2.png) [@drarnau](https://discourse.julialang.org/u/drarnau)\
**Post date:** [October 15, 2020, 2:57pm UTC](https://discourse.julialang.org/t/how-to-check-if-glm-will-be-able-to-run-ols-beforehand/48432/4 "2020-10-15T14:57:51Z")

</div>

That’s brilliant, thank you.

---

<div class="post-metadata">

**Author:** ![drarnau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drarnau/32/4476_2.png) [@drarnau](https://discourse.julialang.org/u/drarnau)\
**Post date:** [October 15, 2020, 2:58pm UTC](https://discourse.julialang.org/t/how-to-check-if-glm-will-be-able-to-run-ols-beforehand/48432/5 "2020-10-15T14:58:32Z")

</div>

Interesting. Is one of the variables ignored here?

---

<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 15, 2020, 3:00pm UTC](https://discourse.julialang.org/t/how-to-check-if-glm-will-be-able-to-run-ols-beforehand/48432/6 "2020-10-15T15:00:48Z")

</div>

Yeah I guess `x2` is ignored.

---

<div class="post-metadata">

**Author:** ![dave.f.kleinschmidt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dave.f.kleinschmidt/32/55_2.png) [@dave.f.kleinschmidt](https://discourse.julialang.org/u/dave.f.kleinschmidt)\
**Post date:** [October 27, 2020, 4:45pm UTC](https://discourse.julialang.org/t/how-to-check-if-glm-will-be-able-to-run-ols-beforehand/48432/7 "2020-10-27T16:45:01Z")

</div>

The `true` tells GLM to use a pivoted cholesky:

[https://github.com/JuliaStats/GLM.jl/blob/ef246bb8fdbfa3f3058435035d0b0cf42abdd06e/src/linpred.jl#L80-L117](https://github.com/JuliaStats/GLM.jl/blob/ef246bb8fdbfa3f3058435035d0b0cf42abdd06e/src/linpred.jl#L80-L117)

and the interface:

[https://github.com/JuliaStats/GLM.jl/blob/29a0e1c574a51d8574a79872b6c250364164a420/src/lm.jl#L125-L145](https://github.com/JuliaStats/GLM.jl/blob/29a0e1c574a51d8574a79872b6c250364164a420/src/lm.jl#L125-L145)

---

<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 27, 2020, 5:14pm UTC](https://discourse.julialang.org/t/how-to-check-if-glm-will-be-able-to-run-ols-beforehand/48432/8 "2020-10-27T17:14:26Z")

</div>

Additionally, I’ve filed an issue a while ago to make this more clear by making `allowrankdeficient` a keyword argument [here](https://github.com/JuliaStats/GLM.jl/issues/351).

It would be a good first issue for someone who wants to get involved in linear estimation in Julia.

---

<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:** [November 1, 2020, 7:32pm UTC](https://discourse.julialang.org/t/how-to-check-if-glm-will-be-able-to-run-ols-beforehand/48432/9 "2020-11-01T19:32:56Z")

</div>

PR [here](https://github.com/JuliaStats/GLM.jl/pull/386).
