# GLM linear regression. How to extract the coefficients?

**URL:** <https://discourse.julialang.org/t/glm-linear-regression-how-to-extract-the-coefficients/28219>\
**Category:** Statistics\
**Tags:** glm\
**Created:** [August 30, 2019, 7:51pm UTC](https://discourse.julialang.org/t/glm-linear-regression-how-to-extract-the-coefficients/28219 "2019-08-30T19:51:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![doomphoenix-qxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doomphoenix-qxz/32/7009_2.png) [@doomphoenix-qxz](https://discourse.julialang.org/u/doomphoenix-qxz)\
**Post date:** [August 30, 2019, 7:51pm UTC](https://discourse.julialang.org/t/glm-linear-regression-how-to-extract-the-coefficients/28219/1 "2019-08-30T19:51:53Z")

</div>

I fit a simple linear regression using `GLM`. How do I extract the calculated slope and intercept?

I looked through the documentation but didn’t see an example of it. If such an example exists, feel free to point there.

---

<div class="post-metadata">

**Author:** ![curious](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/curious/32/7629_2.png) [@curious](https://discourse.julialang.org/u/curious)\
**Post date:** [August 30, 2019, 8:49pm UTC](https://discourse.julialang.org/t/glm-linear-regression-how-to-extract-the-coefficients/28219/2 "2019-08-30T20:49:28Z")

</div>

There may be a better way, but this one works.

```julia
reg = GLM.lm(GLM.@formula(y ~ x), data)
slope = GLM.coef(reg)[2]

```

Also, check

```julia
GLM.coeftable(reg)

```

You can use that, for example, to extract `p` value for the slope.

```julia
pval = GLM.coeftable(reg).cols[4][2]

```

---

<div class="post-metadata">

**Author:** ![yoninazarathy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yoninazarathy/32/10086_2.png) [@yoninazarathy](https://discourse.julialang.org/u/yoninazarathy)\
**Post date:** [August 30, 2019, 9:29pm UTC](https://discourse.julialang.org/t/glm-linear-regression-how-to-extract-the-coefficients/28219/3 "2019-08-30T21:29:23Z")

</div>

See for example Listing 8.5 here: [https://people.smp.uq.edu.au/YoniNazarathy/julia-stats/StatisticsWithJulia.pdf#page289](https://people.smp.uq.edu.au/YoniNazarathy/julia-stats/StatisticsWithJulia.pdf#page289)

---

<div class="post-metadata">

**Author:** ![doomphoenix-qxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doomphoenix-qxz/32/7009_2.png) [@doomphoenix-qxz](https://discourse.julialang.org/u/doomphoenix-qxz)\
**Post date:** [August 30, 2019, 9:56pm UTC](https://discourse.julialang.org/t/glm-linear-regression-how-to-extract-the-coefficients/28219/4 "2019-08-30T21:56:45Z")

</div>

These are exactly what I needed! I marked curious as the solution because he answered first, but really both of you answered it. This could definitely be improved in the GLM docs; once I knew the name of the function I was able to find it, but it’s mentioned quickly in the “Manual” section and never appears in any of the examples… I may try to do a PR.
