# How do I a logistic regression without the intercept in GLM.jl?

**URL:** https://discourse.julialang.org/t/how-do-i-a-logistic-regression-without-the-intercept-in-glm-jl/95720
**Category:** Machine Learning
**Created:** [March 8, 2023, 8:11am UTC](https://discourse.julialang.org/t/how-do-i-a-logistic-regression-without-the-intercept-in-glm-jl/95720 "2023-03-08T08:11:33Z")
**Posts on this page:** 5
**Page:** 1

<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:11am UTC](https://discourse.julialang.org/t/how-do-i-a-logistic-regression-without-the-intercept-in-glm-jl/95720/1 "2023-03-08T08:11:33Z")

</div>

I can’t figure out how to fit a model without the intercept

The [documentatioin](https://juliastats.org/GLM.jl/v0.11/) just says the intercept is fitted by default but doesn’t show how too do it without the intercept? is that even possible?

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/5/25575ed65d9718c7f58ef90968903447e98ffac2.png)

---

<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:17am UTC](https://discourse.julialang.org/t/how-do-i-a-logistic-regression-without-the-intercept-in-glm-jl/95720/2 "2023-03-08T08:17:00Z")

</div>

```julia
using StatsModels

# add term(-1)

#e.g.

formula = Term(:target) ~ term(-1) + sum(Term.(Symbol.(names(last_2_years4[:, Not(:target)]))))

```

---

<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:16am UTC](https://discourse.julialang.org/t/how-do-i-a-logistic-regression-without-the-intercept-in-glm-jl/95720/3 "2023-03-08T09:16:24Z")

</div>

I answered in the other thread - here are two options with or without the `@formula` macro:

```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
───────────────────────────────────────────────────────────────

```

---

<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, 9:32am UTC](https://discourse.julialang.org/t/how-do-i-a-logistic-regression-without-the-intercept-in-glm-jl/95720/4 "2023-03-08T09:32:26Z")

</div>

I want the terms to be constructed programmatically

---

<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:36am UTC](https://discourse.julialang.org/t/how-do-i-a-logistic-regression-without-the-intercept-in-glm-jl/95720/5 "2023-03-08T09:36:27Z")

</div>

Ah, that wasn’t clear from this question or the other thread, where the discussion was around using the `lm` function.

If you want to translate something you know how to write in `@formula` syntax into code, expanding the macro is helpful:

```julia
julia> @macroexpand(@formula(y ~ 0 + x))
:(StatsModels.Term(:y) ~ StatsModels.ConstantTerm(0) + StatsModels.Term(:x))

```

Also, `term` is the preferred function for construcing `Term` objects, as it will accept strings so you can do `term.(names(df[!, Not(:target)]))`
