# How to use the second method of GLM.lm?

**URL:** <https://discourse.julialang.org/t/how-to-use-the-second-method-of-glm-lm/121139>\
**Category:** Statistics\
**Created:** [October 10, 2024, 11:29am UTC](https://discourse.julialang.org/t/how-to-use-the-second-method-of-glm-lm/121139 "2024-10-10T11:29:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MJulia](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@MJulia](https://discourse.julialang.org/u/MJulia)\
**Post date:** [October 10, 2024, 11:29am UTC](https://discourse.julialang.org/t/how-to-use-the-second-method-of-glm-lm/121139/1 "2024-10-10T11:29:03Z")

</div>

In the documents of GLM.lm, it says:

“In the second method, X must be a matrix holding values of the independent variable(s) in columns (including if appropriate the intercept), and y must be a vector holding values of the dependent variable.”

I want to use the second method to get same results of the first method of GLM.lm, but how to let X hold a column for the intercept? I haven’t found an example for it.

---

<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 10, 2024, 3:53pm UTC](https://discourse.julialang.org/t/how-to-use-the-second-method-of-glm-lm/121139/2 "2024-10-10T15:53:44Z")

</div>

```julia
julia> X = rand(5, 3)
5×3 Matrix{Float64}:
 0.865508 0.657168 0.722091
 0.428188 0.443666 0.443288
 0.717246 0.957425 0.889641
 0.337297 0.0668421 0.500019
 0.769625 0.411271 0.811622

julia> [ones(size(X, 1)) X]
5×4 Matrix{Float64}:
 1.0 0.865508 0.657168 0.722091
 1.0 0.428188 0.443666 0.443288
 1.0 0.717246 0.957425 0.889641
 1.0 0.337297 0.0668421 0.500019
 1.0 0.769625 0.411271 0.811622

```

---

<div class="post-metadata">

**Author:** ![MJulia](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@MJulia](https://discourse.julialang.org/u/MJulia)\
**Post date:** [October 11, 2024, 12:59am UTC](https://discourse.julialang.org/t/how-to-use-the-second-method-of-glm-lm/121139/3 "2024-10-11T00:59:38Z")

</div>

Thanks:)
