# Linear regression without the intercept term

**URL:** <https://discourse.julialang.org/t/linear-regression-without-the-intercept-term/18063>\
**Category:** Statistics\
**Tags:** question, regression, fit, glm\
**Created:** [November 27, 2018, 1:36pm UTC](https://discourse.julialang.org/t/linear-regression-without-the-intercept-term/18063 "2018-11-27T13:36:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![leejm516](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leejm516/32/8219_2.png) [@leejm516](https://discourse.julialang.org/u/leejm516)\
**Post date:** [November 27, 2018, 1:36pm UTC](https://discourse.julialang.org/t/linear-regression-without-the-intercept-term/18063/1 "2018-11-27T13:36:36Z")

</div>

In GLM.jl, the use of DataFrame is preferred, but the lm function does support the use of vectors and matrices. In the latter case, however, I can’t do fit without the intercept term (i.e. b0 = 0). Is there a way to do this without using DataFrame?

When I saw the source codes, the X argument in the lm function should be AbstractMatrix, not AbstractVector.

Certainly, when using DataFrame, there is no problem at all; this question is just my curiosity 🙂

```julia
julia> using GLM; x = [1,2,3]; y = [2,5,7];

julia> ols = lm(@formula(Y ~ 0 + X), data)
StatsModels.DataFrameRegressionModel{LinearModel{LmResp{Array{Float64,1}},DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}

Formula: Y ~ +X

Coefficients:
     Estimate Std.Error t value Pr(>|t|)
X 2.35714 0.0874818 26.9444 0.0014

julia> ols = lm(x, y) # regression without the intercept term
ERROR: MethodError: no method matching fit(::Type{LinearModel}, ::Array{Int64,1}, ::Array{Int64,1}, ::Bool)
Closest candidates are:
  fit(::Type{StatsBase.Histogram}, ::Any...; kwargs...) at C:\Users\leejm516\.julia\packages\StatsBase\56Djy\src\hist.jl:319
  fit(::StatsBase.StatisticalModel, ::Any...) at C:\Users\leejm516\.julia\packages\StatsBase\56Djy\src\statmodels.jl:151
  fit(::Type{D<:Distributions.Distribution}, ::Any...) where D<:Distributions.Distribution at C:\Users\leejm516\.julia\packages\Distributions\WHjOk\src\genericfit.jl:34
  ...
Stacktrace:
 [1] lm(::Array{Int64,1}, ::Array{Int64,1}, ::Bool) at C:\Users\leejm516\.julia\packages\GLM\0c65q\src\lm.jl:146 (repeats 2 times)
 [2] top-level scope at none:0

julia> ols = lm([ones(3) x], y) # regression with the intercept term
LinearModel{LmResp{Array{Float64,1}},DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}}:

Coefficients:
      Estimate Std.Error t value Pr(>|t|)
x1 -0.333333 0.62361 -0.534522 0.6875
x2 2.5 0.288675 8.66025 0.0732

julia>

```

---

<div class="post-metadata">

**Author:** ![andreasnoack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasnoack/32/27_2.png) [@andreasnoack](https://discourse.julialang.org/u/andreasnoack)\
**Post date:** [November 27, 2018, 1:48pm UTC](https://discourse.julialang.org/t/linear-regression-without-the-intercept-term/18063/2 "2018-11-27T13:48:33Z")

</div>

You just have to make `x` a matrix.

```julia
julia> using GLM; x = [1,2,3]; y = [2,5,7];

julia> lm(reshape(x, length(x), 1), y)
LinearModel{LmResp{Array{Float64,1}},DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}}:

Coefficients:
     Estimate Std.Error t value Pr(>|t|)
x1 2.35714 0.0874818 26.9444 0.0014

```

---

<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:** [November 27, 2018, 3:47pm UTC](https://discourse.julialang.org/t/linear-regression-without-the-intercept-term/18063/3 "2018-11-27T15:47:14Z")

</div>

We should probably add a convenience function for this, as it keeps coming up.

---

<div class="post-metadata">

**Author:** ![leejm516](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leejm516/32/8219_2.png) [@leejm516](https://discourse.julialang.org/u/leejm516)\
**Post date:** [November 28, 2018, 12:09pm UTC](https://discourse.julialang.org/t/linear-regression-without-the-intercept-term/18063/4 "2018-11-28T12:09:54Z")

</div>

Yeah… what a silly question 🙂 Thank you for kind replying.

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [January 19, 2023, 11:22pm UTC](https://discourse.julialang.org/t/linear-regression-without-the-intercept-term/18063/5 "2023-01-19T23:22:39Z")

</div>

> [@andreasnoack](#):
>
> `lm(reshape(x, length(x), 1), y)`

The next step is missing for me:

```julia
ops = lm(reshape(x, length(x), 1), y)
y_linreg_fitted = coef(ops) .* x

```

---

<div class="post-metadata">

**Author:** ![mousum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mousum/32/35843_2.png) [@mousum](https://discourse.julialang.org/u/mousum)\
**Post date:** [January 24, 2023, 6:43am UTC](https://discourse.julialang.org/t/linear-regression-without-the-intercept-term/18063/6 "2023-01-24T06:43:33Z")

</div>

Or  
`fitted(ops)`

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [March 8, 2023, 8:09am UTC](https://discourse.julialang.org/t/linear-regression-without-the-intercept-term/18063/7 "2023-03-08T08:09:28Z")

</div>

How do you do this for logistic regression?

---

<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:** [March 8, 2023, 9:14am UTC](https://discourse.julialang.org/t/linear-regression-without-the-intercept-term/18063/8 "2023-03-08T09:14:32Z")

</div>

Same thing?

```julia
julia> using GLM

julia> df = (x = rand(10), y = rand(Bool, 10));

julia> glm(@formula(y ~ 0 + x), df, Bernoulli(), LogitLink())
StatsModels.TableRegressionModel{GeneralizedLinearModel{GLM.GlmResp{Vector{Float64}, Bernoulli{Float64}, LogitLink}, GLM.DensePredChol{Float64, LinearAlgebra.CholeskyPivoted{Float64, Matrix{Float64}, Vector{Int64}}}}, Matrix{Float64}}

y ~ 0 + x

Coefficients:
──────────────────────────────────────────────────────────────
      Coef. Std. Error z Pr(>|z|) Lower 95% Upper 95%
──────────────────────────────────────────────────────────────
x -1.32166 1.47523 -0.90 0.3703 -4.21306 1.56975
──────────────────────────────────────────────────────────────

julia> glm(reshape(df.x, 10, 1), df.y, Bernoulli(), LogitLink())
GeneralizedLinearModel{GLM.GlmResp{Vector{Float64}, Bernoulli{Float64}, LogitLink}, GLM.DensePredChol{Float64, LinearAlgebra.CholeskyPivoted{Float64, Matrix{Float64}, Vector{Int64}}}}:

Coefficients:
───────────────────────────────────────────────────────────────
       Coef. Std. Error z Pr(>|z|) Lower 95% Upper 95%
───────────────────────────────────────────────────────────────
x1 -1.32166 1.47523 -0.90 0.3703 -4.21306 1.56975
───────────────────────────────────────────────────────────────

```
