# Non-call expression encountered

**URL:** <https://discourse.julialang.org/t/non-call-expression-encountered/90725>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [November 23, 2022, 6:44pm UTC](https://discourse.julialang.org/t/non-call-expression-encountered/90725 "2022-11-23T18:44:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pino](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@Pino](https://discourse.julialang.org/u/Pino)\
**Post date:** [November 23, 2022, 6:44pm UTC](https://discourse.julialang.org/t/non-call-expression-encountered/90725/1 "2022-11-23T18:44:20Z")

</div>

New to Julia, coming from matlab.

I’m tying to run a regression using a dataframe but I want to use a specific range of the dataframe as I have many covariates.

So instead of running the following

```julia
df_test = DataFrame(A = rand(Int, 100), B = rand(Int, 100), C = rand(0:1, 100) )

model_test = glm(@formula(C ~ A + B+C),
 df_test, Binomial(), LogitLink())  

```

I would like to do like (in a wrong syntax reminescent of matlab):

```julia
model_test = glm(@formula(C ~ A + df_test[:,2:end]),
 df_test, Binomial(), LogitLink()) 

```

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [November 23, 2022, 8:45pm UTC](https://discourse.julialang.org/t/non-call-expression-encountered/90725/2 "2022-11-23T20:45:24Z")

</div>

Macroexpanding `@formula` shows that it just creates a call of `~`, `+` etc on symbolic representations of `Term` objects, i.e.,

```julia
julia> @macroexpand @formula C ~ A + B
:(StatsModels.Term(:C) ~ StatsModels.Term(:A) + StatsModels.Term(:B))

```

Thus, we can just construct the desired expression directly using functions only

```julia
julia> using StatsModels

julia> my_formula = Term(:C) ~ +( (Term(Symbol(x)) for x ∈ names(df_test)[2:end])...)
FormulaTerm
Response:
  C(unknown)
Predictors:
  B(unknown)
  C(unknown)

julia> model_test = glm(my_formula, df_test, Binomial(), LogitLink())
StatsModels.TableRegressionModel{GeneralizedLinearModel{GLM.GlmResp{Vector{Float64}, Binomial{Float64}, LogitLink}, GLM.DensePredChol{Float64, LinearAlgebra.Cholesky{Float64, Matrix{Float64}}}}, Matrix{Float64}}

C ~ 1 + B + C

```

Alternatively, you can just pass the data as a design matrix and a target vector directly:

```julia
julia> model_test = glm(Matrix(df_test[:, 2:end]), df_test[:, :C], Binomial(), LogitLink())
GeneralizedLinearModel{GLM.GlmResp{Vector{Float64}, Binomial{Float64}, LogitLink}, GLM.DensePredChol{Float64, LinearAlgebra.Cholesky{Float64, Matrix{Float64}}}}:

```

In any case, you probably want `1:end-1` as otherwise `C` is regressed on `C`.

---

<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:** [November 23, 2022, 11:22pm UTC](https://discourse.julialang.org/t/non-call-expression-encountered/90725/3 "2022-11-23T23:22:16Z")

</div>

Shorter rhs is `sum(term.(names(df)[:, 2:end]))` (or if you want to make column selection a bit more robust `names(df[:, Not(:C)])`)
